protocol: ensure `connect` supports connecting address

Be more defensive by preventing a node, that is not configured for
Tor, to not connect to `.onion` addresses via the `Service::connect`
method.

This means that an `Outbox::connect` should not be produced, and the
address table should not insert an entry.
This commit is contained in:
Fintan Halpenny 2026-01-26 14:40:32 +00:00 committed by Lorenz Leutgeb
parent 36d5a4c8b0
commit 91eb6fc078
1 changed files with 25 additions and 6 deletions

View File

@ -221,6 +221,8 @@ pub enum ConnectError {
SelfConnection, SelfConnection,
#[error("outbound connection limit reached when attempting {nid} ({addr})")] #[error("outbound connection limit reached when attempting {nid} ({addr})")]
LimitReached { nid: NodeId, addr: Address }, LimitReached { nid: NodeId, addr: Address },
#[error("attempted connection to {nid}, via {addr} but addresses of this kind are not supported")]
UnsupportedAddress { nid: NodeId, addr: Address },
} }
/// A store for all node data. /// A store for all node data.
@ -2265,6 +2267,9 @@ where
if nid == self.node_id() { if nid == self.node_id() {
return Err(ConnectError::SelfConnection); return Err(ConnectError::SelfConnection);
} }
if !self.is_supported_address(&addr) {
return Err(ConnectError::UnsupportedAddress { nid, addr });
}
if self.sessions.contains_key(&nid) { if self.sessions.contains_key(&nid) {
return Err(ConnectError::SessionExists { nid }); return Err(ConnectError::SessionExists { nid });
} }
@ -2503,7 +2508,7 @@ where
.filter(|entry| !self.sessions.contains_key(&entry.node)) .filter(|entry| !self.sessions.contains_key(&entry.node))
.filter(|entry| !self.config.external_addresses.contains(&entry.address.addr)) .filter(|entry| !self.config.external_addresses.contains(&entry.address.addr))
.filter(|entry| &entry.node != self.nid()) .filter(|entry| &entry.node != self.nid())
.filter(|entry| !entry.address.addr.is_onion() || self.config.onion.is_some()) .filter(|entry| self.is_supported_address(&entry.address.addr))
.fold(HashMap::new(), |mut acc, entry| { .fold(HashMap::new(), |mut acc, entry| {
acc.entry(entry.node) acc.entry(entry.node)
.and_modify(|e: &mut Peer| e.addresses.push(entry.address.clone())) .and_modify(|e: &mut Peer| e.addresses.push(entry.address.clone()))
@ -2641,11 +2646,7 @@ where
}) })
.map(|ka| (peer.nid, ka)) .map(|ka| (peer.nid, ka))
}) })
.filter(|(_, ka)| match AddressType::from(&ka.addr) { .filter(|(_, ka)| self.is_supported_address(&ka.addr));
// Only consider onion addresses if configured.
AddressType::Onion => self.config.onion.is_some(),
AddressType::Dns | AddressType::Ipv4 | AddressType::Ipv6 => true,
});
// Peers we are going to attempt connections to. // Peers we are going to attempt connections to.
let connect = available.take(wanted).collect::<Vec<_>>(); let connect = available.take(wanted).collect::<Vec<_>>();
@ -2689,6 +2690,24 @@ where
} }
} }
} }
/// Checks if the given [`Address`] is supported for connecting to.
///
/// # IPv4/IPv6/DNS
///
/// Always returns `true`.
///
/// # Tor
///
/// If the [`Address`] is an `.onion` address and the service supports onion
/// routing then this will return `true`.
fn is_supported_address(&self, address: &Address) -> bool {
match AddressType::from(address) {
// Only consider onion addresses if configured.
AddressType::Onion => self.config.onion.is_some(),
AddressType::Dns | AddressType::Ipv4 | AddressType::Ipv6 => true,
}
}
} }
/// Gives read access to the service state. /// Gives read access to the service state.