fix(runtime): judge IPv6 forms of IPv4 addresses and never fetch through a proxy

The public-address check missed several kinds of IPv6 address:
- IPv4-compatible and 6to4 addresses carrying a private IPv4 address;
- site-local, Teredo, local-use NAT64, discard-only, benchmarking,
  ORCHID and documentation ranges.

It also refused all well-known NAT64 addresses, which would break every
named grant on a DNS64 network.

Addresses carrying an IPv4 address are now judged by that address, and
the special-purpose ranges are refused. The HTTP client is told never to
take a proxy from the environment, since it would connect to the proxy
rather than to the checked address.
This commit is contained in:
Marco Allegretti 2026-10-09 18:16:42 +00:00
parent 4e95c49062
commit b163655b87
No known key found for this signature in database
3 changed files with 56 additions and 8 deletions

View file

@ -161,17 +161,45 @@ pub fn is_public(ip: std::net::IpAddr) -> bool {
|| (a == 198 && (18..20).contains(&b)))
}
IpAddr::V6(v6) => {
let seg = v6.segments();
let embedded = |high: u16, low: u16| {
IpAddr::V4(std::net::Ipv4Addr::new(
(high >> 8) as u8,
high as u8,
(low >> 8) as u8,
low as u8,
))
};
// Forms that carry an IPv4 address are judged by that address:
// mapped and compatible addresses, 6to4 and NAT64.
if let Some(v4) = v6.to_ipv4_mapped() {
return is_public(IpAddr::V4(v4));
}
let first = v6.segments()[0];
if seg[..6] == [0; 6] && !(seg[6] == 0 && seg[7] <= 1) {
return is_public(embedded(seg[6], seg[7]));
}
if seg[0] == 0x2002 {
return is_public(embedded(seg[1], seg[2]));
}
if seg[..6] == [0x0064, 0xff9b, 0, 0, 0, 0] {
return is_public(embedded(seg[6], seg[7]));
}
!(v6.is_unspecified()
|| v6.is_loopback()
|| v6.is_multicast()
|| (first & 0xfe00) == 0xfc00
|| (first & 0xffc0) == 0xfe80
|| (first == 0x2001 && v6.segments()[1] == 0x0db8)
|| (first == 0x0064 && v6.segments()[1] == 0xff9b))
// Unique local, link-local and the deprecated site-local.
|| (seg[0] & 0xfe00) == 0xfc00
|| (seg[0] & 0xffc0) == 0xfe80
|| (seg[0] & 0xffc0) == 0xfec0
// NAT64 for local use, discard-only.
|| (seg[0] == 0x0064 && seg[1] == 0xff9b)
|| (seg[0] == 0x0100 && seg[1..4] == [0, 0, 0])
// 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 && seg[1] == 0x0db8)
|| (seg[0] & 0xfff0) == 0x3ff0)
}
}
}
@ -214,7 +242,15 @@ mod tests {
#[test]
fn only_public_unicast_addresses_are_public() {
for public in ["93.184.216.34", "1.1.1.1", "2606:4700::1111"] {
// NAT64 and 6to4 forms of a public address are public, so DNS64
// networks keep working.
for public in [
"93.184.216.34",
"1.1.1.1",
"2606:4700::1111",
"64:ff9b::5db8:d822",
"2002:5db8:d822::1",
] {
assert!(is_public(public.parse().unwrap()), "{public}");
}
for local in [
@ -238,7 +274,17 @@ mod tests {
"::ffff:127.0.0.1",
"::ffff:10.0.0.1",
"64:ff9b::a00:1",
"64:ff9b:1::1",
"2001:db8::1",
"fec0::1",
"::7f00:1",
"2002:7f00:1::",
"2002:a00:1::",
"2001::1",
"2001:2::1",
"2001:10::1",
"3fff::1",
"100::1",
] {
assert!(!is_public(local.parse().unwrap()), "{local}");
}

View file

@ -445,6 +445,8 @@ fn host_fetch(
}
let agent = ureq::AgentBuilder::new()
.resolver(GrantedResolver(reach))
// A proxy would be connected to instead of the checked address.
.try_proxy_from_env(false)
.redirects(0)
.timeout_connect(Duration::from_secs(10))
.timeout(Duration::from_secs(30))
@ -498,7 +500,7 @@ impl ureq::Resolver for GrantedResolver {
std::io::ErrorKind::PermissionDenied,
format!(
"{netloc} resolves only to addresses on this machine or a local network, \
which a fetch grant for a name does not cover"
which only a grant naming that address covers"
),
));
}

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.
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 the IPv6 forms of these, are refused, 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. 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, 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.