Skip to content

Commit 1134d60

Browse files
Joffrefclaude
andcommitted
fix(core): classify NAT64 addresses by the longest matching prefix
embedded_ipv4 returned the first registered prefix that contained the address. With a configured 2001:db8::/32 and a later-discovered 2001:db8:8c52:7003::/96, 2001:db8:8c52:7003::7f00:1 was read through the /32 as 140.82.112.3 and passed the SSRF checks, while the /96 route translates it to 127.0.0.1. Pick the longest matching prefix, the well-known prefix included, the same way the route that carries the packet does. The tests register the broad prefix first and check loopback, metadata and private destinations under the nested one. They use RFC 9637 documentation space because the registry is process-wide and a 2001:db8::/32 would change how other tests classify their 2001:db8 addresses. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Joffref <[email protected]>
1 parent f7f1f99 commit 1134d60

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

‎crates/openshell-core/src/net.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,29 @@ mod tests {
859859
assert!(!is_internal_ip(public));
860860
}
861861

862+
#[test]
863+
fn nat64_nested_prefix_registered_last_classifies_by_the_nested_prefix() {
864+
// Only this test registers these documentation prefixes (RFC 9637).
865+
// Under the broad /32 alone every address below embeds 140.82.112.3,
866+
// which is public.
867+
let broad = nat64::Nat64Prefix::new("3fff:6464::/32".parse().unwrap()).unwrap();
868+
let nested = nat64::Nat64Prefix::new("3fff:6464:8c52:7003::/96".parse().unwrap()).unwrap();
869+
let loopback: IpAddr = "3fff:6464:8c52:7003::7f00:1".parse().unwrap();
870+
let metadata: IpAddr = "3fff:6464:8c52:7003::a9fe:a9fe".parse().unwrap();
871+
let private: IpAddr = "3fff:6464:8c52:7003::a00:5".parse().unwrap();
872+
nat64::register_network_prefix(broad);
873+
assert!(!is_internal_ip(loopback), "only the /32 is known");
874+
nat64::register_network_prefix(nested);
875+
assert!(is_always_blocked_ip(loopback));
876+
assert!(is_always_blocked_ip(metadata));
877+
assert!(is_internal_ip(private));
878+
assert!(!is_always_blocked_ip(private));
879+
let public: IpNet = "140.82.112.0/20".parse().unwrap();
880+
let rfc1918: IpNet = "10.0.0.0/8".parse().unwrap();
881+
assert!(!allowed_net_contains(&public, private));
882+
assert!(allowed_net_contains(&rfc1918, private));
883+
}
884+
862885
#[test]
863886
fn nat64_local_use_range_is_internal_without_a_registered_prefix() {
864887
assert!(is_internal_ip("64:ff9b:1::8c52:7003".parse().unwrap()));

‎crates/openshell-core/src/net/nat64.rs‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,21 @@ pub fn network_prefixes() -> Vec<Nat64Prefix> {
160160

161161
/// The IPv4 address `addr` translates to under the well-known prefix or any
162162
/// registered network-specific prefix.
163+
///
164+
/// When prefixes overlap, the longest matching prefix wins, as it does for
165+
/// the route that carries the packet: a `/96` discovered inside a configured
166+
/// `/32` embeds the IPv4 address in its last 32 bits, whatever order the two
167+
/// were registered in.
163168
#[must_use]
164169
pub fn embedded_ipv4(addr: Ipv6Addr) -> Option<Ipv4Addr> {
165-
if let Some(v4) = WELL_KNOWN_PREFIX.embedded_ipv4(addr) {
166-
return Some(v4);
167-
}
168-
NETWORK_PREFIXES
170+
let prefixes = NETWORK_PREFIXES
169171
.read()
170-
.unwrap_or_else(std::sync::PoisonError::into_inner)
171-
.iter()
172-
.find_map(|prefix| prefix.embedded_ipv4(addr))
172+
.unwrap_or_else(std::sync::PoisonError::into_inner);
173+
std::iter::once(&WELL_KNOWN_PREFIX)
174+
.chain(prefixes.iter())
175+
.filter(|prefix| prefix.0.contains(&addr))
176+
.max_by_key(|prefix| prefix.0.prefix_len())
177+
.and_then(|prefix| prefix.embedded_ipv4(addr))
173178
}
174179

175180
#[cfg(test)]
@@ -262,4 +267,22 @@ mod tests {
262267
assert!(network_prefixes().contains(&nsp));
263268
assert_eq!(embedded_ipv4(addr), Some(Ipv4Addr::new(10, 0, 0, 1)));
264269
}
270+
271+
#[test]
272+
fn overlapping_prefixes_use_the_longest_match() {
273+
// Only this test registers these documentation prefixes (RFC 9637).
274+
// The broad prefix goes in first, as a configured prefix would before
275+
// RFC 7050 discovers the nested one.
276+
register_network_prefix(prefix("3fff:64::/32"));
277+
register_network_prefix(prefix("3fff:64:8c52:7003::/96"));
278+
assert_eq!(
279+
embedded_ipv4("3fff:64:8c52:7003::7f00:1".parse().unwrap()),
280+
Some(Ipv4Addr::LOCALHOST)
281+
);
282+
// Outside the /96, the /32 still applies.
283+
assert_eq!(
284+
embedded_ipv4("3fff:64:a00:5::".parse().unwrap()),
285+
Some(Ipv4Addr::new(10, 0, 0, 5))
286+
);
287+
}
265288
}

0 commit comments

Comments
 (0)