fix(app): the proxy refuses IPv6 outside global unicast and its stop closes open tunnels
- IPv6 counts as public only in 2000::/3 (without 2001::/23, documentation and 6to4), so site local fec0::/10 and IPv4-compatible addresses are refused; the collector uses the same rule - every proxy connection is a task of one set: a stop ends the open CONNECT tunnels and waits until their sockets are gone Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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`
|
||||
|
||||
@@ -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<bool>,
|
||||
task: Mutex<Option<tokio::task::JoinHandle<()>>>,
|
||||
}
|
||||
|
||||
impl EgressProxy {
|
||||
pub async fn start(allow: fn(&IpAddr) -> bool) -> std::io::Result<Self> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user