fix(runtime): match the IPv6 special-purpose ranges exactly

The documentation range was matched as 3ff0::/12 instead of 3fff::/20,
which also refused unallocated global space. ORCHIDv2 (2001:20::/28)
was missing. IPv4-translated addresses are now judged by the IPv4
address they carry. Tests cover the NAT64 /32 outside the well-known
/96.
This commit is contained in:
Marco Allegretti 2026-10-09 18:17:57 +00:00
parent b163655b87
commit 5f5474774a
No known key found for this signature in database
2 changed files with 11 additions and 3 deletions

View file

@ -175,6 +175,10 @@ pub fn is_public(ip: std::net::IpAddr) -> bool {
if let Some(v4) = v6.to_ipv4_mapped() { if let Some(v4) = v6.to_ipv4_mapped() {
return is_public(IpAddr::V4(v4)); return is_public(IpAddr::V4(v4));
} }
// IPv4-translated (::ffff:0:a.b.c.d).
if seg[..6] == [0, 0, 0, 0, 0xffff, 0] {
return is_public(embedded(seg[6], seg[7]));
}
if seg[..6] == [0; 6] && !(seg[6] == 0 && seg[7] <= 1) { if seg[..6] == [0; 6] && !(seg[6] == 0 && seg[7] <= 1) {
return is_public(embedded(seg[6], seg[7])); return is_public(embedded(seg[6], seg[7]));
} }
@ -197,9 +201,9 @@ pub fn is_public(ip: std::net::IpAddr) -> bool {
// Teredo, benchmarking, ORCHID and documentation. // Teredo, benchmarking, ORCHID and documentation.
|| (seg[0] == 0x2001 && seg[1] == 0) || (seg[0] == 0x2001 && seg[1] == 0)
|| (seg[0] == 0x2001 && seg[1] == 0x0002 && seg[2] == 0) || (seg[0] == 0x2001 && seg[1] == 0x0002 && seg[2] == 0)
|| (seg[0] == 0x2001 && (seg[1] & 0xfff0) == 0x0010) || (seg[0] == 0x2001 && matches!(seg[1] & 0xfff0, 0x0010 | 0x0020))
|| (seg[0] == 0x2001 && seg[1] == 0x0db8) || (seg[0] == 0x2001 && seg[1] == 0x0db8)
|| (seg[0] & 0xfff0) == 0x3ff0) || (seg[0] == 0x3fff && (seg[1] & 0xf000) == 0))
} }
} }
} }
@ -250,6 +254,7 @@ mod tests {
"2606:4700::1111", "2606:4700::1111",
"64:ff9b::5db8:d822", "64:ff9b::5db8:d822",
"2002:5db8:d822::1", "2002:5db8:d822::1",
"3ffe::1",
] { ] {
assert!(is_public(public.parse().unwrap()), "{public}"); assert!(is_public(public.parse().unwrap()), "{public}");
} }
@ -275,6 +280,9 @@ mod tests {
"::ffff:10.0.0.1", "::ffff:10.0.0.1",
"64:ff9b::a00:1", "64:ff9b::a00:1",
"64:ff9b:1::1", "64:ff9b:1::1",
"64:ff9b:0:0:1::1",
"::ffff:0:a00:1",
"2001:20::1",
"2001:db8::1", "2001:db8::1",
"fec0::1", "fec0::1",
"::7f00:1", "::7f00:1",

View file

@ -90,7 +90,7 @@ Each capability-controlled host import (fetch, notifications, clipboard) checks
Fetch does not follow redirects: a redirect is returned to the component as a response, so every destination it requests is checked against its grants. HTTP error statuses are responses too; only policy and transport failures are errors. Only the methods GET, HEAD, POST, PUT, PATCH, DELETE and OPTIONS are accepted, and a component cannot set `Host`, `Content-Length`, `Transfer-Encoding` or other hop-by-hop headers. Connecting times out after 10 seconds and the whole request after 30 seconds, not counting DNS resolution; response bodies are limited to 16 MiB. Fetch requires a runtime built with the `net-fetch` feature. Fetch does not follow redirects: a redirect is returned to the component as a response, so every destination it requests is checked against its grants. HTTP error statuses are responses too; only policy and transport failures are errors. Only the methods GET, HEAD, POST, PUT, PATCH, DELETE and OPTIONS are accepted, and a component cannot set `Host`, `Content-Length`, `Transfer-Encoding` or other hop-by-hop headers. Connecting times out after 10 seconds and the whole request after 30 seconds, not counting DNS resolution; response bodies are limited to 16 MiB. Fetch requires a runtime built with the `net-fetch` feature.
The runtime resolves a fetch destination itself and connects only to the addresses it allows. A grant for a name, and `net:fetch:*`, reach only public unicast addresses: loopback, unspecified, private, shared (100.64/10), link-local, multicast, broadcast, reserved, documentation and benchmarking addresses, and their IPv6 counterparts (unique local, site-local, Teredo and others), are refused, and an IPv6 address that carries an IPv4 address (mapped, compatible, 6to4, NAT64) is judged by that address, so DNS cannot point a granted name at this machine or the local network. An IPv4 address named in a grant, such as `net:fetch:127.0.0.1`, is reached as declared. Fetch never goes through a proxy. Ports are not restricted. Credentials in a URL are refused; a component sends credentials in a header, and only to a host it is granted, since redirects are not followed. IPv6 literals cannot be granted. The runtime resolves a fetch destination itself and connects only to the addresses it allows. A grant for a name, and `net:fetch:*`, reach only public unicast addresses: loopback, unspecified, private, shared (100.64/10), link-local, multicast, broadcast, reserved, documentation and benchmarking addresses, and their IPv6 counterparts (unique local, site-local, Teredo and others), are refused, and an IPv6 address that carries an IPv4 address (mapped, compatible, translated, 6to4, NAT64) is judged by that address, so DNS cannot point a granted name at this machine or the local network. An IPv4 address named in a grant, such as `net:fetch:127.0.0.1`, is reached as declared. Fetch never goes through a proxy. Ports are not restricted. Credentials in a URL are refused; a component sends credentials in a header, and only to a host it is granted, since redirects are not followed. IPv6 literals cannot be granted.
`tests/runtime/check_grants.py` runs the real runtime on a probe component (`tests/components/grants-probe`) under each kind of grant. `tests/runtime/check_grants.py` runs the real runtime on a probe component (`tests/components/grants-probe`) under each kind of grant.