radicle/node: `Address::is_local` for DNS names

`Address:is_local` would return `false` for all DNS names. This is
incorrect, with one counterexample being the name "localhost", which
generally resolves to a local (usually loopback) address.

The function is changed to catch "localhost", but also the top level
domain ".localhost", which is reserved in RFC 2606, Section 2.

`Address::is_routable` would return `true` for all DNS names. While it
is much harder to decide global routeability based on a domain name, as
these usually have to be resolved to an address before being able to
judge routability, there is one particular class of names, namely local
ones (see above), which are not globally routable.
This commit is contained in:
Lorenz Leutgeb 2025-11-29 12:34:06 +01:00 committed by Fintan Halpenny
parent dc624ed518
commit f9a62e7d8d
1 changed files with 10 additions and 2 deletions

View File

@ -465,8 +465,15 @@ pub struct Address(
impl Address {
/// Check whether this address is from the local network.
pub fn is_local(&self) -> bool {
match self.0.host {
HostName::Ip(ip) => address::is_local(&ip),
match &self.0.host {
HostName::Ip(ip) => address::is_local(ip),
HostName::Dns(name) => {
let name = name.strip_suffix(".").unwrap_or(name);
// RFC 2606, Section 2
// <https://datatracker.ietf.org/doc/html/rfc2606#section-2>
name.ends_with(".localhost") || name == "localhost"
}
_ => false,
}
}
@ -475,6 +482,7 @@ impl Address {
pub fn is_routable(&self) -> bool {
match self.0.host {
HostName::Ip(ip) => address::is_routable(&ip),
HostName::Dns(_) => !self.is_local(),
_ => true,
}
}