diff --git a/app/src/admin.spec.md b/app/src/admin.spec.md index 03c9aee..a8d2f51 100644 --- a/app/src/admin.spec.md +++ b/app/src/admin.spec.md @@ -252,13 +252,16 @@ it in the background (headless); the virtual monitor for screen sharing comes ne (`--proxy-server`, and `--proxy-bypass-list=<-loopback>` so not even loopback is direct; QUIC off, WebRTC only through the proxy). The proxy takes `CONNECT host:port` (HTTPS, WebSocket) and plain HTTP requests with an absolute `http://` address. It resolves the name once (at most 10 s), refuses - with 403 unless every address is public (not loopback, private, link-local, CGNAT, documentation, - benchmarking, multicast, reserved or unspecified, also inside IPv4-mapped IPv6), and connects + with 403 unless every address is public (IPv4: not loopback, private, link-local, CGNAT, + documentation, benchmarking, multicast, reserved or unspecified; IPv6: only global unicast + `2000::/3` without `2001::/23`, documentation and 6to4, and an IPv4-mapped address as its IPv4), + and connects only to those addresses, so Chrome can never reach the PC or the local network. Plain HTTP goes on with `Connection: close` and without proxy headers. A request it cannot read is a 400. It logs no host or address, only that it refused one. - Stop: CDP `Browser.close`, then up to 5 s for the process to end, else it and the processes it - started are killed. The services' stop ends the proxy too. + started are killed. Then the proxy stops taking connections, closes every open one (tunnels + included) and the stop waits until they are gone. - Dashboard `browser`: `{state, note}` with `state` off, ready, failed (the note says why) or exited (it ended on its own). - Linux (development only): Ubuntu's AppArmor refuses Chrome's sandbox; `JARVIS_CHROME_NO_SANDBOX=1` diff --git a/app/src/browser.rs b/app/src/browser.rs index fe4f931..a9aa7cd 100644 --- a/app/src/browser.rs +++ b/app/src/browser.rs @@ -34,14 +34,13 @@ pub fn is_public(ip: &IpAddr) -> bool { if let Some(v4) = v6.to_ipv4_mapped() { return is_public(&IpAddr::V4(v4)); } + // Only global unicast 2000::/3 is routed on the internet (this also leaves out site + // local fec0::/10, unique and link local, IPv4-compatible ::a.b.c.d and NAT64). let s = v6.segments(); - !(v6.is_loopback() || v6.is_unspecified() || v6.is_multicast() - || (s[0] & 0xfe00) == 0xfc00 // unique local fc00::/7 - || (s[0] & 0xffc0) == 0xfe80 // link local fe80::/10 - || (s[0] == 0x2001 && s[1] == 0x0db8) // documentation - || (s[0] == 0x0064 && s[1] == 0xff9b) // NAT64 (could reach IPv4 private) - || s[0] == 0x2002 // 6to4 (embeds any IPv4) - || (s[0] == 0x2001 && s[1] == 0)) // Teredo + (s[0] & 0xe000) == 0x2000 + && !(s[0] == 0x2001 && s[1] < 0x0200) // IETF 2001::/23 (Teredo, benchmarking, ...) + && !(s[0] == 0x2001 && s[1] == 0x0db8) // documentation + && s[0] != 0x2002 // 6to4 (embeds any IPv4) } } } @@ -51,31 +50,53 @@ pub fn is_public(ip: &IpAddr) -> bool { /// A loopback HTTP proxy that connects only to addresses `allow` accepts (`is_public` for Chrome). pub struct EgressProxy { pub port: u16, - task: tokio::task::JoinHandle<()>, + shutdown: tokio::sync::watch::Sender, + task: Mutex>>, } impl EgressProxy { pub async fn start(allow: fn(&IpAddr) -> bool) -> std::io::Result { let listener = TcpListener::bind("127.0.0.1:0").await?; let port = listener.local_addr()?.port(); + let (shutdown, rx) = tokio::sync::watch::channel(false); + let mut stopping = rx.clone(); let task = tokio::spawn(async move { - while let Ok((client, _)) = listener.accept().await { - tokio::spawn(async move { - let _ = serve(client, allow).await; - }); + // Every connection is a task of this set, so a stop ends the open tunnels too. + let mut conns = tokio::task::JoinSet::new(); + loop { + tokio::select! { + r = listener.accept() => if let Ok((client, _)) = r { + let mut stop = rx.clone(); + conns.spawn(async move { + tokio::select! { + _ = serve(client, allow) => {} + _ = stop.wait_for(|s| *s) => {} + } + }); + }, + _ = stopping.wait_for(|s| *s) => break, + Some(_) = conns.join_next(), if !conns.is_empty() => {} + } } + drop(listener); + conns.shutdown().await; }); - Ok(Self { port, task }) + Ok(Self { port, shutdown, task: Mutex::new(Some(task)) }) } - pub fn stop(&self) { - self.task.abort(); + /// Stop taking connections, close every open one and wait until they are gone. + pub async fn stop(&self) { + let _ = self.shutdown.send(true); + let task = self.task.lock().unwrap().take(); + if let Some(t) = task { + let _ = t.await; + } } } impl Drop for EgressProxy { fn drop(&mut self) { - self.task.abort(); + let _ = self.shutdown.send(true); } } @@ -283,7 +304,7 @@ impl BotBrowser { } crate::info_log!("browser", "bot browser stopped"); } - self.proxy.stop(); + self.proxy.stop().await; } } @@ -332,11 +353,13 @@ mod tests { #[test] fn only_public_addresses_are_public() { - let public = ["8.8.8.8", "1.1.1.1", "142.250.1.1", "2606:4700:4700::1111", "2a00:1450:4001::1"]; + let public = ["8.8.8.8", "1.1.1.1", "142.250.1.1", "2606:4700:4700::1111", "2a00:1450:4001::1", + "2001:4860:4860::8888", "2400:cb00::1"]; let not = ["127.0.0.1", "10.0.0.1", "172.16.5.4", "192.168.10.9", "169.254.169.254", "100.64.0.1", "0.0.0.0", "0.1.2.3", "192.0.0.8", "192.0.2.1", "198.18.0.1", "203.0.113.5", "224.0.0.1", "240.0.0.1", "255.255.255.255", "::1", "::", "fc00::1", "fd12::1", "fe80::1", "ff02::1", "2001:db8::1", - "::ffff:127.0.0.1", "::ffff:192.168.0.1", "64:ff9b::a00:1", "2002:c0a8:1::1", "2001:0:4136:e378::1"]; + "::ffff:127.0.0.1", "::ffff:192.168.0.1", "64:ff9b::a00:1", "2002:c0a8:1::1", "2001:0:4136:e378::1", + "fec0::1", "fef0::1", "::1.2.3.4", "100::1", "2001:2::1", "2001:10::1", "4000::1", "e000::1"]; for a in public { assert!(is_public(&a.parse().unwrap()), "{a} should be public"); } @@ -423,3 +446,46 @@ mod tests { assert!(chrome_args(Path::new("/p"), 1, true).iter().any(|x| x == "--no-sandbox")); } } + +#[cfg(test)] +mod stop_tests { + use super::*; + + #[tokio::test] + async fn stopping_the_proxy_closes_the_tunnels_it_holds() { + // An echo server the tunnel reaches. + let l = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = l.local_addr().unwrap().port(); + let (closed_tx, closed_rx) = tokio::sync::oneshot::channel(); + tokio::spawn(async move { + let (mut c, _) = l.accept().await.unwrap(); + let mut b = [0u8; 64]; + loop { + match c.read(&mut b).await { + Ok(0) | Err(_) => break, + Ok(n) => { let _ = c.write_all(&b[..n]).await; } + } + } + let _ = closed_tx.send(()); + }); + let p = EgressProxy::start(|ip| ip.is_loopback()).await.unwrap(); + let mut c = TcpStream::connect(("127.0.0.1", p.port)).await.unwrap(); + c.write_all(format!("CONNECT 127.0.0.1:{port} HTTP/1.1\r\n\r\n").as_bytes()).await.unwrap(); + let mut b = vec![0u8; 39]; + c.read_exact(&mut b).await.unwrap(); + assert!(b.starts_with(b"HTTP/1.1 200 Connection Established")); + c.write_all(b"ping").await.unwrap(); + let mut e = [0u8; 4]; + c.read_exact(&mut e).await.unwrap(); + assert_eq!(&e, b"ping"); + + tokio::time::timeout(Duration::from_secs(5), p.stop()).await.expect("stop waits only for the cleanup"); + // The open tunnel is gone on both sides, and no new connection is taken. + let _ = c.write_all(b"after").await; + let mut rest = Vec::new(); + let n = tokio::time::timeout(Duration::from_secs(2), c.read_to_end(&mut rest)).await.expect("closed").unwrap_or(0); + assert_eq!(n, 0, "nothing passes after the stop"); + tokio::time::timeout(Duration::from_secs(2), closed_rx).await.expect("the upstream side closed").unwrap(); + assert!(TcpStream::connect(("127.0.0.1", p.port)).await.is_err()); + } +} diff --git a/src/voice_worker/collect.py b/src/voice_worker/collect.py index d35c486..4891e4b 100644 --- a/src/voice_worker/collect.py +++ b/src/voice_worker/collect.py @@ -54,10 +54,21 @@ def load_config(path) -> Config: def _is_public_ip(ip) -> bool: + if ip.version == 6: + if ip.ipv4_mapped: + return _is_public_ip(ip.ipv4_mapped) + # Only global unicast 2000::/3 is routed on the internet (leaves out site local fec0::/10, + # IPv4-compatible ::a.b.c.d and the rest); 6to4 can embed any IPv4 address. + if ip not in _GLOBAL_UNICAST or ip in _SIX_TO_FOUR: + return False return not (ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved or ip.is_multicast or ip.is_unspecified) +_GLOBAL_UNICAST = ipaddress.ip_network("2000::/3") +_SIX_TO_FOUR = ipaddress.ip_network("2002::/16") + + def _resolve(host: str, port: int, deadline: float) -> list: """getaddrinfo on a daemon thread, waited for only until `deadline` (a late answer is dropped).""" out: dict = {} diff --git a/src/voice_worker/voice_worker.spec.md b/src/voice_worker/voice_worker.spec.md index 1790ffc..cb2c482 100644 --- a/src/voice_worker/voice_worker.spec.md +++ b/src/voice_worker/voice_worker.spec.md @@ -333,7 +333,8 @@ still deleted (below). characters, new by its id or link); `search` keeps each new web search result (title and link) for the words, from DuckDuckGo's lite page, which needs no key (its bot challenge is a failed run, not "no results"). - Every request checks its address like the web search (`web_search.spec.md`): only http(s), and - every address the name resolves to must be public; the connection goes only to the checked + every address the name resolves to must be public (for IPv6 only global unicast `2000::/3`, without + 6to4); the connection goes only to the checked addresses in their order (TLS still checks the name), so a second lookup cannot lead into the local network. Every redirect is checked the same way. At most 1 MB and 15 s in all: the name lookup, connecting, TLS, the headers and the body of every redirect count, and a server that sends diff --git a/tests/test_voice_worker_collect.py b/tests/test_voice_worker_collect.py index 7b1d0e3..e22bb09 100644 --- a/tests/test_voice_worker_collect.py +++ b/tests/test_voice_worker_collect.py @@ -383,3 +383,10 @@ def test_the_fetcher_gives_up_on_a_slow_name_lookup(monkeypatch): took = time.monotonic() - t0 print(f"slow lookup: failed after {took:.2f}s") assert took < 1.5 + + +def test_only_global_unicast_ipv6_counts_as_public(): + for a in ("fec0::1", "::1.2.3.4", "100::1", "2001:2::1", "2002:c0a8::1", "fe80::1", "fd00::1", "::ffff:10.0.0.1"): + assert not collect._is_public_ip(ipaddress.ip_address(a)), a + for a in ("2606:4700:4700::1111", "2001:4860:4860::8888", "8.8.8.8"): + assert collect._is_public_ip(ipaddress.ip_address(a)), a