From 82fe4d8e6528547e928c358688d0cbcc222fa4c3 Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 07:41:39 +0300 Subject: [PATCH] windows: the SSH tunnel's host and user cannot be read as ssh options sshProxy.server.host and .user are ordinary settings the signed-in user may change, and they reached ssh.exe's command line, which the service runs as SYSTEM: a host of -oProxyCommand= would have run that program as SYSTEM (found by the commit security review). The user now goes as -l's value and the host after "--", so neither can be an option; the validator holds the host to a host name or IP address, the user to a login name, and host keys to one line each. Co-Authored-By: Claude Opus 5.5 --- windows/crates/ps-core/src/config/validate.rs | 55 +++++++++++++++++++ windows/crates/ps-service/src/ssh.rs | 15 +++-- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/windows/crates/ps-core/src/config/validate.rs b/windows/crates/ps-core/src/config/validate.rs index 4fd12b8..71b5752 100644 --- a/windows/crates/ps-core/src/config/validate.rs +++ b/windows/crates/ps-core/src/config/validate.rs @@ -72,6 +72,20 @@ fn profile_name_ok(name: &str) -> bool { && chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-') } +/// A host name, an IPv4 address or a bracketless IPv6 one. +fn ssh_host_ok(host: &str) -> bool { + if host.parse::().is_ok() { + return true; + } + !host.is_empty() + && host.len() <= 253 + && host.split('.').all(|l| !l.is_empty() && l.len() <= 63 && !l.starts_with('-') && l.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')) +} + +fn ssh_user_ok(user: &str) -> bool { + !user.is_empty() && !user.starts_with('-') && user.chars().all(|c| c.is_alphanumeric() || matches!(c, '.' | '_' | '-' | '@' | '$')) +} + fn count_some(values: &[bool]) -> usize { values.iter().filter(|v| **v).count() } @@ -411,6 +425,23 @@ fn tunnels(cfg: &Config, v: &mut Issues) { if s.server.user.is_none() { v.error("sshProxy.server.user", "the SSH tunnel needs a login user"); } + // They reach ssh's command line (as SYSTEM on Windows): a host name or address and a + // login name, nothing that could read as an option or carry another line. + if let Some(host) = &s.server.host + && !ssh_host_ok(host) + { + v.error("sshProxy.server.host", "a host name or an IP address"); + } + if let Some(user) = &s.server.user + && !ssh_user_ok(user) + { + v.error("sshProxy.server.user", "a login name: letters, digits, '.', '_', '-' and '@', not starting with '-'"); + } + for (i, key) in s.host_key.iter().enumerate() { + if key.chars().any(char::is_control) { + v.error(format!("sshProxy.hostKey.{i}"), "a host key is one line"); + } + } v.requires(s.as_outbound, proxy_on, "sshProxy.asOutbound", "sshProxy.asOutbound", "proxy.enable = true"); if s.strict_host_key_checking == crate::config::StrictHostKeyChecking::Yes && s.known_hosts_file.is_none() @@ -732,6 +763,30 @@ mod tests { assert_eq!(errors(&Config::default()), Vec::::new()); } + #[test] + fn ssh_host_and_user_cannot_be_options() { + let mut cfg = Config::default(); + cfg.ssh_proxy.enable = true; + cfg.ssh_proxy.strict_host_key_checking = crate::config::StrictHostKeyChecking::No; + for (host, user, bad) in [ + ("vps.example", "me", None), + ("192.0.2.7", "me.you", None), + ("2001:db8::1", "me", None), + ("-oProxyCommand=calc.exe", "me", Some("sshProxy.server.host")), + ("vps.example", "-oProxyCommand=calc.exe", Some("sshProxy.server.user")), + ("vps.example x", "me", Some("sshProxy.server.host")), + ("vps.example", "me\nx", Some("sshProxy.server.user")), + ] { + cfg.ssh_proxy.server.host = Some(host.into()); + cfg.ssh_proxy.server.user = Some(user.into()); + let errs = errors(&cfg); + match bad { + None => assert!(!errs.iter().any(|e| e.starts_with("sshProxy.server")), "{host} {user}: {errs:?}"), + Some(p) => assert!(errs.iter().any(|e| e.starts_with(&format!("{p}:"))), "{host} {user}: {errs:?}"), + } + } + } + #[test] fn what_is_not_ported_is_said() { let quiet: Vec = validate(&Config::default()).into_iter().filter(|i| i.message.contains("not on Windows yet")).map(|i| i.path).collect(); diff --git a/windows/crates/ps-service/src/ssh.rs b/windows/crates/ps-service/src/ssh.rs index c2358f6..17a7f98 100644 --- a/windows/crates/ps-service/src/ssh.rs +++ b/windows/crates/ps-service/src/ssh.rs @@ -126,11 +126,14 @@ pub fn args(cfg: &Config, known_hosts: &Path, identity: Option<&Path>) -> Vec format!("{u}@{host}"), - None => host, - }); + // The user and the host never read as options (ssh runs as SYSTEM; `-oProxyCommand=…` as + // a host would run a program): the user as -l's value, the host after the options' end. + // The validator also holds them to a login name and a host name or address. + if let Some(u) = &s.server.user { + a.extend(["-l".into(), u.clone()]); + } + a.push("--".into()); + a.push(s.server.host.clone().unwrap_or_default()); a } @@ -164,7 +167,7 @@ mod tests { assert!(a.windows(2).any(|w| w == ["-p", "2222"])); assert!(a.windows(2).any(|w| w == ["-i", r"C:\keys\id_ed25519"])); assert!(a.contains(&"UserKnownHostsFile=kh".to_owned())); - assert_eq!(a.last().unwrap(), "me@vps.example"); + assert_eq!(&a[a.len() - 4..], ["-l", "me", "--", "vps.example"]); } #[test]