diff --git a/crates/weft-runtime/src/grants.rs b/crates/weft-runtime/src/grants.rs index ab60c2d..b208ded 100644 --- a/crates/weft-runtime/src/grants.rs +++ b/crates/weft-runtime/src/grants.rs @@ -47,18 +47,31 @@ impl Grants { } #[cfg_attr(not(feature = "net-fetch"), allow(dead_code))] - /// Whether a request to `host` is covered by a fetch grant. `host` is the - /// URL's host in the lowercase form URL parsing produces. - pub fn fetch_host(&self, host: &str) -> Result<(), String> { + /// Whether a request to `host` is covered by a fetch grant, and which + /// addresses it may reach. `host` is the URL's host in the lowercase form + /// URL parsing produces. + pub fn fetch_host(&self, host: &str) -> Result { if self.fetch.is_empty() { 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::().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 { FetchScope::AnyHost => true, FetchScope::Host(granted) => granted == host, }); if covered { - Ok(()) + Ok(Reach::Public) } else { Err(format!("capability net:fetch:{host} is not granted")) } @@ -115,6 +128,54 @@ 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) => { + if let Some(v4) = v6.to_ipv4_mapped() { + return is_public(IpAddr::V4(v4)); + } + let first = v6.segments()[0]; + !(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)) + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -138,6 +199,51 @@ mod tests { 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() { + for public in ["93.184.216.34", "1.1.1.1", "2606:4700::1111"] { + 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", + "2001:db8::1", + ] { + assert!(!is_public(local.parse().unwrap()), "{local}"); + } + } + #[test] fn host_fetch_grants_match_exact_hosts() { let mut grants = Grants::default(); diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs index 1fe5d05..b221996 100644 --- a/crates/weft-runtime/src/main.rs +++ b/crates/weft-runtime/src/main.rs @@ -416,7 +416,14 @@ fn host_fetch( return Err(format!("unsupported URL scheme '{}'", parsed.scheme())); } 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 // methods are accepted. if !matches!( @@ -437,6 +444,7 @@ fn host_fetch( } } let agent = ureq::AgentBuilder::new() + .resolver(GrantedResolver(reach)) .redirects(0) .timeout_connect(Duration::from_secs(10)) .timeout(Duration::from_secs(30)) @@ -471,6 +479,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> { + 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 a fetch grant for a name does not cover" + ), + )); + } + Ok(addresses) + } +} + #[cfg(all(feature = "wasmtime-runtime", not(feature = "net-fetch")))] fn host_fetch( grants: &Grants, diff --git a/docs/architecture.md b/docs/architecture.md index ee92a31..04dfab7 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -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: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:` | `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:clipboard:read` / `sys:clipboard:write` | `weft:app/clipboard#read` / `#write` | | `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 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 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. `tests/runtime/check_grants.py` runs the real runtime on a probe component (`tests/components/grants-probe`) under each kind of grant. diff --git a/tests/runtime/check_grants.py b/tests/runtime/check_grants.py index bb1abc5..07c232f 100644 --- a/tests/runtime/check_grants.py +++ b/tests/runtime/check_grants.py @@ -16,9 +16,12 @@ scenario passes a different set of `--grant` and `--preopen` arguments: denied; - a host-specific fetch grant: requests to that host return their real status (200, 404, and 302 without following the redirect to another - host); requests to another host, a forged `Host` header, a method that - injects a request line and a `file:` URL are refused; -- a wildcard fetch grant: the other host is reachable. + host); requests to another host, credentials in the URL, a forged + `Host` header, a method that injects a request line and a `file:` URL + 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 capabilities passed as `--grant`, unknown capabilities and preopens @@ -42,8 +45,13 @@ APP_ID = "org.weft.test.grants" NOT_GRANTED = "is not granted" +# Paths the test server was asked for. +REQUESTS = [] + + class Handler(http.server.BaseHTTPRequestHandler): def do_GET(self): + REQUESTS.append(self.path) if self.path == "/ok": self.send_response(200) elif self.path == "/redirect": @@ -75,7 +83,8 @@ class Runner: f"fetch-ok http://127.0.0.1:{port}/ok\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-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): 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-redirect", True, contains="302") 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-bad-method", False, contains="unsupported HTTP method") expect(probes, "fetch-file-scheme", False, contains="unsupported URL scheme") + local = "local network" + REQUESTS.clear() _, 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 ( (["--grant", "fs:rw:app-data"], "is not granted through --grant"),