From d9d52c49b648d546785d6f18e11ed7094675f382 Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 08:06:42 +0300 Subject: [PATCH] windows: no unquoted value can add a directive to Tor's torrc proxy.listener.address went into the torrc unquoted (Socks5Proxy, with upstream = "proxy"): a newline in it, set by a user who may change the configuration, would add a directive, ClientTransportPlugin among them, which runs a program as SYSTEM (found by the commit security review). The address is written from its parsed IpAddr now; a bridge line with a control character in it is refused; and the validator holds every listen address (the proxy's, the SSH tunnel's, tg-ws-proxy's) to an IP address, which they all are. Co-Authored-By: Claude Opus 5.5 --- windows/crates/ps-core/src/config/validate.rs | 22 +++++++++++++ windows/crates/ps-service/src/tor.rs | 32 +++++++++++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/windows/crates/ps-core/src/config/validate.rs b/windows/crates/ps-core/src/config/validate.rs index f0a3836..ff20a13 100644 --- a/windows/crates/ps-core/src/config/validate.rs +++ b/windows/crates/ps-core/src/config/validate.rs @@ -147,6 +147,17 @@ fn not_ported(cfg: &Config, v: &mut Issues) { fn proxy(cfg: &Config, v: &mut Issues) { let p = &cfg.proxy; let sing_box = p.backend.runs_sing_box(); + // Listen addresses reach other programs' configs and command lines (Tor's torrc, ssh's -D, + // tg-ws-proxy's --host): an IP address, nothing else. + for (path, address) in [ + ("proxy.listener.address", &p.listener.address), + ("sshProxy.listener.address", &cfg.ssh_proxy.listener.address), + ("tgWsProxy.listener.address", &cfg.tg_ws_proxy.listener.address), + ] { + if address.parse::().is_err() { + v.error(path, format!("'{address}' is not an IP address")); + } + } let mut seen = BTreeSet::new(); for (i, ob) in p.outbounds.iter().enumerate() { @@ -763,6 +774,17 @@ mod tests { assert_eq!(errors(&Config::default()), Vec::::new()); } + #[test] + fn listen_addresses_are_ip_addresses() { + let mut cfg = Config::default(); + assert!(!errors(&cfg).iter().any(|e| e.contains("listener.address"))); + cfg.proxy.listener.address = "127.0.0.1\nClientTransportPlugin x exec calc.exe".into(); + cfg.tg_ws_proxy.listener.address = "::".into(); + let errs = errors(&cfg); + assert!(errs.iter().any(|e| e.starts_with("proxy.listener.address:")), "{errs:?}"); + assert!(!errs.iter().any(|e| e.starts_with("tgWsProxy.listener.address:")), "{errs:?}"); + } + #[test] fn ssh_host_and_user_cannot_be_options() { let mut cfg = Config::default(); diff --git a/windows/crates/ps-service/src/tor.rs b/windows/crates/ps-service/src/tor.rs index 2e8d22b..0cd8790 100644 --- a/windows/crates/ps-service/src/tor.rs +++ b/windows/crates/ps-service/src/tor.rs @@ -32,13 +32,18 @@ fn quote(s: &str) -> String { } /// Bridge lines as upstream takes them: trimmed, blanks and comments gone, "Bridge " in front. -fn bridge_lines(text: &str) -> Vec { +/// One with a control character left in it (a lone carriage return) is refused: it could end +/// the line in Tor's reading and start a directive. +fn bridge_lines(text: &str) -> Result> { text.lines() .map(str::trim) .filter(|l| !l.is_empty() && !l.starts_with('#')) .map(|l| { + if l.chars().any(char::is_control) { + anyhow::bail!("a bridge line has a control character in it"); + } let bare = l.strip_prefix("Bridge").filter(|r| r.starts_with(char::is_whitespace)).map(str::trim_start).unwrap_or(l); - format!("Bridge {bare}") + Ok(format!("Bridge {bare}")) }) .collect() } @@ -64,9 +69,12 @@ pub fn torrc(cfg: &Config, bundle: Option<&Path>, data: &Path, read: impl Fn(&st } c.push(if t.as_outbound { format!("SocksPort 127.0.0.1:{}", t.socks_port) } else { "SocksPort 0".into() }); - let mut bridges: Vec = t.bridges.lines.iter().flat_map(|l| bridge_lines(l)).collect(); + let mut bridges: Vec = Vec::new(); + for l in &t.bridges.lines { + bridges.extend(bridge_lines(l)?); + } if let Some(f) = &t.bridges.file { - bridges.extend(bridge_lines(&read(f).with_context(|| format!("reading tor.bridges.file {f}"))?)); + bridges.extend(bridge_lines(&read(f).with_context(|| format!("reading tor.bridges.file {f}"))?)?); } if !bridges.is_empty() { c.push("UseBridges 1".into()); @@ -81,8 +89,13 @@ pub fn torrc(cfg: &Config, bundle: Option<&Path>, data: &Path, read: impl Fn(&st } if t.upstream == Upstream::Proxy { let l = &cfg.proxy.listener; - let host = if l.address == "0.0.0.0" || l.address == "::" { "127.0.0.1".to_owned() } else { l.address.clone() }; - let host = if host.contains(':') { format!("[{host}]") } else { host }; + // Written from the parsed address, never the text: it is not quoted in the torrc. + let ip: std::net::IpAddr = l.address.parse().context("proxy.listener.address is not an IP address")?; + let host = match ip { + ip if ip.is_unspecified() => "127.0.0.1".to_owned(), + std::net::IpAddr::V6(v6) => format!("[{v6}]"), + v4 => v4.to_string(), + }; c.push(format!("Socks5Proxy {host}:{}", l.port)); if let Some(user) = &l.auth.username { let password = match (&l.auth.password, &l.auth.password_file) { @@ -168,5 +181,12 @@ mod tests { let t = torrc(&c, None, Path::new("D"), |_| Ok(String::new())).unwrap(); assert!(t.contains("Socks5ProxyUsername \"u\\x0aClientTransportPlugin x exec calc.exe\"\n"), "{t}"); assert!(!t.lines().any(|l| l.starts_with("ClientTransportPlugin x"))); + // Nor through the address, which is not quoted, or a bridge line. + c.proxy.listener.auth.username = None; + c.proxy.listener.address = "127.0.0.1\nClientTransportPlugin x exec calc.exe".into(); + assert!(torrc(&c, None, Path::new("D"), |_| Ok(String::new())).is_err()); + c.proxy.listener.address = "127.0.0.1".into(); + c.tor.bridges.lines = vec!["obfs4 192.0.2.1:443 FP\rClientTransportPlugin x exec calc.exe".into()]; + assert!(torrc(&c, None, Path::new("D"), |_| Ok(String::new())).is_err()); } }