From 07e14015ad0defb5bc3701f30f9d0449411e7db3 Mon Sep 17 00:00:00 2001 From: Yao Jingxi <23722032@bjtu.edu.cn> Date: Sat, 21 Nov 2026 11:29:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(remote):=20ssh=20=E5=AD=90=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E5=8F=AA=E4=BF=9D=E7=95=99=E5=AE=83=E8=87=AA=E5=B7=B1?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E7=9A=84=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?= =?UTF-8?q?=EF=BC=88S26=EF=BC=8C#2231=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OpenSSH 会把 SendEnv 模式匹配到的本地变量发给远程主机,很多系统自带 SendEnv LANG LC_*。ssh 会合并命令行和所有配置文件里的 SendEnv 模式,命令行没有办法撤回,所以唯一可靠的做法是让 ssh 子进程只继承白名单里的变量。 TransportChain::restrict_environment:第一跳是 SSH 的链清空继承环境,只保留 ssh 本身需要的变量(HOME、USER、LOGNAME、SHELL、PATH、TMPDIR、SSH_AUTH_SOCK、SSH_SK_PROVIDER、KRB5CCNAME、KRB5_CONFIG、XDG_RUNTIME_DIR 和 Windows 系统变量,Windows 上不区分大小写);本地链和第一跳是容器的链不变,容器引擎只传 -e 列出的变量。远程服务连接(client::session::spawn)、连接探测(workspace probe)和会话连接(workspace sessions)三处在设置 AskPass 变量之前调用,AskPass 变量照常传给 ssh。 测试:白名单与大小写规则、只有第一跳是 SSH 的链才收紧、探测和会话连接两条路径用假 ssh 记录实际收到的环境(不含 CARGO_PKG_NAME 和 LANG,PATH 不变,SSH_ASKPASS_REQUIRE=force)。去掉探测处的调用后该测试失败。 --- rust/crates/remote/src/client/session.rs | 9 +- rust/crates/remote/src/client/transport.rs | 175 ++++++++++++++++++++- rust/crates/workspace/src/probe.rs | 2 + rust/crates/workspace/src/probe/tests.rs | 44 ++++++ rust/crates/workspace/src/sessions.rs | 2 + 5 files changed, 225 insertions(+), 7 deletions(-) diff --git a/rust/crates/remote/src/client/session.rs b/rust/crates/remote/src/client/session.rs index 50c4fb3b..b0bc4748 100644 --- a/rust/crates/remote/src/client/session.rs +++ b/rust/crates/remote/src/client/session.rs @@ -847,13 +847,14 @@ fn spawn(chain: &TransportChain, server_command: &[String]) -> Result, + ignore_case: bool, +) -> Vec<(OsString, OsString)> { + variables + .into_iter() + .filter(|(name, _)| { + name.to_str().is_some_and(|name| { + KEPT_SSH_VARIABLES.iter().any(|kept| { + if ignore_case { + kept.eq_ignore_ascii_case(name) + } else { + *kept == name + } + }) + }) + }) + .collect() +} + fn ssh_command(hop: &SshHop, remote: &[Word]) -> Result, TransportError> { validate_ssh_destination(&hop.destination)?; let mut command: Vec = vec![ @@ -818,4 +913,78 @@ mod tests { )); } } + + fn variables(pairs: &[(&str, &str)]) -> Vec<(OsString, OsString)> { + pairs + .iter() + .map(|(name, value)| (OsString::from(name), OsString::from(value))) + .collect() + } + + #[test] + fn ssh_keeps_only_the_variables_it_needs() { + let inherited = variables(&[ + ("HOME", "/home/dev"), + ("PATH", "/usr/bin:/bin"), + ("SSH_AUTH_SOCK", "/tmp/agent.sock"), + ("LANG", "zh_CN.UTF-8"), + ("LC_ALL", "zh_CN.UTF-8"), + ("HTTPS_PROXY", "http://proxy.example:8080"), + ("AWS_SECRET_ACCESS_KEY", "secret"), + ("GIT_ASKPASS", "/opt/other/askpass"), + ]); + assert_eq!( + kept_ssh_variables(inherited, false), + variables(&[ + ("HOME", "/home/dev"), + ("PATH", "/usr/bin:/bin"), + ("SSH_AUTH_SOCK", "/tmp/agent.sock"), + ]) + ); + } + + #[test] + fn windows_variable_names_match_without_regard_to_case() { + let inherited = variables(&[ + ("Path", r"C:\Windows\System32"), + ("SYSTEMROOT", r"C:\Windows"), + ("Lang", "zh-CN"), + ]); + assert_eq!( + kept_ssh_variables(inherited.clone(), true), + variables(&[ + ("Path", r"C:\Windows\System32"), + ("SYSTEMROOT", r"C:\Windows") + ]) + ); + // Elsewhere names are case-sensitive, so neither spelling is listed. + assert!(kept_ssh_variables(inherited, false).is_empty()); + } + + #[test] + fn only_a_chain_that_starts_with_ssh_restricts_the_environment() { + let keeps_caller_variable = |chain: TransportChain| { + let mut command = Command::new("true"); + command.env("CALLER_VARIABLE", "1"); + chain.restrict_environment(&mut command); + command + .get_envs() + .any(|(name, value)| name == "CALLER_VARIABLE" && value.is_some()) + }; + let ssh = || Hop::Ssh(SshHop::new("devbox")); + let container = || Hop::Container(ContainerHop::new(ContainerEngine::Docker, "api")); + assert!(!keeps_caller_variable(TransportChain::new(vec![ssh()]))); + assert!(!keeps_caller_variable(TransportChain::new(vec![ + ssh(), + container() + ]))); + assert!(keeps_caller_variable(TransportChain::local())); + assert!(keeps_caller_variable(TransportChain::new( + vec![container()] + ))); + assert!(keeps_caller_variable(TransportChain::new(vec![ + container(), + ssh() + ]))); + } } diff --git a/rust/crates/workspace/src/probe.rs b/rust/crates/workspace/src/probe.rs index 78e56019..90440a9d 100644 --- a/rust/crates/workspace/src/probe.rs +++ b/rust/crates/workspace/src/probe.rs @@ -142,6 +142,8 @@ fn probe_with(request: ProbeRequest, ssh_program: Option<&str>) -> Result "$0.environment"`. +#[cfg(unix)] +fn environment_seen_by(ssh: &FakeSsh) -> std::collections::BTreeMap { + std::fs::read_to_string(ssh.program.with_extension("environment")) + .unwrap() + .lines() + .filter_map(|line| line.split_once('=')) + .map(|(name, value)| (name.to_owned(), value.to_owned())) + .collect() +} + +// ssh sends the remote host every local variable a `SendEnv` pattern names, +// so the probe and the connection start it with only the variables it needs +// and the AskPass helper's (S26). +#[cfg(unix)] +#[test] +fn the_ssh_client_starts_with_only_the_variables_it_needs() { + // Cargo sets its package variables for every test process; none is kept. + assert!(std::env::var_os("CARGO_PKG_NAME").is_some()); + let body = "env > \"$0.environment\"\n\ + echo 'dev@devbox: Permission denied (password).' >&2\nexit 255"; + let probing = FakeSsh::new("remote-probe-environment", body); + probe_with(probe_request(), Some(probing.path())).unwrap(); + let connecting = FakeSsh::new("remote-connect-environment", body); + sessions::connect_with(connect_request(json!([])), Some(connecting.path())).unwrap(); + for ssh in [&probing, &connecting] { + let seen = environment_seen_by(ssh); + // Names only: values may be secrets of the machine running the test. + let names = seen.keys().collect::>(); + assert!(!seen.contains_key("CARGO_PKG_NAME"), "{names:?}"); + assert!(!seen.contains_key("LANG"), "{names:?}"); + assert_eq!( + seen.get("PATH").map(String::as_str), + std::env::var("PATH").ok().as_deref() + ); + assert_eq!( + seen.get("SSH_ASKPASS_REQUIRE").map(String::as_str), + Some("force"), + "{names:?}" + ); + } +} + // The connection log keeps one category per failed status, so a log line // tells authentication, host key and network failures apart. #[test] diff --git a/rust/crates/workspace/src/sessions.rs b/rust/crates/workspace/src/sessions.rs index 20338cf1..5bdf1d5a 100644 --- a/rust/crates/workspace/src/sessions.rs +++ b/rust/crates/workspace/src/sessions.rs @@ -317,6 +317,8 @@ fn connect_logged( .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()); + // Before the AskPass variables, which must reach ssh. + chain.restrict_environment(&mut process); let mut authentication = lithe_git_host::authentication::Session::new().map_err(|error| { CoreError::new( ErrorCode::ProcessStartFailed, From 500157e0ea91c21eca39d73f019af536a55d93e2 Mon Sep 17 00:00:00 2001 From: Yao Jingxi <23722032@bjtu.edu.cn> Date: Sat, 21 Nov 2026 11:30:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?docs(remote):=20=E5=8D=8F=E8=AE=AE=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E5=86=99=E6=98=8E=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=20ssh=20=E6=97=B6=E5=8F=AA=E4=BF=9D=E7=95=99=20OpenSS?= =?UTF-8?q?H=20=E9=9C=80=E8=A6=81=E7=9A=84=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=EF=BC=88S26=EF=BC=8C#2231=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/contracts/remote-workspace-protocol-v1.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/contracts/remote-workspace-protocol-v1.md b/rust/contracts/remote-workspace-protocol-v1.md index 02008d0c..c6e260bc 100644 --- a/rust/contracts/remote-workspace-protocol-v1.md +++ b/rust/contracts/remote-workspace-protocol-v1.md @@ -30,6 +30,16 @@ startup deadline. The lock is released when its owning descriptor closes; its inode is retained so concurrent waiters cannot lock different files. This keeps a redundant launcher from restarting an explicitly closed session. +When a chain starts with an SSH hop, the reference client starts `ssh` with +only the local variables OpenSSH itself needs (home, user, shell, search +path, temporary directory, agent socket, Kerberos and the Windows system +variables), followed by its own AskPass variables. ssh sends the remote host +every variable a `SendEnv` pattern names, many systems ship +`SendEnv LANG LC_*`, and no command-line option can withdraw a pattern, so +this is the only way to keep local locale, proxy and credential variables +off the host; the server runs with the remote user's login environment. +Other clients should do the same. + ## Stream 1. The server writes the preamble `\nLITHE-REMOTE-PROTOCOL/1\n` once per