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 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,