From 130c020151129c5d2dea00adb9a6a4d15eb86119 Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 26 Oct 2026 22:57:06 +0300 Subject: [PATCH] windows: refuse loopback through the proxy; Clash API always has a secret Per-app rules naming an outbound that is not running are left out with a warning instead of failing the start, and failover renders as sing-box's own urltest until the service drives it. Co-Authored-By: Claude Opus 5.5 --- windows/crates/ps-core/src/render/routing.rs | 27 ++++++++++++------- windows/crates/ps-core/src/render/sing_box.rs | 9 ++++--- windows/crates/ps-core/tests/render.rs | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/windows/crates/ps-core/src/render/routing.rs b/windows/crates/ps-core/src/render/routing.rs index ef19ae5..c923582 100644 --- a/windows/crates/ps-core/src/render/routing.rs +++ b/windows/crates/ps-core/src/render/routing.rs @@ -139,15 +139,21 @@ pub(super) fn direct_match(input: &Input) -> RouteMatch { /// The rules that pick an outbound by program, and the loopback hops' guard: kept in every /// route mode, ahead of everything that picks by destination, as upstream's pin rules are. -fn always_rules(input: &Input) -> Vec { +fn always_rules(input: &Input, known: &dyn Fn(&str) -> bool, warnings: &mut Vec) -> Vec { let cfg = input.cfg; let mut rules = Vec::new(); if !input.env.direct_processes.is_empty() { rules.push(json!({ "process_path": input.env.direct_processes, "outbound": "direct" })); } if cfg.per_app_routing.enable { - for (i, prof) in cfg.per_app_routing.profiles.iter().enumerate() { - let _ = i; + for prof in &cfg.per_app_routing.profiles { + if !derived::BUILTIN_TAGS.contains(&prof.outbound.as_str()) && !known(&prof.outbound) { + warnings.push(format!( + "per-app profile '{}': no outbound or group named '{}' is running; its programs follow the routing", + prof.name, prof.outbound + )); + continue; + } let inbound = per_app_inbound(&prof.name); rules.push(target(json!({ "inbound": [inbound] }), &prof.outbound)); if !prof.process_names.is_empty() { @@ -187,12 +193,15 @@ pub(super) fn route(input: &Input, hop_ports: &[u16], known: &dyn Fn(&str) -> bo if onion { common.push(json!({ "domain_suffix": ["onion"], "outbound": derived::TOR_OUTBOUND_TAG })); } - // A client asking for a loopback hop would have sing-box dial the helpers' listeners - // for it, past the routing. - if !hop_ports.is_empty() { - common.push(json!({ "ip_cidr": LOOPBACK, "port": hop_ports, "action": "reject" })); - } - common.extend(always_rules(input)); + // Nothing reaches this host's loopback through the proxy: the helpers' listeners, the + // Clash API and whatever else listens there would be a client's for the asking, a LAN + // client's too when the listener is exposed. Upstream guards only the hops (its firewall + // keeps the rest from other users); Windows has no such firewall here, so every port. A + // name that resolves to loopback on its own is not caught: keep the listener on loopback. + let _ = hop_ports; + common.push(json!({ "ip_cidr": LOOPBACK, "action": "reject" })); + common.push(json!({ "domain": ["localhost"], "domain_suffix": [".localhost"], "action": "reject" })); + common.extend(always_rules(input, known, warnings)); // The custom rules: each outbound's own routing, then routing.rules, at their priority. let mut sections = Vec::new(); diff --git a/windows/crates/ps-core/src/render/sing_box.rs b/windows/crates/ps-core/src/render/sing_box.rs index 9b57a75..a2887cf 100644 --- a/windows/crates/ps-core/src/render/sing_box.rs +++ b/windows/crates/ps-core/src/render/sing_box.rs @@ -174,10 +174,11 @@ pub(super) fn config(input: &Input, asm: &Assembly, warnings: &mut Vec) route_obj["auto_detect_interface"] = true.into(); } - let mut clash = json!({ "external_controller": format!("127.0.0.1:{}", p.sing_box.clash_api_port) }); - if let Some(secret) = &env.clash_secret { - clash["secret"] = secret.clone().into(); - } + // Any local program could switch selectors or read the connections otherwise. + let Some(secret) = env.clash_secret.as_ref().filter(|s| !s.is_empty()) else { + return Err(RenderError("the Clash API needs a secret".into())); + }; + let clash = json!({ "external_controller": format!("127.0.0.1:{}", p.sing_box.clash_api_port), "secret": secret }); let mut experimental = json!({ "clash_api": clash }); if fake_ip || onion_fake { // Fake addresses handed out survive a restart, so apps still holding one keep working. diff --git a/windows/crates/ps-core/tests/render.rs b/windows/crates/ps-core/tests/render.rs index 634cd6a..e30c8f8 100644 --- a/windows/crates/ps-core/tests/render.rs +++ b/windows/crates/ps-core/tests/render.rs @@ -34,7 +34,7 @@ fn env(dir: &PathBuf) -> Env { hop_login: Some(("hop".into(), "hop-pass".into())), warp_profiles: warp, direct_processes: vec![r"C:\ProgramData\ProxySuite\bin\tor\tor.exe".into()], - groups_watched: true, + groups_watched: false, sidecar_secret: "0b1c2d3e-4f50-6172-8394-a5b6c7d8e9f0".into(), windows: true, ..Default::default()