Skip to content

Commit c26bf59

Browse files
fix(core): block IPv6 transition SSRF targets (#2801)
## Summary - classify IPv4-compatible, IPv4-mapped, and SIIT IPv4-translated IPv6 addresses using the existing IPv4 SSRF policy - decode IPv4 destinations under the RFC 6052 well-known NAT64 prefix - conservatively block local-use NAT64, Teredo, and 6to4 ranges - add boundary coverage for every newly handled transition prefix ## Why The workflow webhook SSRF guard previously recognized IPv4-mapped IPv6 addresses but not other standardized IPv6 forms that can embed or route to IPv4 destinations. Private, loopback, or link-local IPv4 targets represented through those forms could therefore pass address classification. This also covers the legacy SIIT IPv4-translated prefix (`::ffff:0:0:0/96`), which Rust's `Ipv6Addr::to_ipv4()` does not recognize but an SIIT-enabled network may route to the IPv4 value in the final 32 bits. Network-specific NAT64 prefixes remain a deployment concern and should be restricted through egress policy; they cannot be inferred generically from an IPv6 address. ## Test plan - `cargo fmt --all -- --check` - `cargo test -p buzz-core network` (35 passed) - `cargo clippy -p buzz-core --all-targets -- -D warnings` - `git diff --check` --------- Co-authored-by: Amp <[email protected]>
1 parent 50655ac commit c26bf59

1 file changed

Lines changed: 169 additions & 7 deletions

File tree

‎crates/buzz-core/src/network.rs‎

Lines changed: 169 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
//! Provides shared helpers used across crates for SSRF protection and
44
//! IP address classification.
55
6+
// RFC 6052 well-known NAT64 prefix (64:ff9b::/96).
7+
const NAT64_WELL_KNOWN_PREFIX: [u8; 12] = [0x00, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0];
8+
9+
// Legacy SIIT IPv4-translated prefix (::ffff:0:0:0/96).
10+
const IPV4_TRANSLATED_PREFIX: [u8; 12] = [0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0];
11+
12+
/// Extract an IPv4 address stored in the final four octets under a `/96` prefix.
13+
///
14+
/// Using network-order octets directly avoids error-prone segment shifting.
15+
fn embedded_ipv4(v6: &std::net::Ipv6Addr, prefix: &[u8; 12]) -> Option<std::net::Ipv4Addr> {
16+
let octets = v6.octets();
17+
octets
18+
.starts_with(prefix)
19+
.then(|| std::net::Ipv4Addr::new(octets[12], octets[13], octets[14], octets[15]))
20+
}
21+
622
/// Returns `true` if the IP address is in a private, reserved, or
723
/// loopback range. Used for SSRF protection — webhook targets must
824
/// not resolve to these addresses.
@@ -21,7 +37,12 @@
2137
/// - IPv6 link-local fe80::/10
2238
/// - IPv6 multicast ff00::/8
2339
/// - IPv6 documentation 2001:db8::/32 (RFC 3849) — should never appear in production
24-
/// - IPv4-mapped IPv6 ::ffff:0:0/96 (checked recursively against IPv4 rules)
40+
/// - IPv4-compatible and mapped IPv6 (checked recursively against IPv4 rules)
41+
/// - IPv4-translated ::ffff:0:0:0/96 (embedded IPv4 checked recursively)
42+
/// - NAT64 well-known 64:ff9b::/96 (embedded IPv4 checked recursively)
43+
/// - NAT64 local-use 64:ff9b:1::/48 (RFC 8215)
44+
/// - Teredo 2001::/32 (RFC 4380)
45+
/// - 6to4 2002::/16 (RFC 3056)
2546
pub fn is_private_ip(ip: &std::net::IpAddr) -> bool {
2647
match ip {
2748
std::net::IpAddr::V4(v4) => {
@@ -38,17 +59,37 @@ pub fn is_private_ip(ip: &std::net::IpAddr) -> bool {
3859
|| (octets[0] == 198 && (octets[1] & 0xFE) == 18)
3960
}
4061
std::net::IpAddr::V6(v6) => {
41-
// Check IPv4-mapped IPv6 addresses (::ffff:x.x.x.x) against IPv4 rules.
42-
if let Some(v4) = v6.to_ipv4_mapped() {
62+
// Check IPv4-compatible and mapped addresses against IPv4 rules.
63+
if let Some(v4) = v6.to_ipv4() {
64+
return is_private_ip(&std::net::IpAddr::V4(v4));
65+
}
66+
67+
let segments = v6.segments();
68+
69+
// NAT64 well-known prefix (RFC 6052). Preserve access to public IPv4
70+
// destinations while rejecting embedded private/reserved addresses.
71+
if let Some(v4) = embedded_ipv4(v6, &NAT64_WELL_KNOWN_PREFIX) {
4372
return is_private_ip(&std::net::IpAddr::V4(v4));
4473
}
74+
75+
// Legacy SIIT IPv4-translated addresses can route to the IPv4 value
76+
// in their final four octets but are not recognized by `to_ipv4()`.
77+
if let Some(v4) = embedded_ipv4(v6, &IPV4_TRANSLATED_PREFIX) {
78+
return is_private_ip(&std::net::IpAddr::V4(v4));
79+
}
80+
4581
v6.is_loopback()
4682
|| v6.is_unspecified()
47-
|| v6.segments()[0] & 0xfe00 == 0xfc00 // fc00::/7 ULA
48-
|| v6.segments()[0] & 0xffc0 == 0xfe80 // fe80::/10 link-local
49-
|| v6.segments()[0] & 0xff00 == 0xff00 // ff00::/8 multicast
83+
|| segments[0] & 0xfe00 == 0xfc00 // fc00::/7 ULA
84+
|| segments[0] & 0xffc0 == 0xfe80 // fe80::/10 link-local
85+
|| segments[0] & 0xff00 == 0xff00 // ff00::/8 multicast
86+
|| (segments[0] == 0x0064
87+
&& segments[1] == 0xff9b
88+
&& segments[2] == 1) // 64:ff9b:1::/48 local-use NAT64
89+
|| (segments[0] == 0x2001 && segments[1] == 0) // 2001::/32 Teredo
90+
|| segments[0] == 0x2002 // 2002::/16 6to4
5091
// RFC 3849 — documentation range, should never appear in production
51-
|| (v6.segments()[0] == 0x2001 && v6.segments()[1] == 0x0db8)
92+
|| (segments[0] == 0x2001 && segments[1] == 0x0db8)
5293
}
5394
}
5495
}
@@ -133,6 +174,127 @@ mod tests {
133174
fn test_ipv4_mapped_v6_public() {
134175
assert!(!is_private_ip(&"::ffff:8.8.8.8".parse::<IpAddr>().unwrap()));
135176
}
177+
#[test]
178+
fn test_ipv4_compatible_v6_private() {
179+
assert!(is_private_ip(&"::10.0.0.1".parse::<IpAddr>().unwrap()));
180+
assert!(is_private_ip(&"::127.0.0.1".parse::<IpAddr>().unwrap()));
181+
assert!(is_private_ip(
182+
&"::169.254.169.254".parse::<IpAddr>().unwrap()
183+
));
184+
assert!(!is_private_ip(&"::8.8.8.8".parse::<IpAddr>().unwrap()));
185+
}
186+
#[test]
187+
fn test_nat64_well_known_prefix() {
188+
let first = "64:ff9b::".parse().unwrap();
189+
let last = "64:ff9b::ffff:ffff".parse().unwrap();
190+
assert_eq!(
191+
embedded_ipv4(&first, &NAT64_WELL_KNOWN_PREFIX),
192+
Some("0.0.0.0".parse().unwrap())
193+
);
194+
assert_eq!(
195+
embedded_ipv4(&last, &NAT64_WELL_KNOWN_PREFIX),
196+
Some("255.255.255.255".parse().unwrap())
197+
);
198+
let embedded = "64:ff9b::172.16.1.2".parse().unwrap();
199+
assert_eq!(
200+
embedded_ipv4(&embedded, &NAT64_WELL_KNOWN_PREFIX),
201+
Some("172.16.1.2".parse().unwrap())
202+
);
203+
assert!(is_private_ip(
204+
&"64:ff9b::10.0.0.1".parse::<IpAddr>().unwrap()
205+
));
206+
assert!(is_private_ip(
207+
&"64:ff9b::127.0.0.1".parse::<IpAddr>().unwrap()
208+
));
209+
assert!(is_private_ip(
210+
&"64:ff9b::169.254.169.254".parse::<IpAddr>().unwrap()
211+
));
212+
assert!(!is_private_ip(
213+
&"64:ff9b::8.8.8.8".parse::<IpAddr>().unwrap()
214+
));
215+
assert!(!is_private_ip(
216+
&"64:ff9a:ffff:ffff:ffff:ffff:ffff:ffff"
217+
.parse::<IpAddr>()
218+
.unwrap()
219+
));
220+
assert!(!is_private_ip(&"64:ff9b::1:0:0".parse::<IpAddr>().unwrap()));
221+
}
222+
#[test]
223+
fn test_ipv4_translated_prefix() {
224+
let first = "0:0:0:0:ffff:0:0:0".parse().unwrap();
225+
let last = "0:0:0:0:ffff:0:ffff:ffff".parse().unwrap();
226+
assert_eq!(
227+
embedded_ipv4(&first, &IPV4_TRANSLATED_PREFIX),
228+
Some("0.0.0.0".parse().unwrap())
229+
);
230+
assert_eq!(
231+
embedded_ipv4(&last, &IPV4_TRANSLATED_PREFIX),
232+
Some("255.255.255.255".parse().unwrap())
233+
);
234+
assert!(is_private_ip(
235+
&"::ffff:0:10.0.0.1".parse::<IpAddr>().unwrap()
236+
));
237+
assert!(is_private_ip(
238+
&"::ffff:0:127.0.0.1".parse::<IpAddr>().unwrap()
239+
));
240+
assert!(is_private_ip(
241+
&"::ffff:0:169.254.169.254".parse::<IpAddr>().unwrap()
242+
));
243+
assert!(!is_private_ip(
244+
&"::ffff:0:8.8.8.8".parse::<IpAddr>().unwrap()
245+
));
246+
assert!(!is_private_ip(
247+
&"0:0:0:0:fffe:ffff:ffff:ffff".parse::<IpAddr>().unwrap()
248+
));
249+
assert!(!is_private_ip(
250+
&"0:0:0:0:ffff:1:0:0".parse::<IpAddr>().unwrap()
251+
));
252+
}
253+
#[test]
254+
fn test_nat64_local_use_prefix_boundaries() {
255+
assert!(is_private_ip(&"64:ff9b:1::".parse::<IpAddr>().unwrap()));
256+
assert!(is_private_ip(
257+
&"64:ff9b:1:ffff:ffff:ffff:ffff:ffff"
258+
.parse::<IpAddr>()
259+
.unwrap()
260+
));
261+
assert!(!is_private_ip(
262+
&"64:ff9b::ffff:ffff:ffff:ffff:ffff"
263+
.parse::<IpAddr>()
264+
.unwrap()
265+
));
266+
assert!(!is_private_ip(&"64:ff9b:2::".parse::<IpAddr>().unwrap()));
267+
}
268+
#[test]
269+
fn test_teredo_prefix_boundaries() {
270+
assert!(is_private_ip(&"2001::".parse::<IpAddr>().unwrap()));
271+
assert!(is_private_ip(
272+
&"2001:0:ffff:ffff:ffff:ffff:ffff:ffff"
273+
.parse::<IpAddr>()
274+
.unwrap()
275+
));
276+
assert!(!is_private_ip(
277+
&"2000:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
278+
.parse::<IpAddr>()
279+
.unwrap()
280+
));
281+
assert!(!is_private_ip(&"2001:1::1".parse::<IpAddr>().unwrap()));
282+
}
283+
#[test]
284+
fn test_6to4_prefix_boundaries() {
285+
assert!(is_private_ip(&"2002::".parse::<IpAddr>().unwrap()));
286+
assert!(is_private_ip(
287+
&"2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
288+
.parse::<IpAddr>()
289+
.unwrap()
290+
));
291+
assert!(!is_private_ip(
292+
&"2001:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
293+
.parse::<IpAddr>()
294+
.unwrap()
295+
));
296+
assert!(!is_private_ip(&"2003::1".parse::<IpAddr>().unwrap()));
297+
}
136298

137299
// CGNAT (RFC 6598) — 100.64.0.0/10
138300
#[test]

0 commit comments

Comments
 (0)