Merge fix/fetch-public-destinations: reach only public addresses through a fetch grant for a name (#26)

This commit is contained in:
Marco Allegretti 2026-10-10 09:25:44 +02:00 committed by GitHub
commit 2020bfe2c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 226 additions and 12 deletions

View file

@ -47,18 +47,31 @@ impl Grants {
} }
#[cfg_attr(not(feature = "net-fetch"), allow(dead_code))] #[cfg_attr(not(feature = "net-fetch"), allow(dead_code))]
/// Whether a request to `host` is covered by a fetch grant. `host` is the /// Whether a request to `host` is covered by a fetch grant, and which
/// URL's host in the lowercase form URL parsing produces. /// addresses it may reach. `host` is the URL's host in the lowercase form
pub fn fetch_host(&self, host: &str) -> Result<(), String> { /// URL parsing produces.
pub fn fetch_host(&self, host: &str) -> Result<Reach, String> {
if self.fetch.is_empty() { if self.fetch.is_empty() {
return Err("capability net:fetch is not granted".to_owned()); return Err("capability net:fetch is not granted".to_owned());
} }
// An address named in a grant is reached as declared, wherever it
// is; a name, or any host, reaches only public addresses, so a
// granted name cannot be pointed at this machine or the local
// network through DNS.
if host.parse::<std::net::Ipv4Addr>().is_ok()
&& self
.fetch
.iter()
.any(|scope| matches!(scope, FetchScope::Host(granted) if granted == host))
{
return Ok(Reach::Declared);
}
let covered = self.fetch.iter().any(|scope| match scope { let covered = self.fetch.iter().any(|scope| match scope {
FetchScope::AnyHost => true, FetchScope::AnyHost => true,
FetchScope::Host(granted) => granted == host, FetchScope::Host(granted) => granted == host,
}); });
if covered { if covered {
Ok(()) Ok(Reach::Public)
} else { } else {
Err(format!("capability net:fetch:{host} is not granted")) Err(format!("capability net:fetch:{host} is not granted"))
} }
@ -115,6 +128,86 @@ impl Preopen {
} }
} }
/// The addresses a granted request may connect to.
#[cfg_attr(not(feature = "net-fetch"), allow(dead_code))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Reach {
/// Only public addresses.
Public,
/// The address the grant names, public or not.
Declared,
}
/// Whether `ip` is a public unicast address: not loopback, unspecified,
/// private, shared (CGNAT), link-local, multicast, broadcast, reserved,
/// documentation or benchmarking space, nor an IPv6 form of one.
#[cfg_attr(not(feature = "net-fetch"), allow(dead_code))]
pub fn is_public(ip: std::net::IpAddr) -> bool {
use std::net::IpAddr;
match ip {
IpAddr::V4(v4) => {
let [a, b, c, _] = v4.octets();
!(v4.is_unspecified()
|| v4.is_loopback()
|| v4.is_private()
|| v4.is_link_local()
|| v4.is_multicast()
|| v4.is_broadcast()
|| v4.is_documentation()
|| a == 0
|| a >= 240
|| (a == 100 && (64..128).contains(&b))
|| (a == 192 && b == 0 && c == 0)
|| (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));
}
// 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]));
}
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()
// 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 && matches!(seg[1] & 0xfff0, 0x0010 | 0x0020))
|| (seg[0] == 0x2001 && seg[1] == 0x0db8)
|| (seg[0] == 0x3fff && (seg[1] & 0xf000) == 0))
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -138,6 +231,73 @@ mod tests {
assert!(grants.notifications().is_err()); assert!(grants.notifications().is_err());
} }
#[test]
fn named_and_wildcard_grants_reach_only_public_addresses() {
let mut grants = Grants::default();
grants.add("net:fetch:api.example.org").unwrap();
grants.add("net:fetch:127.0.0.1").unwrap();
assert_eq!(grants.fetch_host("api.example.org"), Ok(Reach::Public));
// An address named in a grant is reached as declared.
assert_eq!(grants.fetch_host("127.0.0.1"), Ok(Reach::Declared));
let mut any = Grants::default();
any.add("net:fetch:*").unwrap();
assert_eq!(any.fetch_host("10.0.0.1"), Ok(Reach::Public));
}
#[test]
fn only_public_unicast_addresses_are_public() {
// 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",
"3ffe::1",
] {
assert!(is_public(public.parse().unwrap()), "{public}");
}
for local in [
"127.0.0.1",
"0.0.0.0",
"10.1.2.3",
"172.16.0.1",
"192.168.1.1",
"169.254.169.254",
"100.64.0.1",
"224.0.0.1",
"255.255.255.255",
"192.0.2.1",
"198.18.0.1",
"240.0.0.1",
"::1",
"::",
"fd00::1",
"fe80::1",
"ff02::1",
"::ffff:127.0.0.1",
"::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",
"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}");
}
}
#[test] #[test]
fn host_fetch_grants_match_exact_hosts() { fn host_fetch_grants_match_exact_hosts() {
let mut grants = Grants::default(); let mut grants = Grants::default();

View file

@ -416,7 +416,14 @@ fn host_fetch(
return Err(format!("unsupported URL scheme '{}'", parsed.scheme())); return Err(format!("unsupported URL scheme '{}'", parsed.scheme()));
} }
let host = parsed.host_str().ok_or("URL has no host")?; let host = parsed.host_str().ok_or("URL has no host")?;
grants.fetch_host(host)?; let reach = grants.fetch_host(host)?;
// Credentials are never taken from a URL; a component that needs them
// sends them in a header to the host it is granted.
if !parsed.username().is_empty() || parsed.password().is_some() {
return Err(
"URLs with credentials are not supported; send an Authorization header".to_owned(),
);
}
// The method is written into the request line as given, so only known // The method is written into the request line as given, so only known
// methods are accepted. // methods are accepted.
if !matches!( if !matches!(
@ -437,6 +444,9 @@ fn host_fetch(
} }
} }
let agent = ureq::AgentBuilder::new() 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) .redirects(0)
.timeout_connect(Duration::from_secs(10)) .timeout_connect(Duration::from_secs(10))
.timeout(Duration::from_secs(30)) .timeout(Duration::from_secs(30))
@ -471,6 +481,33 @@ fn host_fetch(
}) })
} }
/// Resolves a fetch destination and keeps only the addresses its grant
/// may reach; the connection is made to those addresses alone, so the
/// check cannot be bypassed by a second, different DNS answer.
#[cfg(all(feature = "wasmtime-runtime", feature = "net-fetch"))]
struct GrantedResolver(grants::Reach);
#[cfg(all(feature = "wasmtime-runtime", feature = "net-fetch"))]
impl ureq::Resolver for GrantedResolver {
fn resolve(&self, netloc: &str) -> std::io::Result<Vec<std::net::SocketAddr>> {
use std::net::ToSocketAddrs;
let addresses: Vec<_> = netloc
.to_socket_addrs()?
.filter(|a| self.0 == grants::Reach::Declared || grants::is_public(a.ip()))
.collect();
if addresses.is_empty() {
return Err(std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
format!(
"{netloc} resolves only to addresses on this machine or a local network, \
which only a grant naming that address covers"
),
));
}
Ok(addresses)
}
}
#[cfg(all(feature = "wasmtime-runtime", not(feature = "net-fetch")))] #[cfg(all(feature = "wasmtime-runtime", not(feature = "net-fetch")))]
fn host_fetch( fn host_fetch(
grants: &Grants, grants: &Grants,

View file

@ -83,14 +83,14 @@ Each capability-controlled host import (fetch, notifications, clipboard) checks
| `fs:read:app-data` / `fs:rw:app-data` | Preopen the app's data directory (see *App data*) as `/data`, read-only or read-write | | `fs:read:app-data` / `fs:rw:app-data` | Preopen the app's data directory (see *App data*) as `/data`, read-only or read-write |
| `fs:read:xdg-documents` / `fs:rw:xdg-documents` | Preopen the documents directory from the XDG user-dirs configuration (`XDG_DOCUMENTS_DIR`) as `/xdg/documents`; the launch fails when none is configured | | `fs:read:xdg-documents` / `fs:rw:xdg-documents` | Preopen the documents directory from the XDG user-dirs configuration (`XDG_DOCUMENTS_DIR`) as `/xdg/documents`; the launch fails when none is configured |
| `net:fetch:<host>` | `weft:app/fetch` to that exact host (a lowercase DNS name or dotted IPv4 address) over HTTP or HTTPS, on any port | | `net:fetch:<host>` | `weft:app/fetch` to that exact host (a lowercase DNS name or dotted IPv4 address) over HTTP or HTTPS, on any port |
| `net:fetch:*` | `weft:app/fetch` to any host | | `net:fetch:*` | `weft:app/fetch` to any host at a public address |
| `sys:notifications` | `weft:app/notifications` | | `sys:notifications` | `weft:app/notifications` |
| `sys:clipboard:read` / `sys:clipboard:write` | `weft:app/clipboard#read` / `#write` | | `sys:clipboard:read` / `sys:clipboard:write` | `weft:app/clipboard#read` / `#write` |
| `hw:gpu:compute` / `hw:gpu:render` | Not supported; the launch fails | | `hw:gpu:compute` / `hw:gpu:render` | Not supported; the launch fails |
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.
Fetch grants name hosts, not addresses: ports are not restricted, and a host that resolves to a loopback or private address (or `net:fetch:*`) can reach local services. 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.

View file

@ -16,9 +16,12 @@ scenario passes a different set of `--grant` and `--preopen` arguments:
denied; denied;
- a host-specific fetch grant: requests to that host return their real - a host-specific fetch grant: requests to that host return their real
status (200, 404, and 302 without following the redirect to another status (200, 404, and 302 without following the redirect to another
host); requests to another host, a forged `Host` header, a method that host); requests to another host, credentials in the URL, a forged
injects a request line and a `file:` URL are refused; `Host` header, a method that injects a request line and a `file:` URL
- a wildcard fetch grant: the other host is reachable. are refused;
- a wildcard fetch grant, and a grant for the name `localhost`: the local
server is refused, since only an address named in a grant reaches this
machine or a local network; the connection is never made.
It also checks that the runtime refuses, with a specific error, filesystem It also checks that the runtime refuses, with a specific error, filesystem
capabilities passed as `--grant`, unknown capabilities and preopens capabilities passed as `--grant`, unknown capabilities and preopens
@ -42,8 +45,13 @@ APP_ID = "org.weft.test.grants"
NOT_GRANTED = "is not granted" NOT_GRANTED = "is not granted"
# Paths the test server was asked for.
REQUESTS = []
class Handler(http.server.BaseHTTPRequestHandler): class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
REQUESTS.append(self.path)
if self.path == "/ok": if self.path == "/ok":
self.send_response(200) self.send_response(200)
elif self.path == "/redirect": elif self.path == "/redirect":
@ -75,7 +83,8 @@ class Runner:
f"fetch-ok http://127.0.0.1:{port}/ok\n" f"fetch-ok http://127.0.0.1:{port}/ok\n"
f"fetch-missing http://127.0.0.1:{port}/missing\n" f"fetch-missing http://127.0.0.1:{port}/missing\n"
f"fetch-redirect http://127.0.0.1:{port}/redirect\n" f"fetch-redirect http://127.0.0.1:{port}/redirect\n"
f"fetch-other-host http://localhost:{port}/ok\n") f"fetch-other-host http://localhost:{port}/ok\n"
f"fetch-credentials http://user:secret@127.0.0.1:{port}/ok\n")
def run(self, *arguments, expect_success=True): def run(self, *arguments, expect_success=True):
command = [str(self.runtime), APP_ID, "1", "--module", str(self.module), command = [str(self.runtime), APP_ID, "1", "--module", str(self.module),
@ -140,12 +149,20 @@ def check(runner, data):
expect(probes, "fetch-missing", True, contains="404") expect(probes, "fetch-missing", True, contains="404")
expect(probes, "fetch-redirect", True, contains="302") expect(probes, "fetch-redirect", True, contains="302")
expect(probes, "fetch-other-host", False, contains=NOT_GRANTED) expect(probes, "fetch-other-host", False, contains=NOT_GRANTED)
expect(probes, "fetch-credentials", False, contains="credentials")
expect(probes, "fetch-host-header", False, contains="set by the runtime") expect(probes, "fetch-host-header", False, contains="set by the runtime")
expect(probes, "fetch-bad-method", False, contains="unsupported HTTP method") expect(probes, "fetch-bad-method", False, contains="unsupported HTTP method")
expect(probes, "fetch-file-scheme", False, contains="unsupported URL scheme") expect(probes, "fetch-file-scheme", False, contains="unsupported URL scheme")
local = "local network"
REQUESTS.clear()
_, probes = runner.run("--grant", "net:fetch:*") _, probes = runner.run("--grant", "net:fetch:*")
expect(probes, "fetch-other-host", True, contains="200") expect(probes, "fetch-other-host", False, contains=local)
expect(probes, "fetch-ok", False, contains=local)
_, probes = runner.run("--grant", "net:fetch:localhost")
expect(probes, "fetch-other-host", False, contains=local)
if REQUESTS:
raise AssertionError(f"refused fetches reached the server: {REQUESTS}")
for arguments, message in ( for arguments, message in (
(["--grant", "fs:rw:app-data"], "is not granted through --grant"), (["--grant", "fs:rw:app-data"], "is not granted through --grant"),