From 5f5474774a2b6b5e10ed5554d69b1852ae79406b Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 18:17:57 +0000 Subject: [PATCH] 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. --- crates/weft-runtime/src/grants.rs | 12 ++++++++++-- docs/architecture.md | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/weft-runtime/src/grants.rs b/crates/weft-runtime/src/grants.rs index 6c8bd10..e98ebcf 100644 --- a/crates/weft-runtime/src/grants.rs +++ b/crates/weft-runtime/src/grants.rs @@ -175,6 +175,10 @@ pub fn is_public(ip: std::net::IpAddr) -> bool { if let Some(v4) = v6.to_ipv4_mapped() { 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) { 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. || (seg[0] == 0x2001 && seg[1] == 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] & 0xfff0) == 0x3ff0) + || (seg[0] == 0x3fff && (seg[1] & 0xf000) == 0)) } } } @@ -250,6 +254,7 @@ mod tests { "2606:4700::1111", "64:ff9b::5db8:d822", "2002:5db8:d822::1", + "3ffe::1", ] { assert!(is_public(public.parse().unwrap()), "{public}"); } @@ -275,6 +280,9 @@ mod tests { "::ffff:10.0.0.1", "64:ff9b::a00:1", "64:ff9b:1::1", + "64:ff9b:0:0:1::1", + "::ffff:0:a00:1", + "2001:20::1", "2001:db8::1", "fec0::1", "::7f00:1", diff --git a/docs/architecture.md b/docs/architecture.md index 25639bc..22762ef 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -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. -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.