From 956371147e0dad9161be0a8552a646efa1b33cb8 Mon Sep 17 00:00:00 2001 From: Adrien Langou Date: Wed, 18 Nov 2026 12:34:15 +0100 Subject: [PATCH 1/6] feat(cli): verify sandbox SSH host keys The CLI, the TUI, and generated SSH config entries accepted any SSH host key. They now run ssh with StrictHostKeyChecking=yes against a per-sandbox known-hosts file. The ssh-proxy relay reads the host key from the server's key exchange reply, pins it only when its SHA-256 fingerprint equals the one the gateway published, and withholds the reply otherwise. A sandbox with no published fingerprint is refused unless OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY=1 is set. SSH config entries generated by earlier releases are refused with the command that regenerates them. Signed-off-by: Adrien Langou --- Cargo.lock | 1 + crates/openshell-cli/Cargo.toml | 1 + crates/openshell-cli/src/main.rs | 67 +- crates/openshell-cli/src/ssh.rs | 429 ++++- crates/openshell-cli/src/tls.rs | 17 +- .../sandbox_create_lifecycle_integration.rs | 80 + .../tests/ssh_proxy_host_key_integration.rs | 739 ++++++++ .../tests/ssh_proxy_shutdown_integration.rs | 4 + crates/openshell-core/src/forward.rs | 215 ++- crates/openshell-core/src/lib.rs | 1 + crates/openshell-core/src/paths.rs | 16 + crates/openshell-core/src/ssh_host_key.rs | 1618 +++++++++++++++++ crates/openshell-tui/src/lib.rs | 118 +- docs/how-it-works/sandboxes/overview.mdx | 19 +- examples/vscode-remote-sandbox.md | 14 +- scripts/smoke-test-network-policy.sh | 2 - skills/debug-openshell-cluster/SKILL.md | 1 + skills/openshell-cli/SKILL.md | 6 + 18 files changed, 3163 insertions(+), 185 deletions(-) create mode 100644 crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs create mode 100644 crates/openshell-core/src/ssh_host_key.rs diff --git a/Cargo.lock b/Cargo.lock index 458b6000aa..47659b9289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4379,6 +4379,7 @@ dependencies = [ "prost-types", "rcgen", "reqwest 0.12.28", + "russh", "rustls", "serde", "serde_json", diff --git a/crates/openshell-cli/Cargo.toml b/crates/openshell-cli/Cargo.toml index 18c0b84099..c0ab40524a 100644 --- a/crates/openshell-cli/Cargo.toml +++ b/crates/openshell-cli/Cargo.toml @@ -95,6 +95,7 @@ futures = { workspace = true } openshell-server = { path = "../openshell-server", features = ["test-support"] } rcgen = { workspace = true } reqwest = { workspace = true } +russh = "0.65.0" serde_json = { workspace = true } temp-env = "0.3" tokio-stream = { workspace = true } diff --git a/crates/openshell-cli/src/main.rs b/crates/openshell-cli/src/main.rs index 8bcf8f00f3..ad36490cd1 100644 --- a/crates/openshell-cli/src/main.rs +++ b/crates/openshell-cli/src/main.rs @@ -21,6 +21,7 @@ use openshell_cli::completers; use openshell_cli::run; use openshell_cli::tls::TlsOptions; use openshell_core::proto::{GpuResourceRequirements, ServiceAuthorizationMode}; +use openshell_core::ssh_host_key::{HostKeyCheck, HostKeyExpectation, KnownHostsPin}; /// Resolved gateway context: name + gateway endpoint. struct GatewayContext { @@ -664,10 +665,12 @@ enum Commands { /// Two mutually exclusive modes: /// /// **Token mode** (used internally by `sandbox connect`): - /// `openshell ssh-proxy --gateway --sandbox --token ` + /// `openshell ssh-proxy --gateway --sandbox --token + /// --host-key --known-hosts ` /// /// **Name mode** (for use in `~/.ssh/config`): - /// `openshell ssh-proxy --gateway --name ` + /// `openshell ssh-proxy --gateway --name + /// --known-hosts ` #[command(hide = true, help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")] SshProxy { /// Gateway URL (e.g., ). @@ -694,6 +697,17 @@ enum Commands { /// Sandbox name. Used in name mode. #[arg(long)] name: Option, + + /// SSH host key the sandbox must present: the fingerprint the gateway + /// published, or `unverified`. Required in token mode; name mode asks + /// the gateway instead. + #[arg(long, requires = "known_hosts", conflicts_with = "name")] + host_key: Option, + + /// Known-hosts file in which the verified host key is pinned for `ssh`. + /// Required in both modes. + #[arg(long)] + known_hosts: Option, }, } @@ -3723,7 +3737,7 @@ async fn run_async() -> Result<()> { } SandboxCommands::SshConfig { name } => { let name = resolve_sandbox_name(name, &ctx.name, &cli.workspace)?; - run::print_ssh_config(&ctx.name, &name, &cli.workspace); + run::print_ssh_config(&ctx.name, &name, &cli.workspace)?; } SandboxCommands::Provider(command) => match command { SandboxProviderCommands::List { @@ -4179,10 +4193,22 @@ async fn run_async() -> Result<()> { server, gateway_name, name, + host_key, + known_hosts, }) => { match (gateway, sandbox, token, server, gateway_name, name) { // Token mode (existing behavior): pre-created session credentials. (Some(gw), Some(sandbox), Some(tok), _, gateway_name_opt, _) => { + let (Some(expectation), Some(known_hosts)) = (host_key, known_hosts) else { + eprintln!( + "openshell: this SSH connection was started by an OpenShell client that predates host-key verification and is refused.\n\ + Restart that client (the openshell command or openshell term) and connect again." + ); + std::process::exit(1); + }; + let host_key = KnownHostsPin::at(known_hosts, &sandbox, &cli.workspace) + .and_then(|pin| HostKeyCheck::for_relay(expectation, pin)) + .map_err(|err| miette::miette!("{err}"))?; let mut effective_tls = match gateway_name_opt { Some(ref g) => tls.with_gateway_name(g), None => tls, @@ -4190,8 +4216,15 @@ async fn run_async() -> Result<()> { if let Some(ref g) = gateway_name_opt { apply_auth(&mut effective_tls, g)?; } - run::sandbox_ssh_proxy(&gw, &sandbox, &cli.workspace, &tok, &effective_tls) - .await?; + run::sandbox_ssh_proxy( + &gw, + &sandbox, + &cli.workspace, + &tok, + &effective_tls, + host_key, + ) + .await?; } // Name mode with --gateway-name: resolve endpoint from metadata. (_, _, _, server_override, Some(g), Some(n)) => { @@ -4209,11 +4242,27 @@ async fn run_async() -> Result<()> { }; let mut tls = tls.with_gateway_name(&g); apply_auth(&mut tls, &g)?; - run::sandbox_ssh_proxy_by_name(&endpoint, &n, &tls, &cli.workspace).await?; + run::sandbox_ssh_proxy_by_name( + &endpoint, + Some(&g), + &n, + &tls, + &cli.workspace, + known_hosts, + ) + .await?; } // Legacy name mode with --server only (no --gateway-name). (_, _, _, Some(srv), None, Some(n)) => { - run::sandbox_ssh_proxy_by_name(&srv, &n, &tls, &cli.workspace).await?; + run::sandbox_ssh_proxy_by_name( + &srv, + None, + &n, + &tls, + &cli.workspace, + known_hosts, + ) + .await?; } _ => { return Err(miette::miette!( @@ -5131,6 +5180,10 @@ mod tests { "tok-abc", "--gateway-name", "my-gateway", + "--host-key", + "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA", + "--known-hosts", + "/home/u/.config/openshell/known_hosts/my-gateway/openshell-my-box.team-a", ]) .expect("TUI proxy command flags must be accepted by the CLI"); assert_eq!(cli.workspace, "team-a"); diff --git a/crates/openshell-cli/src/ssh.rs b/crates/openshell-cli/src/ssh.rs index 5eb6738ddd..289de94946 100644 --- a/crates/openshell-cli/src/ssh.rs +++ b/crates/openshell-cli/src/ssh.rs @@ -14,12 +14,17 @@ use miette::{IntoDiagnostic, Report, Result, WrapErr}; use nix::sys::signal::Signal; use openshell_core::driver_mounts; use openshell_core::forward::{ - ForwardSpec, build_proxy_command, format_gateway_url, resolve_ssh_gateway, sandbox_ssh_args, - shell_escape, validate_ssh_session_response, write_forward_pid, + ForwardSpec, SandboxProxyCommand, format_gateway_url, resolve_ssh_gateway, + sandbox_proxy_command, sandbox_ssh_args, shell_escape, validate_ssh_session_response, + write_forward_pid, }; use openshell_core::proto::{ - CreateSshSessionRequest, GetSandboxRequest, SandboxPhase, SshRelayTarget, TcpForwardFrame, - TcpForwardInit, tcp_forward_init, + CreateSshSessionRequest, CreateSshSessionResponse, GetSandboxRequest, SandboxPhase, + SshRelayTarget, TcpForwardFrame, TcpForwardInit, tcp_forward_init, +}; +use openshell_core::ssh_host_key::{ + HostKeyCheck, HostKeyExpectation, HostKeyGate, KnownHostsPin, SandboxSshTarget, + UnverifiedPolicy, host_key_alias, }; use openshell_core::upload::{ DestinationKind as UploadDestinationKind, ExpectedDestination, UploadPlan, @@ -104,22 +109,25 @@ impl Editor { } struct SshSessionConfig { - proxy_command: String, + proxy_command: SandboxProxyCommand, sandbox_id: String, - sandbox_name: String, - gateway_url: String, - token: String, main_terminal: bool, +} + +struct CreatedSshSession { + response: CreateSshSessionResponse, + gateway_url: String, replica: Option, + main_terminal: bool, } -async fn ssh_session_config( +async fn create_ssh_session( server: &str, name: &str, tls: &TlsOptions, workspace: &str, terminal_relay_registration_timeout: Option, -) -> Result { +) -> Result { let mut client = grpc_client(server, tls).await?; // Resolve the sandbox and retain its ID for local lifecycle tracking. @@ -165,11 +173,6 @@ async fn ssh_session_config( validate_ssh_session_response(&session) .map_err(|err| miette::miette!("gateway returned invalid SSH session response: {err}"))?; - let exe = std::env::current_exe() - .into_diagnostic() - .wrap_err("failed to resolve OpenShell executable")?; - let exe_command = exe.to_string_lossy().into_owned(); - // When using Cloudflare bearer auth, the SSH CONNECT must go through the // external tunnel endpoint (the cluster URL), not the server's internal // scheme/host/port which may be plaintext HTTP on 127.0.0.1. @@ -189,30 +192,64 @@ async fn ssh_session_config( ); format_gateway_url(&gateway_scheme, &gateway_host, gateway_port) }; + + Ok(CreatedSshSession { + response: session, + gateway_url, + replica, + main_terminal: sandbox.spec.as_ref().is_none_or(|spec| spec.tty), + }) +} + +async fn ssh_session_config( + server: &str, + name: &str, + tls: &TlsOptions, + workspace: &str, + terminal_relay_registration_timeout: Option, +) -> Result { + let session = create_ssh_session( + server, + name, + tls, + workspace, + terminal_relay_registration_timeout, + ) + .await?; + + let exe = std::env::current_exe() + .into_diagnostic() + .wrap_err("failed to resolve OpenShell executable")?; let gateway_name = tls .gateway_name() .ok_or_else(|| miette::miette!("gateway name is required to build SSH proxy command"))?; - let proxy_command = build_proxy_command( - &exe_command, - &gateway_url, - name, + let target = SandboxSshTarget { + gateway: gateway_name, workspace, - &session.token, - gateway_name, + sandbox: name, + }; + let host_key = + HostKeyCheck::for_session(&session.response, &target, UnverifiedPolicy::from_env()) + .into_diagnostic()?; + if let Some(warning) = host_key.unverified_warning(name) { + eprintln!("{warning}"); + } + let proxy_command = sandbox_proxy_command( + &exe.to_string_lossy(), + &session.gateway_url, + &target, + &session.response.token, + host_key, ); Ok(SshSessionConfig { proxy_command, - sandbox_id: session.sandbox_id.clone(), - sandbox_name: name.to_string(), - gateway_url, - token: session.token, - main_terminal: sandbox.spec.as_ref().is_none_or(|spec| spec.tty), - replica, + sandbox_id: session.response.sandbox_id, + main_terminal: session.main_terminal, }) } -fn ssh_base_command(proxy_command: &str) -> Command { +fn ssh_base_command(proxy_command: &SandboxProxyCommand) -> Command { // An explicit environment override wins over the CLI verbosity default. let ssh_log_level = std::env::var("OPENSHELL_SSH_LOG_LEVEL").unwrap_or_else(|_| { DEFAULT_SSH_LOG_LEVEL @@ -717,7 +754,7 @@ pub async fn sandbox_connect_editor( let session = ssh_session_config(server, name, tls, workspace, None).await?; let workspace_root = discover_workspace_root(&session).await?; - let host_alias = host_alias(name, workspace); + let host_alias = host_key_alias(name, workspace); install_ssh_config(gateway, name, workspace)?; launch_editor(editor, &host_alias, &workspace_root)?; eprintln!( @@ -820,7 +857,11 @@ pub async fn sandbox_forward( /// A forward must stay owned by the process we spawn so it can be reaped or /// tracked by PID. User SSH config may otherwise fork it into the background. -fn ssh_forward_command(proxy_command: &str, sandbox_id: &str, spec: &ForwardSpec) -> Command { +fn ssh_forward_command( + proxy_command: &SandboxProxyCommand, + sandbox_id: &str, + spec: &ForwardSpec, +) -> Command { let mut command = ssh_base_command(proxy_command); command .arg("-o") @@ -1314,7 +1355,7 @@ async fn ssh_tar_upload( /// Upload EOF marks a complete archive and the SSH exit status reports /// extraction, so the session must stay in the process we spawn. User SSH /// config may otherwise fork it into the background. -fn ssh_upload_command(proxy_command: &str) -> Command { +fn ssh_upload_command(proxy_command: &SandboxProxyCommand) -> Command { let mut command = ssh_base_command(proxy_command); command .arg("-o") @@ -1962,6 +2003,7 @@ pub async fn sandbox_ssh_proxy( workspace: &str, token: &str, tls: &TlsOptions, + host_key: HostKeyCheck, ) -> Result<()> { let server = grpc_server_from_ssh_gateway_url(gateway_url)?; let mut client = grpc_client(&server, tls).await?; @@ -1976,7 +2018,15 @@ pub async fn sandbox_ssh_proxy( .await .ok() .and_then(|response| owner_replica(&response)); - relay_ssh_proxy(client, sandbox_name, workspace, token, replica.as_ref()).await + relay_ssh_proxy( + client, + sandbox_name, + workspace, + token, + replica.as_ref(), + host_key.into_gate(), + ) + .await } async fn relay_ssh_proxy( @@ -1985,6 +2035,7 @@ async fn relay_ssh_proxy( workspace: &str, token: &str, replica: Option<&AsciiMetadataValue>, + mut gate: HostKeyGate, ) -> Result<()> { let init = TcpForwardFrame { payload: Some(openshell_core::proto::tcp_forward_frame::Payload::Init( @@ -2052,6 +2103,12 @@ async fn relay_ssh_proxy( else { continue; }; + let data = gate.push(&data).unwrap_or_else(|refusal| { + // Printed rather than returned: ssh passes this text to the user + // as is, and a returned error would be reformatted as a report. + eprintln!("openshell: {refusal}"); + std::process::exit(1) + }); if data.is_empty() { continue; } @@ -2112,43 +2169,91 @@ fn grpc_server_from_ssh_gateway_url(gateway_url: &str) -> Result { /// and sandbox name instead of pre-created gateway/token credentials. It is /// suitable for use as an SSH `ProxyCommand` in `~/.ssh/config` because it /// creates a fresh session on every invocation. +/// +/// `known_hosts` is the pin file the config entry also names as its +/// `UserKnownHostsFile`. An entry without it tells `ssh` to accept any host +/// key, so it is refused. `gateway_name` is the gateway the entry names, when +/// it names one. pub async fn sandbox_ssh_proxy_by_name( server: &str, + gateway_name: Option<&str>, name: &str, tls: &TlsOptions, workspace: &str, + known_hosts: Option, ) -> Result<()> { - let session = ssh_session_config(server, name, tls, workspace, None).await?; + let Some(known_hosts) = known_hosts else { + eprintln!( + "{}", + stale_ssh_config_refusal(gateway_name, workspace, name) + ); + std::process::exit(1); + }; + let session = create_ssh_session(server, name, tls, workspace, None).await?; + let expectation = HostKeyExpectation::from_published( + &session.response.host_key_fingerprint, + name, + UnverifiedPolicy::from_env(), + ) + .into_diagnostic()?; + let pin = KnownHostsPin::at(known_hosts, name, workspace).into_diagnostic()?; + let host_key = HostKeyCheck::for_relay(expectation, pin).into_diagnostic()?; + if let Some(warning) = host_key.unverified_warning(name) { + eprintln!("{warning}"); + } let server = grpc_server_from_ssh_gateway_url(&session.gateway_url)?; let client = grpc_client(&server, tls).await?; relay_ssh_proxy( client, - &session.sandbox_name, + name, workspace, - &session.token, + &session.response.token, session.replica.as_ref(), + host_key.into_gate(), ) .await } -fn host_alias(name: &str, workspace: &str) -> String { - format!("openshell-{name}.{workspace}") +fn stale_ssh_config_refusal(gateway: Option<&str>, workspace: &str, sandbox: &str) -> String { + let gateway = gateway.map_or_else(String::new, |gateway| { + format!(" -g {}", shell_escape(gateway)) + }); + let openshell = format!("openshell{gateway} --workspace {}", shell_escape(workspace)); + let name = shell_escape(sandbox); + format!( + "openshell: this SSH config entry for sandbox '{sandbox}' (workspace '{workspace}') predates host-key verification and is refused.\n\ + Regenerate it: {openshell} sandbox connect {name} --editor vscode\n\ + Or replace the entry with the output of: {openshell} sandbox ssh-config {name}" + ) } -fn render_ssh_config(gateway: &str, name: &str, workspace: &str) -> String { +fn render_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result { let exe = std::env::current_exe().expect("failed to resolve OpenShell executable"); let exe = shell_escape(&exe.to_string_lossy()); + let pin = KnownHostsPin::for_target(&SandboxSshTarget { + gateway, + workspace, + sandbox: name, + }) + .into_diagnostic()?; let proxy_cmd = format!( - "{exe} ssh-proxy --gateway-name {} --name {} --workspace {}", + "{exe} ssh-proxy --gateway-name {} --name {} --workspace {} --known-hosts {}", shell_escape(gateway), shell_escape(name), shell_escape(workspace), + shell_escape(&pin.path().to_string_lossy()), ); - let host_alias = host_alias(name, workspace); - format!( - "Host {host_alias}\n User sandbox\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n GlobalKnownHostsFile /dev/null\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {proxy_cmd}\n" - ) + let host_alias = host_key_alias(name, workspace); + let options = pin + .ssh_options() + .into_iter() + .map(|(key, value)| format!(" {key} {value}")) + .collect::>() + .join("\n"); + Ok(format!( + "Host {host_alias}\n User sandbox\n{options}\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {proxy_cmd}\n" + )) } fn openshell_ssh_config_path() -> Result { @@ -2272,6 +2377,7 @@ fn upsert_host_block(contents: &str, alias: &str, block: &str) -> String { } pub fn install_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result { + let block = render_ssh_config(gateway, name, workspace)?; let managed_config = openshell_ssh_config_path()?; let main_config = user_ssh_config_path()?; ensure_openshell_include(&main_config, &managed_config)?; @@ -2280,8 +2386,7 @@ pub fn install_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result< openshell_core::paths::create_dir_restricted(parent)?; } - let alias = host_alias(name, workspace); - let block = render_ssh_config(gateway, name, workspace); + let alias = host_key_alias(name, workspace); let contents = fs::read_to_string(&managed_config).unwrap_or_default(); let updated = upsert_host_block(&contents, &alias, &block); fs::write(&managed_config, updated) @@ -2334,8 +2439,9 @@ fn launch_editor_command( /// The `ProxyCommand` uses `--gateway-name` so that `ssh-proxy` resolves the /// gateway endpoint and TLS certificates from the gateway metadata directory /// (`~/.config/openshell/gateways//mtls/`). -pub fn print_ssh_config(gateway: &str, name: &str, workspace: &str) { - print!("{}", render_ssh_config(gateway, name, workspace)); +pub fn print_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result<()> { + print!("{}", render_ssh_config(gateway, name, workspace)?); + Ok(()) } #[cfg(test)] @@ -2351,7 +2457,8 @@ mod tests { environment.run(|| { set_default_ssh_log_level("DEBUG"); let expected = override_level.unwrap_or("DEBUG"); - let command = ssh_base_command("openshell ssh-tunnel"); + let proxy = PinnedProxy::new(); + let command = ssh_base_command(&proxy.command); assert!( command .get_args() @@ -2467,6 +2574,43 @@ mod tests { use crate::TEST_ENV_LOCK; use crate::test_utils::Environment; + const PUBLISHED_FINGERPRINT: &str = "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA"; + + /// A proxy command for sandbox `demo` whose host key is pinned at + /// `known_hosts`. + struct PinnedProxy { + command: SandboxProxyCommand, + known_hosts: PathBuf, + _dir: tempfile::TempDir, + } + + impl PinnedProxy { + fn new() -> Self { + Self::in_directory("known_hosts") + } + + fn in_directory(name: &str) -> Self { + let dir = tempfile::tempdir().unwrap(); + let known_hosts = dir.path().join(name).join("openshell-demo.default"); + let target = SandboxSshTarget { + gateway: "prod", + workspace: "default", + sandbox: "demo", + }; + let pin = + KnownHostsPin::at(known_hosts.clone(), target.sandbox, target.workspace).unwrap(); + let host_key = + HostKeyCheck::for_relay(PUBLISHED_FINGERPRINT.parse().unwrap(), pin).unwrap(); + let command = + sandbox_proxy_command("openshell", "https://gw:443", &target, "tok", host_key); + Self { + command, + known_hosts, + _dir: dir, + } + } + } + #[test] fn upsert_host_block_appends_when_missing() { let input = "Host existing\n HostName example.com\n"; @@ -2556,14 +2700,11 @@ mod tests { let setup = phase == "setup"; async move { if setup { + let PinnedProxy { command, _dir, .. } = PinnedProxy::new(); let session = SshSessionConfig { - proxy_command: String::new(), + proxy_command: command, sandbox_id: String::new(), - sandbox_name: String::new(), - gateway_url: String::new(), - token: String::new(), main_terminal: false, - replica: None, }; return ssh_run_capture_stdout(&session, "unused").await.map(|_| ()); } @@ -2795,22 +2936,74 @@ mod tests { } #[test] - fn render_ssh_config_includes_workspace_in_proxy_command() { - let config = render_ssh_config("my-gw", "demo", "beta"); - assert!( - config.contains("Host openshell-demo.beta"), - "host alias should be workspace-qualified: {config}" - ); - assert!( - config.contains("--workspace beta"), - "ProxyCommand should include --workspace: {config}" + fn host_key_refusals_before_ssh_starts_are_not_retried() { + use openshell_core::ssh_host_key::HostKeyError; + for refusal in [ + HostKeyError::Unpublished { + sandbox: "demo".to_string(), + }, + HostKeyError::BadFingerprint, + ] { + let error = Err::<(), _>(refusal).into_diagnostic().unwrap_err(); + assert!(!connect_error_is_retryable(&error), "{error:?}"); + assert!(!sync_error_is_retryable(&error), "{error:?}"); + } + } + + #[test] + fn stale_ssh_config_refusal_names_the_gateway_and_workspace_of_the_entry() { + assert_eq!( + stale_ssh_config_refusal(Some("my gw"), "beta", "demo"), + "openshell: this SSH config entry for sandbox 'demo' (workspace 'beta') predates host-key verification and is refused.\n\ + Regenerate it: openshell -g 'my gw' --workspace beta sandbox connect demo --editor vscode\n\ + Or replace the entry with the output of: openshell -g 'my gw' --workspace beta sandbox ssh-config demo" ); } + /// `ssh` resolves the rendered block to the intended options, also when + /// the pin path needs quoting and a later block enables connection sharing. + #[cfg(unix)] #[test] - fn host_alias_includes_workspace() { - assert_eq!(host_alias("demo", "default"), "openshell-demo.default"); - assert_eq!(host_alias("demo", "beta"), "openshell-demo.beta"); + fn rendered_ssh_config_makes_ssh_trust_only_the_pin() { + let home = tempfile::tempdir().unwrap(); + crate::test_utils::with_tmp_xdg(&home.path().join("o'brien config home"), |config_home| { + let known_hosts = config_home + .join("openshell/known_hosts/my-gw/openshell-demo.beta") + .display() + .to_string(); + let config = config_home.join("rendered_ssh_config"); + fs::create_dir_all(config_home).unwrap(); + let user_sharing = format!( + "Host *\n ControlMaster auto\n ControlPath \"{}/mux-%C\"\n ControlPersist yes\n", + config_home.display() + ); + fs::write( + &config, + render_ssh_config("my-gw", "demo", "beta").unwrap() + &user_sharing, + ) + .unwrap(); + + let effective = ssh_effective_options(&config, ["openshell-demo.beta"]); + + assert_effective_options(&effective, &["user sandbox"]); + assert_only_the_pin_is_trusted( + &effective, + Path::new(&known_hosts), + "openshell-demo.beta", + ); + assert_connection_sharing_disabled(&effective); + let proxy_command = effective + .lines() + .find_map(|line| line.strip_prefix("proxycommand ")) + .unwrap(); + assert!( + proxy_command.ends_with(&format!( + " ssh-proxy --gateway-name my-gw --name demo --workspace beta --known-hosts {}", + shell_escape(&known_hosts) + )), + "{proxy_command}" + ); + }); } #[test] @@ -2858,31 +3051,21 @@ mod tests { assert_eq!(forward_probe_host(&loopback), "127.0.0.1"); } - /// Resolve `ssh -G` for `command`'s arguments against a user config that - /// enables connection sharing and forking for the host `sandbox`. `extra_args` - /// supplies the host and, for the forking option, `-N`, when `command` lacks them. + /// The options `ssh -G` reports for `args` when `config` is the user's + /// SSH config. #[cfg(unix)] - fn effective_ssh_config(command: &Command, extra_args: &[&str]) -> String { + fn ssh_effective_options>( + config: &Path, + args: impl IntoIterator, + ) -> String { let _guard = TEST_ENV_LOCK .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); - let dir = tempfile::tempdir().unwrap(); - let config = dir.path().join("ssh_config"); - fs::write( - &config, - format!( - "Host sandbox\n ControlMaster auto\n ControlPath {}/mux-%C\n ControlPersist yes\n ForkAfterAuthentication yes\n", - dir.path().display() - ), - ) - .unwrap(); - let output = Command::new("ssh") .arg("-F") - .arg(&config) + .arg(config) .arg("-G") - .args(command.get_args()) - .args(extra_args) + .args(args) .output() .unwrap(); assert!( @@ -2893,6 +3076,39 @@ mod tests { String::from_utf8(output.stdout).unwrap() } + #[cfg(unix)] + fn assert_effective_options(effective: &str, expected: &[&str]) { + for option in expected { + assert!( + effective.lines().any(|line| line == *option), + "ssh does not report `{option}`:\n{effective}" + ); + } + } + + /// Resolve `ssh -G` for `command`'s arguments against a user config that + /// enables connection sharing and forking for the host `sandbox` and trusts + /// any host key. `extra_args` supplies the host and, for the forking option, + /// `-N`, when `command` lacks them. + #[cfg(unix)] + fn effective_ssh_config(command: &Command, extra_args: &[&str]) -> String { + let dir = tempfile::tempdir().unwrap(); + let config = dir.path().join("ssh_config"); + fs::write( + &config, + format!( + "Host sandbox\n ControlMaster auto\n ControlPath {dir}/mux-%C\n ControlPersist yes\n ForkAfterAuthentication yes\n \ + StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n GlobalKnownHostsFile {dir}/global_known_hosts\n \ + HostKeyAlias another-host\n HostKeyAlgorithms rsa-sha2-512\n VerifyHostKeyDNS yes\n UpdateHostKeys yes\n", + dir = dir.path().display() + ), + ) + .unwrap(); + + let extra_args = extra_args.iter().map(std::ffi::OsStr::new); + ssh_effective_options(&config, command.get_args().chain(extra_args)) + } + #[cfg(unix)] fn assert_connection_sharing_disabled(effective: &str) { assert!(effective.lines().any(|line| line == "controlmaster false")); @@ -2904,24 +3120,53 @@ mod tests { ); } + #[cfg(unix)] + fn assert_only_the_pin_is_trusted(effective: &str, known_hosts: &Path, alias: &str) { + assert_effective_options( + effective, + &[ + "stricthostkeychecking true", + &format!("userknownhostsfile {}", known_hosts.display()), + "globalknownhostsfile /dev/null", + &format!("hostkeyalias {alias}"), + "hostkeyalgorithms ssh-ed25519", + "verifyhostkeydns false", + "updatehostkeys false", + ], + ); + } + #[cfg(unix)] #[test] fn ssh_base_command_overrides_user_connection_sharing() { - let effective = - effective_ssh_config(&ssh_base_command("openshell ssh-proxy"), &["-N", "sandbox"]); + let proxy = PinnedProxy::new(); + let effective = effective_ssh_config(&ssh_base_command(&proxy.command), &["-N", "sandbox"]); assert_connection_sharing_disabled(&effective); } + #[cfg(unix)] + #[test] + fn ssh_base_command_overrides_user_host_key_trust() { + for directory in ["known_hosts", "known hosts", "o'brien known=hosts"] { + let proxy = PinnedProxy::in_directory(directory); + let effective = + effective_ssh_config(&ssh_base_command(&proxy.command), &["-N", "sandbox"]); + assert_only_the_pin_is_trusted( + &effective, + &proxy.known_hosts, + "openshell-demo.default", + ); + } + } + #[cfg(unix)] #[test] fn forward_ssh_command_overrides_user_multiplexing_and_forking() { - let forward = ssh_forward_command( - "openshell ssh-proxy --sandbox demo", - "sandbox-id", - &ForwardSpec::new(18080), - ); + let proxy = PinnedProxy::new(); + let forward = ssh_forward_command(&proxy.command, "sandbox-id", &ForwardSpec::new(18080)); let effective = effective_ssh_config(&forward, &[]); assert_connection_sharing_disabled(&effective); + assert_only_the_pin_is_trusted(&effective, &proxy.known_hosts, "openshell-demo.default"); assert!( effective .lines() @@ -2932,9 +3177,11 @@ mod tests { #[cfg(unix)] #[test] fn upload_ssh_command_overrides_user_multiplexing_and_forking() { - let upload = ssh_upload_command("openshell ssh-proxy --sandbox demo"); + let proxy = PinnedProxy::new(); + let upload = ssh_upload_command(&proxy.command); let effective = effective_ssh_config(&upload, &[]); assert_connection_sharing_disabled(&effective); + assert_only_the_pin_is_trusted(&effective, &proxy.known_hosts, "openshell-demo.default"); for expected in [ "forkafterauthentication no", "requesttty false", diff --git a/crates/openshell-cli/src/tls.rs b/crates/openshell-cli/src/tls.rs index fd138aae5b..d294f5303e 100644 --- a/crates/openshell-cli/src/tls.rs +++ b/crates/openshell-cli/src/tls.rs @@ -131,7 +131,7 @@ pub struct TlsMaterials { /// Resolve the TLS cert directory for a known gateway name. fn tls_dir_for_gateway(name: &str) -> Option { - let safe_name = sanitize_name(name); + let safe_name = openshell_core::paths::gateway_dir_name(name); let base = xdg_config_dir().ok()?.join("openshell").join("gateways"); Some(base.join(safe_name).join("mtls")) } @@ -158,24 +158,11 @@ fn default_tls_dir(server: &str) -> Option { } let name = name.unwrap_or_else(|| "openshell".to_string()); - let safe_name = sanitize_name(&name); + let safe_name = openshell_core::paths::gateway_dir_name(&name); let base = xdg_config_dir().ok()?.join("openshell").join("gateways"); Some(base.join(safe_name).join("mtls")) } -fn sanitize_name(value: &str) -> String { - value - .chars() - .map(|ch| { - if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_') { - ch - } else { - '_' - } - }) - .collect() -} - fn xdg_config_dir() -> Result { openshell_core::paths::xdg_config_dir() } diff --git a/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs b/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs index 8e7c0b190f..d8b668a47a 100644 --- a/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs +++ b/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs @@ -58,6 +58,8 @@ fn selected_workspace( } } +const PUBLISHED_HOST_KEY: &str = "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA"; + #[derive(Clone, Default)] struct SandboxState { /// Make `ListProviderProfiles` fail while every other RPC keeps working, @@ -508,6 +510,7 @@ impl OpenShell for TestOpenShell { gateway_scheme: "https".to_string(), gateway_host: "localhost".to_string(), gateway_port: 443, + host_key_fingerprint: PUBLISHED_HOST_KEY.to_string(), ..CreateSshSessionResponse::default() })) } @@ -1319,6 +1322,22 @@ fn install_fake_ssh(dir: &TempDir) -> std::path::PathBuf { install_executable_script(dir, "ssh", "#!/bin/sh\nexit 0\n") } +/// A fake `ssh` that answers the workspace probe and records its arguments, +/// one per line, in the returned file. Also installs a fake `code`. +fn install_recording_ssh(dir: &TempDir) -> std::path::PathBuf { + let arguments = dir.path().join("ssh-arguments"); + install_executable_script( + dir, + "ssh", + format!( + "#!/bin/sh\nprintf '%s\\n' \"$@\" > '{}'\nprintf '/sandbox\\n'\n", + arguments.display() + ), + ); + install_executable_script(dir, "code", "#!/bin/sh\nexit 0\n"); + arguments +} + fn install_fake_pgrep_no_match(dir: &TempDir) -> std::path::PathBuf { install_executable_script(dir, "pgrep", "#!/bin/sh\nexit 1\n") } @@ -3303,6 +3322,67 @@ async fn sandbox_forward_background_tracks_owned_child_when_pid_discovery_fails( .await; } +#[tokio::test] +async fn editor_connect_pins_the_published_host_key_for_ssh_and_the_config_entry() { + let server = run_server().await; + let fake_ssh_dir = helpers::tempdir().unwrap(); + let xdg_dir = helpers::tempdir().unwrap(); + + test_env(&fake_ssh_dir, &xdg_dir) + .run_async(async { + let tls = test_tls(&server); + let ssh_arguments = install_recording_ssh(&fake_ssh_dir); + let pin = xdg_dir + .path() + .join("openshell/known_hosts/openshell/openshell-pinned.default"); + + run::sandbox_connect_editor( + &server.endpoint, + "openshell", + "pinned", + run::Editor::Vscode, + &tls, + "default", + ) + .await + .unwrap(); + + let ssh_arguments = fs::read_to_string(ssh_arguments).unwrap(); + let proxy_command = ssh_arguments + .lines() + .find(|argument| argument.starts_with("ProxyCommand=")) + .unwrap(); + assert!( + proxy_command.ends_with(&format!( + " --host-key '{PUBLISHED_HOST_KEY}' --known-hosts {}", + pin.display() + )), + "{proxy_command}" + ); + for option in [ + "StrictHostKeyChecking=yes".to_string(), + format!("UserKnownHostsFile=\"{}\"", pin.display()), + "HostKeyAlias=openshell-pinned.default".to_string(), + ] { + assert!( + ssh_arguments.lines().any(|argument| argument == option), + "ssh was not passed {option}:\n{ssh_arguments}" + ); + } + let config_entry = + fs::read_to_string(xdg_dir.path().join("openshell/ssh_config")).unwrap(); + assert!( + config_entry.contains(&format!(" UserKnownHostsFile \"{}\"\n", pin.display())), + "{config_entry}" + ); + assert!( + config_entry.contains(&format!(" --known-hosts {}\n", pin.display())), + "{config_entry}" + ); + }) + .await; +} + #[tokio::test] #[ignore = "flaky under concurrent test execution"] async fn sandbox_forward_foreground_fails_when_ssh_exits_before_listener_opens() { diff --git a/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs b/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs new file mode 100644 index 0000000000..0a429109e3 --- /dev/null +++ b/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs @@ -0,0 +1,739 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +use bytes::Bytes; +use http_body_util::{BodyExt, StreamBody}; +use hyper::body::{Frame, Incoming}; +use hyper::service::service_fn; +use hyper_util::rt::{TokioExecutor, TokioIo}; +use openshell_core::proto::{ + CreateSshSessionResponse, Sandbox, SandboxResponse, TcpForwardFrame, tcp_forward_frame, +}; +use prost::Message; +use russh::keys::ssh_key::private::Ed25519Keypair; +use russh::keys::{HashAlg, PrivateKey}; +use std::convert::Infallible; +use std::net::SocketAddr; +use std::path::{Path, PathBuf}; +use std::process::{Output, Stdio}; +use std::sync::Arc; +use std::time::Duration; +use tokio::io::{AsyncReadExt, AsyncWriteExt, DuplexStream}; +use tokio_stream::wrappers::ReceiverStream; + +const SANDBOX: &str = "my-sb"; +const WORKSPACE: &str = "team-a"; +const ALLOW_UNVERIFIED: &str = "OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY"; +const PIN_HEADER: &str = + "# Managed by OpenShell. Rewritten from the gateway-published fingerprint; do not edit.\n"; + +type GrpcFrames = tokio::sync::mpsc::Sender, Infallible>>; +type GrpcBody = StreamBody, Infallible>>>; + +fn host_key(seed: u8) -> PrivateKey { + PrivateKey::from(Ed25519Keypair::from_seed(&[seed; 32])) +} + +fn fingerprint(key: &PrivateKey) -> String { + key.public_key().fingerprint(HashAlg::Sha256).to_string() +} + +/// The pin file `ssh-proxy` leaves behind: holding `key`, or no key at all. +fn pin_content(key: Option<&PrivateKey>) -> String { + key.map_or_else( + || PIN_HEADER.to_string(), + |key| { + format!( + "{PIN_HEADER}openshell-{SANDBOX}.{WORKSPACE} {}\n", + key.public_key().to_openssh().unwrap() + ) + }, + ) +} + +struct Scratch { + dir: tempfile::TempDir, +} + +impl Scratch { + fn new() -> Self { + Self { + dir: tempfile::tempdir().unwrap(), + } + } + + fn config(&self) -> PathBuf { + self.dir.path().join("config") + } + + fn pin(&self) -> PathBuf { + self.pin_named("pin") + } + + fn pin_named(&self, name: &str) -> PathBuf { + self.dir.path().join("known_hosts").join(name) + } +} + +/// A gateway that publishes one host-key fingerprint for the sandbox and +/// relays every SSH connection to `sandbox`. +struct Gateway { + published: String, + sandbox: Box, +} + +impl Gateway { + async fn serve(self) -> SocketAddr { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let gateway = Arc::new(self); + tokio::spawn(async move { + while let Ok((socket, _)) = listener.accept().await { + openshell_core::net::set_tcp_nodelay_best_effort(&socket); + let gateway = gateway.clone(); + let service = service_fn(move |request| { + let gateway = gateway.clone(); + async move { Ok::<_, Infallible>(gateway.respond(request, address).await) } + }); + tokio::spawn(async move { + let _ = hyper::server::conn::http2::Builder::new(TokioExecutor::new()) + .serve_connection(TokioIo::new(socket), service) + .await; + }); + } + }); + address + } + + async fn respond( + &self, + request: hyper::Request, + address: SocketAddr, + ) -> hyper::Response { + let (frames, body) = tokio::sync::mpsc::channel(16); + match request.uri().path() { + "/openshell.v1.OpenShell/GetSandbox" => { + let found = SandboxResponse { + sandbox: Some(Sandbox::default()), + ..Default::default() + }; + respond_once(&frames, &found).await; + } + "/openshell.v1.OpenShell/CreateSshSession" => { + let session = CreateSshSessionResponse { + sandbox_id: "sandbox-1".to_string(), + token: "test-token".to_string(), + gateway_host: address.ip().to_string(), + gateway_port: u32::from(address.port()), + gateway_scheme: "http".to_string(), + host_key_fingerprint: self.published.clone(), + ..Default::default() + }; + respond_once(&frames, &session).await; + } + "/openshell.v1.OpenShell/ForwardTcp" => { + let (relay_end, sandbox_end) = tokio::io::duplex(64 * 1024); + (self.sandbox)(sandbox_end); + tokio::spawn(relay(request.into_body(), relay_end, frames)); + } + _ => { + let _ = frames + .send(Ok(grpc_status(tonic::Status::unimplemented("")))) + .await; + } + } + let mut response = hyper::Response::new(StreamBody::new(ReceiverStream::new(body))); + response + .headers_mut() + .insert("content-type", "application/grpc".parse().unwrap()); + response + } +} + +fn grpc_message(message: &impl Message) -> Frame { + let encoded = message.encode_to_vec(); + let mut framed = vec![0]; + framed.extend_from_slice(&u32::try_from(encoded.len()).unwrap().to_be_bytes()); + framed.extend_from_slice(&encoded); + Frame::data(framed.into()) +} + +fn grpc_status(status: tonic::Status) -> Frame { + Frame::trailers(status.into_http::<()>().into_parts().0.headers) +} + +async fn respond_once(frames: &GrpcFrames, message: &impl Message) { + let _ = frames.send(Ok(grpc_message(message))).await; + let _ = frames.send(Ok(grpc_status(tonic::Status::ok("")))).await; +} + +/// Copies `ForwardTcp` data frames to the sandbox and the sandbox's bytes back. +async fn relay(mut inbound: Incoming, sandbox: DuplexStream, frames: GrpcFrames) { + let (mut from_sandbox, mut to_sandbox) = tokio::io::split(sandbox); + let to_sandbox = tokio::spawn(async move { + let mut pending = Vec::new(); + while let Some(Ok(frame)) = inbound.frame().await { + let Ok(data) = frame.into_data() else { + continue; + }; + pending.extend_from_slice(&data); + while let Some(length) = pending + .get(1..5) + .map(|length| u32::from_be_bytes(length.try_into().unwrap()) as usize) + .filter(|length| pending.len() >= 5 + length) + { + let frame = TcpForwardFrame::decode(&pending[5..5 + length]).unwrap(); + pending.drain(..5 + length); + if let Some(tcp_forward_frame::Payload::Data(data)) = frame.payload + && to_sandbox.write_all(&data).await.is_err() + { + return; + } + } + } + let _ = to_sandbox.shutdown().await; + }); + // Smaller than any key exchange packet, so the proxy has to reassemble + // each one. + let mut chunk = [0_u8; 97]; + while let Ok(read @ 1..) = from_sandbox.read(&mut chunk).await { + let frame = TcpForwardFrame { + payload: Some(tcp_forward_frame::Payload::Data(chunk[..read].to_vec())), + }; + if frames.send(Ok(grpc_message(&frame))).await.is_err() { + break; + } + } + let _ = frames.send(Ok(grpc_status(tonic::Status::ok("")))).await; + to_sandbox.abort(); +} + +fn name_mode(gateway: SocketAddr, known_hosts: &Path) -> Vec { + [ + "ssh-proxy", + "--server", + &format!("http://{gateway}"), + "--name", + SANDBOX, + "--workspace", + WORKSPACE, + "--known-hosts", + &known_hosts.display().to_string(), + ] + .map(str::to_string) + .to_vec() +} + +/// Runs `command` to completion. `allow_unverified` is the value of the +/// variable that permits a sandbox with no published fingerprint. +async fn finished( + mut command: tokio::process::Command, + config: &Path, + allow_unverified: Option<&str>, +) -> Output { + match allow_unverified { + Some(value) => command.env(ALLOW_UNVERIFIED, value), + None => command.env_remove(ALLOW_UNVERIFIED), + }; + let run = command + .env("XDG_CONFIG_HOME", config) + .env("OPENSHELL_TELEMETRY_ENABLED", "false") + .env_remove("OPENSHELL_GATEWAY") + .env_remove("OPENSHELL_GATEWAY_ENDPOINT") + .env_remove("OPENSHELL_WORKSPACE") + .stdin(Stdio::null()) + .kill_on_drop(true) + .output(); + tokio::time::timeout(Duration::from_secs(30), run) + .await + .expect("the command must exit") + .unwrap() +} + +async fn run_proxy(args: &[String], config: &Path) -> Output { + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell")); + command.args(args); + finished(command, config, None).await +} + +fn mismatch_message(published: &PrivateKey, presented: &PrivateKey) -> String { + format!( + "openshell: SSH host key mismatch for sandbox '{SANDBOX}' (workspace '{WORKSPACE}')\n \ + gateway published: {}\n \ + server presented: {}\n\ + The SSH server reached through the gateway is not this sandbox's. The handshake was not forwarded.\n\ + Do not connect. Compare the published value with `openshell --workspace {WORKSPACE} sandbox get {SANDBOX} --output json`, then stop and start the sandbox.\n\ + If the mismatch persists, contact the gateway operator.\n", + fingerprint(published), + fingerprint(presented) + ) +} + +fn ssh_string(bytes: &[u8]) -> Vec { + let mut string = u32::try_from(bytes.len()).unwrap().to_be_bytes().to_vec(); + string.extend_from_slice(bytes); + string +} + +fn ssh_packet(payload: &[u8]) -> Vec { + let padding = 8 - (5 + payload.len()) % 8; + let padding = if padding < 4 { padding + 8 } else { padding }; + let mut packet = u32::try_from(1 + payload.len() + padding) + .unwrap() + .to_be_bytes() + .to_vec(); + packet.push(u8::try_from(padding).unwrap()); + packet.extend_from_slice(payload); + packet.resize(packet.len() + padding, 0); + packet +} + +/// What an SSH server presenting `key` sends up to its first encrypted bytes. +struct Handshake { + before_reply: Vec, + reply_onward: Vec, +} + +impl Handshake { + fn of_server_with(key: &PrivateKey) -> Self { + let mut before_reply = b"SSH-2.0-OpenShell_test\r\n".to_vec(); + before_reply.extend(ssh_packet(&[[20].as_slice(), &[0; 61]].concat())); + let reply = [ + [31].as_slice(), + &ssh_string(&key.public_key().to_bytes().unwrap()), + &ssh_string(&[7; 32]), + &ssh_string(&[9; 83]), + ] + .concat(); + let mut reply_onward = ssh_packet(&reply); + reply_onward.extend(ssh_packet(&[21])); + reply_onward.extend([0xff; 48]); + Self { + before_reply, + reply_onward, + } + } + + fn bytes(&self) -> Vec { + [self.before_reply.as_slice(), &self.reply_onward].concat() + } + + /// A sandbox whose SSH server sends this handshake and hangs up. + fn sandbox(&self) -> Box { + let bytes = self.bytes(); + Box::new(move |mut stream| { + let bytes = bytes.clone(); + tokio::spawn(async move { + let _ = stream.write_all(&bytes).await; + }); + }) + } +} + +#[tokio::test] +async fn name_mode_proxy_withholds_a_key_the_gateway_did_not_publish() { + let published = host_key(1); + let presented = host_key(2); + let handshake = Handshake::of_server_with(&presented); + let gateway = Gateway { + published: fingerprint(&published), + sandbox: handshake.sandbox(), + } + .serve() + .await; + let scratch = Scratch::new(); + + let proxy = run_proxy(&name_mode(gateway, &scratch.pin()), &scratch.config()).await; + + let stderr = String::from_utf8_lossy(&proxy.stderr); + assert_eq!(proxy.status.code(), Some(1), "{stderr}"); + assert!( + handshake.before_reply.starts_with(&proxy.stdout), + "no byte of the key exchange reply may reach ssh" + ); + assert!( + stderr.contains(&mismatch_message(&published, &presented)), + "{stderr}" + ); + assert_eq!( + std::fs::read_to_string(scratch.pin()).unwrap(), + pin_content(None) + ); +} + +#[tokio::test] +async fn name_mode_proxy_refuses_a_sandbox_with_no_published_key() { + let handshake = Handshake::of_server_with(&host_key(1)); + let gateway = Gateway { + published: String::new(), + sandbox: handshake.sandbox(), + } + .serve() + .await; + let scratch = Scratch::new(); + + let proxy = run_proxy(&name_mode(gateway, &scratch.pin()), &scratch.config()).await; + + let stderr = String::from_utf8_lossy(&proxy.stderr); + assert!(!proxy.status.success()); + assert_eq!(proxy.stdout, b""); + assert!( + stderr.contains("sandbox 'my-sb' has no published SSH host key"), + "{stderr}" + ); + assert!(!scratch.pin().exists()); +} + +#[tokio::test] +async fn name_mode_proxy_reaches_a_sandbox_with_no_published_key_only_with_a_warning() { + let key = host_key(1); + let handshake = Handshake::of_server_with(&key); + let gateway = Gateway { + published: String::new(), + sandbox: handshake.sandbox(), + } + .serve() + .await; + let scratch = Scratch::new(); + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell")); + command.args(name_mode(gateway, &scratch.pin())); + + let proxy = finished(command, &scratch.config(), Some("1")).await; + + let stderr = String::from_utf8_lossy(&proxy.stderr); + assert!(proxy.status.success(), "{stderr}"); + assert_eq!(proxy.stdout, handshake.bytes()); + assert!( + stderr.contains( + "warning: SSH host key of sandbox 'my-sb' is NOT verified (the gateway published no fingerprint)" + ), + "{stderr}" + ); + assert_eq!( + std::fs::read_to_string(scratch.pin()).unwrap(), + pin_content(Some(&key)) + ); +} + +/// An `ssh` started by a client from before host-key verification accepts any +/// key, so its proxy must not relay. +#[tokio::test] +async fn proxy_refuses_a_client_that_sends_no_host_key_check() { + let handshake = Handshake::of_server_with(&host_key(1)); + let gateway = Gateway { + published: fingerprint(&host_key(1)), + sandbox: handshake.sandbox(), + } + .serve() + .await; + let scratch = Scratch::new(); + let args = [ + "ssh-proxy", + "--gateway", + &format!("http://{gateway}/proxy/connect"), + "--sandbox", + SANDBOX, + "--workspace", + WORKSPACE, + "--token", + "test-token", + ] + .map(str::to_string); + + let proxy = run_proxy(&args, &scratch.config()).await; + + assert_eq!(proxy.status.code(), Some(1)); + assert_eq!(proxy.stdout, b""); + assert_eq!( + String::from_utf8_lossy(&proxy.stderr), + "openshell: this SSH connection was started by an OpenShell client that predates host-key verification and is refused.\n\ + Restart that client (the openshell command or openshell term) and connect again.\n" + ); +} + +/// A config entry from before host-key verification tells `ssh` to accept any +/// key, so its proxy must not relay. +#[tokio::test] +async fn name_mode_proxy_refuses_a_config_entry_without_a_known_hosts_file() { + let handshake = Handshake::of_server_with(&host_key(1)); + let gateway = Gateway { + published: fingerprint(&host_key(1)), + sandbox: handshake.sandbox(), + } + .serve() + .await; + let scratch = Scratch::new(); + let mut args = name_mode(gateway, &scratch.pin()); + args.truncate(args.len() - 2); + + let proxy = run_proxy(&args, &scratch.config()).await; + + let stderr = String::from_utf8_lossy(&proxy.stderr); + assert_eq!(proxy.status.code(), Some(1), "{stderr}"); + assert_eq!(proxy.stdout, b""); + assert!( + stderr.contains("predates host-key verification and is refused"), + "{stderr}" + ); +} + +/// The real `ssh` client against a real SSH server, with `ssh-proxy` as its +/// `ProxyCommand`. +#[cfg(unix)] +mod openssh { + use super::*; + use openshell_core::forward::{sandbox_proxy_command, sandbox_ssh_args}; + use openshell_core::ssh_host_key::{HostKeyCheck, KnownHostsPin, SandboxSshTarget}; + use russh::server::{Auth, ChannelOpenHandle, Msg, Session}; + use russh::{Channel, ChannelId}; + + const COMMAND: &str = "report-ready"; + const COMMAND_OUTPUT: &str = "ready\n"; + const GATEWAY: &str = "test-gateway"; + + fn write_pin(pin: &Path, content: &str) { + std::fs::create_dir_all(pin.parent().unwrap()).unwrap(); + std::fs::write(pin, content).unwrap(); + } + + /// Accepts every login and answers every command with [`COMMAND_OUTPUT`]. + struct Shell; + + impl russh::server::Handler for Shell { + type Error = russh::Error; + + async fn auth_none(&mut self, _user: &str) -> Result { + Ok(Auth::Accept) + } + + async fn channel_open_session( + &mut self, + _channel: Channel, + reply: ChannelOpenHandle, + _session: &mut Session, + ) -> Result<(), Self::Error> { + reply.accept().await; + Ok(()) + } + + async fn exec_request( + &mut self, + channel: ChannelId, + _data: &[u8], + session: &mut Session, + ) -> Result<(), Self::Error> { + session.channel_success(channel)?; + session.data(channel, COMMAND_OUTPUT)?; + session.exit_status_request(channel, 0)?; + session.eof(channel)?; + session.close(channel)?; + Ok(()) + } + } + + /// A sandbox whose SSH server presents `key`, behind a gateway. + struct SshSandbox { + gateway: SocketAddr, + } + + impl SshSandbox { + async fn presenting(key: &PrivateKey) -> Self { + // The supervisor's server also keeps the default algorithms. + let config = Arc::new(russh::server::Config { + keys: vec![key.clone()], + ..Default::default() + }); + let gateway = Gateway { + published: fingerprint(key), + sandbox: Box::new(move |stream| { + let config = config.clone(); + tokio::spawn(async move { + if let Ok(session) = russh::server::run_stream(config, stream, Shell).await + { + let _ = session.await; + } + }); + }), + } + .serve() + .await; + Self { gateway } + } + + /// Runs [`COMMAND`] with the `ssh` arguments the CLI and TUI build for + /// a session whose published fingerprint is `published`. `ssh` takes + /// the first value of an option, so `overrides` win. + async fn ssh_as_client( + &self, + published: &str, + pin: &Path, + config: &Path, + overrides: &[String], + ) -> Output { + let target = SandboxSshTarget { + gateway: GATEWAY, + workspace: WORKSPACE, + sandbox: SANDBOX, + }; + let pin = KnownHostsPin::at(pin.to_path_buf(), SANDBOX, WORKSPACE).unwrap(); + let host_key = HostKeyCheck::for_relay(published.parse().unwrap(), pin).unwrap(); + let proxy_command = sandbox_proxy_command( + env!("CARGO_BIN_EXE_openshell"), + &format!("http://{}", self.gateway), + &target, + "test-token", + host_key, + ); + let mut ssh = tokio::process::Command::new("ssh"); + ssh.args(["-F", "/dev/null", "-o", "BatchMode=yes"]) + .args(overrides.iter().flat_map(|option| ["-o", option])) + .args(sandbox_ssh_args(&proxy_command, "ERROR")) + .args(["sandbox", COMMAND]); + finished(ssh, config, None).await + } + + /// Runs [`COMMAND`] through the config entry `sandbox ssh-config` + /// prints, with no other configuration and no options of its own. + async fn ssh_through_config_entry(&self, config: &Path) -> Output { + let gateway_dir = config.join("openshell/gateways").join(GATEWAY); + std::fs::create_dir_all(&gateway_dir).unwrap(); + let metadata = serde_json::json!({ + "name": GATEWAY, + "gateway_endpoint": format!("http://{}", self.gateway), + "gateway_port": self.gateway.port(), + "is_remote": false, + "auth_mode": "plaintext", + }); + std::fs::write(gateway_dir.join("metadata.json"), metadata.to_string()).unwrap(); + + let mut print_entry = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell")); + print_entry.args([ + "--gateway", + GATEWAY, + "--workspace", + WORKSPACE, + "sandbox", + "ssh-config", + SANDBOX, + ]); + let printed = finished(print_entry, config, None).await; + assert!( + printed.status.success(), + "{}", + String::from_utf8_lossy(&printed.stderr) + ); + let entry = config.join("ssh_config_entry"); + std::fs::write(&entry, printed.stdout).unwrap(); + + let mut ssh = tokio::process::Command::new("ssh"); + ssh.arg("-F") + .arg(entry) + .args(["-o", "BatchMode=yes"]) + .args([format!("openshell-{SANDBOX}.{WORKSPACE}").as_str(), COMMAND]); + finished(ssh, config, None).await + } + } + + /// The proxy accepts and forwards any key here, so the refusal is `ssh`'s + /// own strict check against a file the proxy does not write. + #[tokio::test] + async fn ssh_refuses_a_key_missing_from_its_own_known_hosts_file() { + let key = host_key(1); + let sandbox = SshSandbox::presenting(&key).await; + let scratch = Scratch::new(); + let proxy_pin = scratch.pin_named("proxy"); + let ssh_pin = scratch.pin_named("ssh"); + let another_key = pin_content(Some(&host_key(2))); + write_pin(&ssh_pin, &another_key); + + let ssh = sandbox + .ssh_as_client( + "unverified", + &proxy_pin, + &scratch.config(), + &[format!("UserKnownHostsFile=\"{}\"", ssh_pin.display())], + ) + .await; + + let stderr = String::from_utf8_lossy(&ssh.stderr); + assert_eq!(ssh.status.code(), Some(255), "{stderr}"); + assert!(stderr.contains("Host key verification failed"), "{stderr}"); + assert_eq!(ssh.stdout, b""); + assert_eq!( + std::fs::read_to_string(&proxy_pin).unwrap(), + pin_content(Some(&key)), + "the proxy forwarded the key exchange reply" + ); + assert_eq!(std::fs::read_to_string(&ssh_pin).unwrap(), another_key); + } + + /// `ssh` is already running when the proxy pins the key, so this holds + /// only because `ssh` reads its known-hosts file when it processes the key + /// exchange reply, not when it starts. Host-key verification rests on it. + #[tokio::test] + async fn ssh_with_the_client_arguments_reaches_the_published_sandbox() { + let key = host_key(1); + let sandbox = SshSandbox::presenting(&key).await; + let scratch = Scratch::new(); + let pin = scratch.pin(); + + let ssh = sandbox + .ssh_as_client(&fingerprint(&key), &pin, &scratch.config(), &[]) + .await; + + let stderr = String::from_utf8_lossy(&ssh.stderr); + assert!(ssh.status.success(), "{stderr}"); + assert_eq!(String::from_utf8_lossy(&ssh.stdout), COMMAND_OUTPUT); + assert_eq!( + std::fs::read_to_string(&pin).unwrap(), + pin_content(Some(&key)) + ); + } + + #[tokio::test] + async fn ssh_with_the_client_arguments_never_reaches_another_server() { + let presented = host_key(1); + let published = host_key(2); + let sandbox = SshSandbox::presenting(&presented).await; + let scratch = Scratch::new(); + let pin = scratch.pin(); + + let ssh = sandbox + .ssh_as_client(&fingerprint(&published), &pin, &scratch.config(), &[]) + .await; + + let stderr = String::from_utf8_lossy(&ssh.stderr); + assert_eq!(ssh.status.code(), Some(255), "{stderr}"); + assert!( + stderr.contains(&mismatch_message(&published, &presented)), + "{stderr}" + ); + assert_eq!(ssh.stdout, b""); + assert_eq!(std::fs::read_to_string(&pin).unwrap(), pin_content(None)); + } + + /// The pin still holds the stale key when `ssh` starts, so this holds only + /// because the proxy replaces it before `ssh` reads the file. + #[tokio::test] + async fn ssh_connects_through_the_generated_config_entry_despite_a_stale_pin() { + let key = host_key(1); + let sandbox = SshSandbox::presenting(&key).await; + let scratch = Scratch::new(); + let pin = scratch + .config() + .join("openshell/known_hosts") + .join(GATEWAY) + .join(format!("openshell-{SANDBOX}.{WORKSPACE}")); + write_pin(&pin, &pin_content(Some(&host_key(2)))); + + let ssh = sandbox.ssh_through_config_entry(&scratch.config()).await; + + let stderr = String::from_utf8_lossy(&ssh.stderr); + assert!(ssh.status.success(), "{stderr}"); + assert_eq!(String::from_utf8_lossy(&ssh.stdout), COMMAND_OUTPUT); + assert_eq!( + std::fs::read_to_string(&pin).unwrap(), + pin_content(Some(&key)) + ); + } +} diff --git a/crates/openshell-cli/tests/ssh_proxy_shutdown_integration.rs b/crates/openshell-cli/tests/ssh_proxy_shutdown_integration.rs index 76f9b32f5e..8afb20bdbd 100644 --- a/crates/openshell-cli/tests/ssh_proxy_shutdown_integration.rs +++ b/crates/openshell-cli/tests/ssh_proxy_shutdown_integration.rs @@ -72,6 +72,10 @@ async fn proxy_exits_with_stdin_open(relay_status: tonic::Status) { "repro", "--token", "test-token", + "--host-key", + "unverified", + "--known-hosts", + &config.path().join("known_hosts").display().to_string(), ]) .env("XDG_CONFIG_HOME", config.path()) .env("OPENSHELL_TELEMETRY_ENABLED", "false") diff --git a/crates/openshell-core/src/forward.rs b/crates/openshell-core/src/forward.rs index c8e6c10131..c9fc794414 100644 --- a/crates/openshell-core/src/forward.rs +++ b/crates/openshell-core/src/forward.rs @@ -7,6 +7,7 @@ //! start, stop, list, and track background SSH port forwards. use crate::paths::{create_dir_restricted, xdg_config_dir}; +use crate::ssh_host_key::{HostKeyCheck, SandboxSshTarget}; use miette::{IntoDiagnostic, Result, WrapErr}; use std::borrow::Cow; use std::net::TcpListener; @@ -364,14 +365,13 @@ fn proxy_command_option_present(args: &[&str], proxy_index: usize) -> bool { fn proxy_option_takes_value(arg: &str) -> bool { matches!( arg, - "--gateway" | "--workspace" | "--token" | "--gateway-name" + "--gateway" | "--workspace" | "--token" | "--gateway-name" | "--host-key" | "--known-hosts" ) } fn proxy_option_has_inline_value(arg: &str) -> bool { - ["--gateway=", "--workspace=", "--token=", "--gateway-name="] - .iter() - .any(|prefix| arg.starts_with(prefix)) + arg.split_once('=') + .is_some_and(|(option, _)| proxy_option_takes_value(option)) } fn is_outer_ssh_option_start(arg: &str) -> bool { @@ -928,7 +928,7 @@ pub fn shell_escape(value: &str) -> String { /// Every interpolated argument is shell-escaped so that server-supplied values /// (gateway URL, sandbox name, workspace, token, gateway name) cannot inject shell /// metacharacters into the command that OpenSSH executes via `/bin/sh -c`. -pub fn build_proxy_command( +fn build_proxy_command( exe: &str, gateway_url: &str, sandbox_name: &str, @@ -947,25 +947,52 @@ pub fn build_proxy_command( ) } +/// The `ProxyCommand` of a sandbox SSH connection, with the host-key decision +/// it was built with. +pub struct SandboxProxyCommand { + command: String, + host_key: HostKeyCheck, +} + +/// Build the `ProxyCommand` that tunnels `ssh` to a sandbox and checks the +/// host key the sandbox presents. +pub fn sandbox_proxy_command( + exe: &str, + gateway_url: &str, + target: &SandboxSshTarget<'_>, + token: &str, + host_key: HostKeyCheck, +) -> SandboxProxyCommand { + let mut command = build_proxy_command( + exe, + gateway_url, + target.sandbox, + target.workspace, + token, + target.gateway, + ); + for flag in host_key.proxy_flags() { + command.push(' '); + command.push_str(&shell_escape(&flag)); + } + SandboxProxyCommand { command, host_key } +} + /// Build the `ssh` options shared by every sandbox session. /// -/// Every sandbox is addressed as the host `sandbox`, so a user's `ControlMaster` -/// or `ControlPath` would reuse one sandbox's connection for another. Command -/// line options beat `ssh_config`, so connection sharing is turned off here. -pub fn sandbox_ssh_args(proxy_command: &str, log_level: &str) -> Vec { - [ - format!("ProxyCommand={proxy_command}"), - "StrictHostKeyChecking=no".to_string(), - "UserKnownHostsFile=/dev/null".to_string(), - "GlobalKnownHostsFile=/dev/null".to_string(), - format!("LogLevel={log_level}"), - "ControlMaster=no".to_string(), - "ControlPath=none".to_string(), - "ControlPersist=no".to_string(), - ] - .into_iter() - .flat_map(|option| ["-o".to_string(), option]) - .collect() +/// Command line options beat `ssh_config`, so connection sharing stays off and +/// the host-key options hold whatever the user's config says. +pub fn sandbox_ssh_args(proxy: &SandboxProxyCommand, log_level: &str) -> Vec { + let host_key_options = proxy + .host_key + .ssh_options() + .into_iter() + .map(|(key, value)| format!("{key}={value}")); + std::iter::once(format!("ProxyCommand={}", proxy.command)) + .chain(host_key_options) + .chain([format!("LogLevel={log_level}")]) + .flat_map(|option| ["-o".to_string(), option]) + .collect() } /// Error returned when a `CreateSshSessionResponse` fails validation. @@ -1015,7 +1042,7 @@ fn is_fingerprint_byte(b: u8) -> bool { /// Validate a `CreateSshSessionResponse` before any of its fields are used to /// build a shell command or config file. /// -/// This is a belt-and-suspenders pair to [`build_proxy_command`]: escaping +/// This is a belt-and-suspenders pair to [`sandbox_proxy_command`]: escaping /// alone is sufficient to prevent injection, but rejecting malformed fields /// at the trust boundary fails loudly before the string is assembled and /// catches gateway bugs or tampering early. @@ -1107,6 +1134,7 @@ pub fn build_sandbox_notes( #[cfg(test)] mod tests { use super::*; + use crate::ssh_host_key::KnownHostsPin; use crate::test_environment::Environment; #[test] @@ -1394,9 +1422,48 @@ mod tests { ); } + const HOST_KEY_FINGERPRINT: &str = "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA"; + + fn sandbox_proxy(exe: &str, known_hosts: &std::path::Path) -> SandboxProxyCommand { + let target = SandboxSshTarget { + gateway: "local", + workspace: "team-a", + sandbox: "sbx-1", + }; + let pin = + KnownHostsPin::at(known_hosts.to_path_buf(), target.sandbox, target.workspace).unwrap(); + let host_key = HostKeyCheck::for_relay(HOST_KEY_FINGERPRINT.parse().unwrap(), pin).unwrap(); + sandbox_proxy_command(exe, "https://127.0.0.1:9443", &target, "tok_123", host_key) + } + + #[test] + fn sandbox_proxy_command_hands_the_host_key_check_to_the_proxy() { + let dir = tempfile::tempdir().unwrap(); + let known_hosts = dir.path().join("pin dir").join("openshell-sbx-1.team-a"); + let args = sandbox_ssh_args( + &sandbox_proxy("/Application Support/openshell", &known_hosts), + "ERROR", + ); + + let proxy_command = args[1].strip_prefix("ProxyCommand=").unwrap(); + let words = split_proxy_command_words(proxy_command); + assert_eq!( + words[words.len() - 4..], + [ + "--host-key", + HOST_KEY_FINGERPRINT, + "--known-hosts", + known_hosts.to_str().unwrap(), + ] + ); + } + #[test] - fn sandbox_ssh_args_disable_connection_sharing() { - let args = sandbox_ssh_args("openshell ssh-proxy", "DEBUG"); + fn sandbox_ssh_args_trust_only_the_pinned_host_key() { + let dir = tempfile::tempdir().unwrap(); + let known_hosts = dir.path().join("openshell-sbx-1.team-a"); + let args = sandbox_ssh_args(&sandbox_proxy("openshell", &known_hosts), "DEBUG"); + let options: Vec<&str> = args .chunks(2) .map(|pair| { @@ -1404,11 +1471,23 @@ mod tests { pair[1].as_str() }) .collect(); - assert!(options.contains(&"ProxyCommand=openshell ssh-proxy")); - assert!(options.contains(&"LogLevel=DEBUG")); - for option in ["ControlMaster=no", "ControlPath=none", "ControlPersist=no"] { - assert!(options.contains(&option), "missing {option}"); - } + assert!(options[0].starts_with("ProxyCommand=openshell ssh-proxy ")); + assert_eq!( + options[1..], + [ + "StrictHostKeyChecking=yes", + &format!("UserKnownHostsFile=\"{}\"", known_hosts.display()), + "GlobalKnownHostsFile=/dev/null", + "HostKeyAlias=openshell-sbx-1.team-a", + "HostKeyAlgorithms=ssh-ed25519", + "VerifyHostKeyDNS=no", + "UpdateHostKeys=no", + "ControlMaster=no", + "ControlPath=none", + "ControlPersist=no", + "LogLevel=DEBUG", + ] + ); } /// Helper: return the concatenation of characters that appear outside @@ -1790,6 +1869,82 @@ mod tests { )); } + /// The argument list of a CLI background forward. + fn generated_forward_argv(proxy: &SandboxProxyCommand) -> Vec { + let mut argv = vec!["/usr/bin/ssh".to_string()]; + argv.extend(sandbox_ssh_args(proxy, "ERROR")); + argv.extend( + [ + "-o", + "ServerAliveInterval=15", + "-o", + "ServerAliveCountMax=3", + "-o", + "ForkAfterAuthentication=no", + "-N", + "-o", + "ExitOnForwardFailure=yes", + "-o", + "SetEnv=OPENSHELL_FORWARD_SANDBOX_ID=id-1", + "-L", + "127.0.0.1:80:127.0.0.1:80", + "sandbox", + ] + .map(str::to_string), + ); + argv + } + + fn assert_matches_only_its_own_forward(tokens: &[&str]) { + let matches = |port, workspace, sandbox, sandbox_id| { + args_match_ssh_forward( + tokens, + port, + Some(workspace), + Some(sandbox), + Some(sandbox_id), + ) + }; + assert!(matches(80, "team-a", "sbx-1", "id-1")); + assert!(!matches(8080, "team-a", "sbx-1", "id-1")); + assert!(!matches(80, "team-b", "sbx-1", "id-1")); + assert!(!matches(80, "team-a", "sbx-2", "id-1")); + assert!(!matches(80, "team-a", "sbx-1", "id-2")); + } + + #[test] + fn ssh_forward_command_matches_exact_argv_with_host_key_flags() { + let dir = tempfile::tempdir().unwrap(); + let known_hosts = dir.path().join("pin dir").join("openshell-sbx-1.team-a"); + let argv = generated_forward_argv(&sandbox_proxy( + "/Application Support/openshell", + &known_hosts, + )); + + let expanded: Vec = argv + .iter() + .flat_map(|arg| expand_proxy_command_arg(arg)) + .collect(); + let tokens: Vec<&str> = expanded.iter().map(String::as_str).collect(); + + assert_matches_only_its_own_forward(&tokens); + } + + #[test] + fn ssh_forward_command_matches_whitespace_split_line_with_host_key_flags() { + let dir = tempfile::tempdir().unwrap(); + let known_hosts = dir.path().join("openshell-sbx-1.team-a"); + let line = + generated_forward_argv(&sandbox_proxy("/path/openshell", &known_hosts)).join(" "); + + assert_matches_only_its_own_forward(&line.split_whitespace().collect::>()); + + let inline = format!( + "ssh -o ProxyCommand=openshell ssh-proxy --sandbox=sbx-1 --workspace=team-a --host-key={HOST_KEY_FINGERPRINT} --known-hosts=/pins/openshell-sbx-1.team-a -N -o SetEnv=OPENSHELL_FORWARD_SANDBOX_ID=id-1 -L 80:127.0.0.1:80 sandbox" + ); + assert_matches_only_its_own_forward(&inline.split_whitespace().collect::>()); + } + #[test] fn split_proxy_command_words_round_trips_shell_escape() { assert_eq!(split_proxy_command_words("a b c"), vec!["a", "b", "c"]); diff --git a/crates/openshell-core/src/lib.rs b/crates/openshell-core/src/lib.rs index 12473a702c..5c48ea056b 100644 --- a/crates/openshell-core/src/lib.rs +++ b/crates/openshell-core/src/lib.rs @@ -66,6 +66,7 @@ pub mod secrets; pub mod settings; pub mod shell; pub mod spiffe; +pub mod ssh_host_key; pub mod telemetry; pub mod time; pub mod token_grant_failure; diff --git a/crates/openshell-core/src/paths.rs b/crates/openshell-core/src/paths.rs index 0470ab6764..c08b33b23c 100644 --- a/crates/openshell-core/src/paths.rs +++ b/crates/openshell-core/src/paths.rs @@ -44,6 +44,22 @@ pub fn openshell_config_dir() -> Result { Ok(xdg_config_dir()?.join("openshell")) } +/// The directory name of a gateway under the `OpenShell` config directory. A +/// gateway reached by `--gateway-endpoint` alone is named by its URL, so +/// every character outside `[A-Za-z0-9._-]` becomes `_`. +pub fn gateway_dir_name(gateway: &str) -> String { + gateway + .chars() + .map(|ch| { + if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_') { + ch + } else { + '_' + } + }) + .collect() +} + /// Resolve the XDG state base directory. /// /// Returns `$XDG_STATE_HOME` if set, otherwise `$HOME/.local/state`. diff --git a/crates/openshell-core/src/ssh_host_key.rs b/crates/openshell-core/src/ssh_host_key.rs new file mode 100644 index 0000000000..9b86074ba3 --- /dev/null +++ b/crates/openshell-core/src/ssh_host_key.rs @@ -0,0 +1,1618 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! SSH host-key verification for sandbox connections. +//! +//! The fingerprint the gateway publishes is the authority. A per-sandbox +//! `known_hosts` file is a cache that must hash to that fingerprint, and `ssh` +//! enforces it with `StrictHostKeyChecking=yes`. This module owns the +//! fingerprint format, the pin location and file format, the `ssh` options, +//! the `ssh-proxy` flags, and the scan of the server's key exchange reply. + +use std::fmt; +use std::fs; +use std::io::{self, Write as _}; +use std::path::{Component, MAIN_SEPARATOR, Path, PathBuf}; +use std::str::FromStr; +use std::sync::atomic::{AtomicU64, Ordering}; + +use base64::Engine as _; +use base64::engine::general_purpose::{STANDARD, STANDARD_NO_PAD}; + +use crate::forward::shell_escape; +use crate::proto::CreateSshSessionResponse; + +const FINGERPRINT_PREFIX: &str = "SHA256:"; +const UNVERIFIED: &str = "unverified"; +const ALLOW_UNVERIFIED_ENV: &str = "OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY"; +const HOST_KEY_ALGORITHM: &str = "ssh-ed25519"; +const PIN_HEADER: &str = + "# Managed by OpenShell. Rewritten from the gateway-published fingerprint; do not edit.\n"; +/// `string "ssh-ed25519"`, then the length prefix of the 32-byte key. +const ED25519_BLOB_PREFIX: &[u8] = b"\0\0\0\x0bssh-ed25519\0\0\0\x20"; +const ED25519_KEY_LEN: usize = 32; +const MAX_BANNER_LINE_LEN: usize = 8192; +/// The padding length byte plus one payload byte, up to the largest packet +/// `ssh` accepts. +const PACKET_LENGTHS: std::ops::RangeInclusive = 2..=262_144; +const MSG_KEXINIT: u8 = 20; +const KEXINIT_COOKIE_LEN: usize = 16; +const KEXINIT_NAME_LISTS: usize = 10; +const MSG_NEWKEYS: u8 = 21; +/// The key exchange reply of every method but group exchange, where it is the +/// group instead. +const MSG_KEX_REPLY: u8 = 31; +const MSG_KEX_GEX_REPLY: u8 = 33; + +static TEMP_FILE_SEQUENCE: AtomicU64 = AtomicU64::new(0); + +/// SHA-256 of an SSH public key blob. Text form: `SHA256:` followed by 43 +/// characters of unpadded standard base64. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct HostKeyFingerprint([u8; 32]); + +impl FromStr for HostKeyFingerprint { + type Err = HostKeyError; + + fn from_str(s: &str) -> Result { + s.strip_prefix(FINGERPRINT_PREFIX) + .and_then(|encoded| STANDARD_NO_PAD.decode(encoded).ok()) + .and_then(|digest| <[u8; 32]>::try_from(digest).ok()) + .map(Self) + .ok_or(HostKeyError::BadFingerprint) + } +} + +impl fmt::Display for HostKeyFingerprint { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{FINGERPRINT_PREFIX}{}", STANDARD_NO_PAD.encode(self.0)) + } +} + +impl HostKeyFingerprint { + /// Hashes the blob exactly as it appears in the server's key exchange + /// reply and in a `known_hosts` line. + pub fn of_blob(blob: &[u8]) -> Result { + openshell_crypto::sha256(blob) + .map(Self) + .map_err(|err| HostKeyError::Crypto(err.to_string())) + } +} + +/// What `ssh-proxy` must see before it forwards the server's key exchange +/// reply. Text form: a fingerprint, or `unverified`. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum HostKeyExpectation { + Fingerprint(HostKeyFingerprint), + /// Only when the gateway published nothing and the user opted in. + AcceptAny, +} + +impl FromStr for HostKeyExpectation { + type Err = HostKeyError; + + fn from_str(s: &str) -> Result { + if s == UNVERIFIED { + Ok(Self::AcceptAny) + } else { + s.parse().map(Self::Fingerprint) + } + } +} + +impl fmt::Display for HostKeyExpectation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Fingerprint(fingerprint) => fingerprint.fmt(f), + Self::AcceptAny => f.write_str(UNVERIFIED), + } + } +} + +impl HostKeyExpectation { + /// Interprets the fingerprint field of a gateway response. The policy + /// decides only what an empty field means; it never changes the outcome + /// for a non-empty one. + pub fn from_published( + published: &str, + sandbox: &str, + policy: UnverifiedPolicy, + ) -> Result { + if !published.is_empty() { + return published.parse().map(Self::Fingerprint); + } + match policy { + UnverifiedPolicy::Allow => Ok(Self::AcceptAny), + UnverifiedPolicy::Refuse => Err(HostKeyError::Unpublished { + sandbox: sandbox.to_string(), + }), + } + } +} + +/// Whether a sandbox with no published fingerprint may be reached unverified. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum UnverifiedPolicy { + Refuse, + Allow, +} + +impl UnverifiedPolicy { + /// `Allow` only when `OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY` is `1`. + pub fn from_env() -> Self { + if std::env::var(ALLOW_UNVERIFIED_ENV).is_ok_and(|value| value == "1") { + Self::Allow + } else { + Self::Refuse + } + } +} + +#[derive(Debug, thiserror::Error)] +pub enum HostKeyError { + #[error( + "sandbox '{sandbox}' has no published SSH host key, so its SSH server cannot be verified.\n \ + If the gateway predates sandbox SSH identities, upgrade it. Otherwise stop and start the sandbox so the gateway assigns a key.\n \ + To connect without verification, set {ALLOW_UNVERIFIED_ENV}=1." + )] + Unpublished { sandbox: String }, + #[error("SSH host key fingerprint is not 'SHA256:' followed by 43 unpadded base64 characters")] + BadFingerprint, + #[error( + "SSH host key mismatch for sandbox '{sandbox}' (workspace '{workspace}')\n \ + gateway published: {expected}\n \ + server presented: {presented}\n\ + The SSH server reached through the gateway is not this sandbox's. The handshake was not forwarded.\n\ + Do not connect. Compare the published value with `openshell --workspace {} sandbox get {} --output json`, then stop and start the sandbox.\n\ + If the mismatch persists, contact the gateway operator.", + shell_escape(.workspace), + shell_escape(.sandbox) + )] + Mismatch { + sandbox: String, + workspace: String, + expected: HostKeyFingerprint, + presented: HostKeyFingerprint, + }, + #[error("malformed SSH stream from the sandbox: {0}")] + Malformed(&'static str), + #[error( + "SSH known-hosts path '{}' cannot be used: it must be absolute, be built from plain gateway, \ + sandbox and workspace names, and contain no '%', '\"', '\\', \"${{\" or newline", + .0.display() + )] + UnusablePath(PathBuf), + #[error("SSH known-hosts file: {0}")] + Io(#[from] io::Error), + #[error("SSH host key fingerprint could not be computed: {0}")] + Crypto(String), +} + +/// The sandbox an SSH connection is for. Keys the pin file. +#[derive(Clone, Copy, Debug)] +pub struct SandboxSshTarget<'a> { + pub gateway: &'a str, + pub workspace: &'a str, + pub sandbox: &'a str, +} + +/// `openshell-.`: the `Host` alias of a generated SSH +/// config block, the `HostKeyAlias` of every connection, and the host field of +/// the pin line. +pub fn host_key_alias(sandbox: &str, workspace: &str) -> String { + format!("openshell-{sandbox}.{workspace}") +} + +/// One `known_hosts` file holding at most one key line for one alias. +#[derive(Debug)] +pub struct KnownHostsPin { + path: PathBuf, + sandbox: String, + workspace: String, +} + +impl KnownHostsPin { + /// `/known_hosts//`, with the gateway + /// name flattened to one directory name. + pub fn for_target(target: &SandboxSshTarget<'_>) -> Result { + let config_dir = crate::paths::openshell_config_dir() + .map_err(|err| io::Error::other(err.to_string()))?; + Self::under(&std::path::absolute(config_dir)?, target) + } + + /// The `ssh-proxy` side: the path the `ssh` invocation or config block + /// handed over. + pub fn at(path: PathBuf, sandbox: &str, workspace: &str) -> Result { + // `ssh` expands `%` tokens and `${VAR}` in both UserKnownHostsFile and + // ProxyCommand, a quote or newline would end the value early, and a + // backslash escapes the character after it. Where the backslash + // separates path components, rejecting it would reject every path. + let expands_or_breaks = |text: &str| { + text.contains(['%', '"', '\n']) + || text.contains("${") + || (MAIN_SEPARATOR != '\\' && text.contains('\\')) + }; + // A relative path would let `ssh` and `ssh-proxy` resolve different + // files, and would make the working directory the pin directory. + let usable = path.is_absolute() + && path.file_name().is_some() + && path.to_str().is_some_and(|text| !expands_or_breaks(text)); + if usable { + Ok(Self { + path, + sandbox: sandbox.to_string(), + workspace: workspace.to_string(), + }) + } else { + Err(HostKeyError::UnusablePath(path)) + } + } + + fn under(config_dir: &Path, target: &SandboxSshTarget<'_>) -> Result { + let alias = host_key_alias(target.sandbox, target.workspace); + let gateway = crate::paths::gateway_dir_name(target.gateway); + let path = config_dir.join("known_hosts").join(&gateway).join(&alias); + if !is_single_normal_component(&gateway) || !is_single_normal_component(&alias) { + return Err(HostKeyError::UnusablePath(path)); + } + Self::at(path, target.sandbox, target.workspace) + } + + fn alias(&self) -> String { + host_key_alias(&self.sandbox, &self.workspace) + } + + pub fn path(&self) -> &Path { + &self.path + } + + /// The options of every sandbox SSH connection that decide which host key + /// it trusts, in the order they are passed. Rendered as `-o key=value` on + /// a command line and as `key value` lines in a config block. + pub fn ssh_options(&self) -> Vec<(&'static str, String)> { + vec![ + ("StrictHostKeyChecking", "yes".to_string()), + // Quoted, so that no `ssh` release splits the path at whitespace, + // `=` or an apostrophe. + ("UserKnownHostsFile", format!("\"{}\"", self.path.display())), + ("GlobalKnownHostsFile", "/dev/null".to_string()), + ("HostKeyAlias", self.alias()), + ("HostKeyAlgorithms", HOST_KEY_ALGORITHM.to_string()), + ("VerifyHostKeyDNS", "no".to_string()), + ("UpdateHostKeys", "no".to_string()), + // A shared connection is reused without running the ProxyCommand, + // so its host key is not checked. + ("ControlMaster", "no".to_string()), + ("ControlPath", "none".to_string()), + ("ControlPersist", "no".to_string()), + ] + } + + /// Rewrites the file header-only when it holds anything other than one key + /// hashing to `fingerprint`. With no fingerprint, no key is kept. + fn forget_unless(&self, fingerprint: Option<&HostKeyFingerprint>) -> io::Result<()> { + // Decided from one read: a second read could see the key another + // connection stored in between, and the rewrite would erase it. + let trusts_nothing_else = self.read()?.is_some_and(|content| { + content == PIN_HEADER.as_bytes() + || self + .key_blob(&content) + .zip(fingerprint) + .is_some_and(|(blob, fingerprint)| { + HostKeyFingerprint::of_blob(&blob).is_ok_and(|kept| kept == *fingerprint) + }) + }); + if trusts_nothing_else { + Ok(()) + } else { + self.replace(PIN_HEADER) + } + } + + /// No-op when the content is already identical; otherwise a temp file + /// (0600) in the same directory, then a rename. + fn store(&self, blob: &[u8]) -> io::Result<()> { + let content = format!("{}{}\n", self.key_line_prefix(), STANDARD.encode(blob)); + if self.read()?.is_some_and(|kept| kept == content.as_bytes()) { + return Ok(()); + } + self.replace(&content) + } + + fn key_line_prefix(&self) -> String { + format!("{PIN_HEADER}{} {HOST_KEY_ALGORITHM} ", self.alias()) + } + + /// The key blob, when `content` is exactly what [`Self::store`] writes. + fn key_blob(&self, content: &[u8]) -> Option> { + content + .strip_prefix(self.key_line_prefix().as_bytes()) + .and_then(|rest| rest.strip_suffix(b"\n")) + .and_then(|encoded| STANDARD.decode(encoded).ok()) + } + + fn read(&self) -> io::Result>> { + match fs::read(&self.path) { + Ok(content) => Ok(Some(content)), + Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(None), + Err(err) => Err(self.with_path(&err)), + } + } + + fn replace(&self, content: &str) -> io::Result<()> { + self.write_then_rename(content) + .map_err(|err| self.with_path(&err)) + } + + fn write_then_rename(&self, content: &str) -> io::Result<()> { + let (Some(dir), Some(name)) = (self.path.parent(), self.path.file_name()) else { + return Err(io::ErrorKind::InvalidInput.into()); + }; + create_owner_only_dir(dir)?; + let (temp_path, mut temp_file) = loop { + let sequence = TEMP_FILE_SEQUENCE.fetch_add(1, Ordering::Relaxed); + let candidate = dir.join(format!( + ".{}.{}.{sequence}.tmp", + name.to_string_lossy(), + std::process::id() + )); + match owner_only_create_new().open(&candidate) { + Ok(file) => break (candidate, file), + Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {} + Err(err) => return Err(err), + } + }; + let written = temp_file.write_all(content.as_bytes()); + drop(temp_file); + let replaced = written.and_then(|()| fs::rename(&temp_path, &self.path)); + if replaced.is_err() { + let _ = fs::remove_file(&temp_path); + } + replaced + } + + fn with_path(&self, err: &io::Error) -> io::Error { + io::Error::new(err.kind(), format!("{}: {err}", self.path.display())) + } +} + +fn is_single_normal_component(name: &str) -> bool { + let mut components = Path::new(name).components(); + matches!( + (components.next(), components.next()), + (Some(Component::Normal(component)), None) if component.to_str() == Some(name) + ) +} + +fn owner_only_create_new() -> fs::OpenOptions { + let mut options = fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt as _; + options.mode(0o600); + } + options +} + +fn create_owner_only_dir(dir: &Path) -> io::Result<()> { + let mut builder = fs::DirBuilder::new(); + builder.recursive(true); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt as _; + builder.mode(0o700); + } + builder.create(dir) +} + +/// The host-key decision for one SSH connection. +#[derive(Debug)] +pub struct HostKeyCheck { + expectation: HostKeyExpectation, + pin: KnownHostsPin, +} + +impl HostKeyCheck { + /// For the CLI and TUI, before `ssh` is spawned. Leaves the pin holding + /// nothing `ssh` could trust except the key the gateway published. + pub fn for_session( + session: &CreateSshSessionResponse, + target: &SandboxSshTarget<'_>, + policy: UnverifiedPolicy, + ) -> Result { + let expectation = HostKeyExpectation::from_published( + &session.host_key_fingerprint, + target.sandbox, + policy, + )?; + Self::for_relay(expectation, KnownHostsPin::for_target(target)?) + } + + /// For `ssh-proxy`, before the first byte is relayed. + pub fn for_relay( + expectation: HostKeyExpectation, + pin: KnownHostsPin, + ) -> Result { + pin.forget_unless(match &expectation { + HostKeyExpectation::Fingerprint(fingerprint) => Some(fingerprint), + HostKeyExpectation::AcceptAny => None, + })?; + Ok(Self { expectation, pin }) + } + + #[must_use] + pub fn unverified_warning(&self, sandbox: &str) -> Option { + matches!(self.expectation, HostKeyExpectation::AcceptAny).then(|| { + format!( + "warning: SSH host key of sandbox '{sandbox}' is NOT verified (the gateway published no fingerprint)" + ) + }) + } + + pub(crate) fn ssh_options(&self) -> Vec<(&'static str, String)> { + self.pin.ssh_options() + } + + /// `["--host-key", , "--known-hosts", ]` + pub(crate) fn proxy_flags(&self) -> [String; 4] { + [ + "--host-key".to_string(), + self.expectation.to_string(), + "--known-hosts".to_string(), + self.pin.path.display().to_string(), + ] + } + + pub fn into_gate(self) -> HostKeyGate { + HostKeyGate { + check: self, + phase: GatePhase::Banner, + buffer: Vec::new(), + } + } + + /// Pins `blob`, unless a fingerprint is expected and `blob` does not hash + /// to it. + fn admit(&self, blob: &[u8]) -> Result<(), HostKeyError> { + if let HostKeyExpectation::Fingerprint(expected) = self.expectation { + let presented = HostKeyFingerprint::of_blob(blob)?; + if presented != expected { + return Err(HostKeyError::Mismatch { + sandbox: self.pin.sandbox.clone(), + workspace: self.pin.workspace.clone(), + expected, + presented, + }); + } + } + self.pin.store(blob)?; + Ok(()) + } + + /// The phase after one cleartext packet from the server. Every packet + /// outside the one accepted order is refused. + fn phase_after( + &self, + awaited: GatePhase, + message: u8, + fields: &[u8], + ) -> Result { + let host_key = matches!(message, MSG_KEX_REPLY | MSG_KEX_GEX_REPLY) + .then(|| ed25519_host_key(fields)) + .flatten(); + match (awaited, message, host_key) { + (GatePhase::KexInit, MSG_KEXINIT, _) if first_kex_packet_follows(fields) == Some(0) => { + Ok(GatePhase::GroupOrReply) + } + (GatePhase::GroupOrReply | GatePhase::Reply, _, Some(blob)) => { + self.admit(blob)?; + Ok(GatePhase::NewKeys) + } + (GatePhase::GroupOrReply, MSG_KEX_REPLY, None) => Ok(GatePhase::Reply), + (GatePhase::NewKeys, MSG_NEWKEYS, _) => Ok(GatePhase::Done), + (GatePhase::GroupOrReply | GatePhase::Reply, MSG_NEWKEYS, _) => Err( + HostKeyError::Malformed("key exchange finished without an Ed25519 host key"), + ), + _ => Err(HostKeyError::Malformed( + "packet outside the key exchange order", + )), + } + } +} + +/// The server-to-client side of the `ssh-proxy` relay. Holds back the server's +/// key exchange reply until the host key in it is checked and pinned. +#[derive(Debug)] +pub struct HostKeyGate { + check: HostKeyCheck, + phase: GatePhase, + /// Bytes not yet released. Its head is the line or packet being decided. + buffer: Vec, +} + +/// What the gate waits for: the identification line, then the cleartext +/// packets of a key exchange in the only order the server may send them. `ssh` +/// can discard a packet outside this order, so a gate that let one through +/// could be shown one host key while `ssh` acts on another. +#[derive(Clone, Copy, Debug)] +enum GatePhase { + Banner, + KexInit, + /// The reply, or before it the group of a group exchange. + GroupOrReply, + Reply, + NewKeys, + Done, +} + +impl HostKeyGate { + /// Feed bytes read from the gateway; returns the bytes that may be written + /// to `ssh` now. Lines are released whole up to the `SSH-` identification + /// line. Then exactly these packets are released whole, in this order: + /// KEXINIT, the group of a group exchange if there is one, the key + /// exchange reply once its Ed25519 host key is pinned, and NEWKEYS. + /// Everything after NEWKEYS passes through. + /// + /// On an error nothing from this call is released, and every later call + /// fails on the same line or packet. + pub fn push(&mut self, chunk: &[u8]) -> Result, HostKeyError> { + self.buffer.extend_from_slice(chunk); + let mut released = Vec::new(); + while let Some(len) = self.releasable()? { + released.extend(self.buffer.drain(..len)); + } + Ok(released) + } + + /// The length of the buffer's head that may be released, or `None` when + /// deciding needs more bytes. An error leaves the head in place. + fn releasable(&mut self) -> Result, HostKeyError> { + match self.phase { + GatePhase::Banner => { + let head = &self.buffer[..self.buffer.len().min(MAX_BANNER_LINE_LEN)]; + let Some(newline) = head.iter().position(|&byte| byte == b'\n') else { + return if head.len() == MAX_BANNER_LINE_LEN { + Err(HostKeyError::Malformed("identification line is too long")) + } else { + Ok(None) + }; + }; + if head.starts_with(b"SSH-") { + self.phase = GatePhase::KexInit; + } + Ok(Some(newline + 1)) + } + GatePhase::Done => Ok((!self.buffer.is_empty()).then_some(self.buffer.len())), + awaited => { + let Some((length, rest)) = self.buffer.split_first_chunk::<4>() else { + return Ok(None); + }; + let length = usize::try_from(u32::from_be_bytes(*length)) + .ok() + .filter(|length| PACKET_LENGTHS.contains(length)) + .ok_or(HostKeyError::Malformed("packet length is out of range"))?; + let Some(packet) = rest.get(..length) else { + return Ok(None); + }; + let padding = usize::from(packet[0]); + let (message, fields) = packet + .get(1..length.saturating_sub(padding)) + .and_then(<[u8]>::split_first) + .ok_or(HostKeyError::Malformed("packet padding leaves no payload"))?; + self.phase = self.check.phase_after(awaited, *message, fields)?; + Ok(Some(4 + length)) + } + } + } +} + +/// The boolean that follows the cookie and the ten name-lists of a KEXINIT. +fn first_kex_packet_follows(fields: &[u8]) -> Option { + let mut rest = fields.get(KEXINIT_COOKIE_LEN..)?; + for _ in 0..KEXINIT_NAME_LISTS { + let (length, after_length) = rest.split_first_chunk::<4>()?; + rest = after_length.get(usize::try_from(u32::from_be_bytes(*length)).ok()?..)?; + } + rest.first().copied() +} + +/// The first string of a key exchange reply, when it is exactly an Ed25519 +/// public key blob. +fn ed25519_host_key(fields: &[u8]) -> Option<&[u8]> { + let (length, rest) = fields.split_first_chunk::<4>()?; + let blob = rest.get(..usize::try_from(u32::from_be_bytes(*length)).ok()?)?; + let key = blob.strip_prefix(ED25519_BLOB_PREFIX)?; + (key.len() == ED25519_KEY_LEN).then_some(blob) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_environment::{Environment, fixture_dir}; + + const KEY_A: [u8; 32] = [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, + ]; + const FINGERPRINT_A: &str = "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA"; + const KNOWN_HOSTS_KEY_A: &str = + "AAAAC3NzaC1lZDI1NTE5AAAAIAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4f"; + const KEY_B: [u8; 32] = [ + 255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, + 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224, + ]; + const FINGERPRINT_B: &str = "SHA256:LIHRKjP8oeafKrX0BHkvTYI/ZcB3tRHZHr9uyEhysws"; + const KNOWN_HOSTS_KEY_B: &str = + "AAAAC3NzaC1lZDI1NTE5AAAAIP/+/fz7+vn49/b19PPy8fDv7u3s6+rp6Ofm5eTj4uHg"; + + const TARGET: SandboxSshTarget<'static> = SandboxSshTarget { + gateway: "prod", + workspace: "default", + sandbox: "my-sb", + }; + + fn ed25519_blob(key: [u8; 32]) -> Vec { + let mut blob = Vec::new(); + for field in [HOST_KEY_ALGORITHM.as_bytes(), &key] { + blob.extend_from_slice(&u32::try_from(field.len()).unwrap().to_be_bytes()); + blob.extend_from_slice(field); + } + blob + } + + fn fingerprint(text: &str) -> HostKeyFingerprint { + text.parse().unwrap() + } + + fn pin_in(config_dir: &Path) -> KnownHostsPin { + KnownHostsPin::under(config_dir, &TARGET).unwrap() + } + + fn pin_file(key: &str) -> String { + format!("{PIN_HEADER}openshell-my-sb.default ssh-ed25519 {key}\n") + } + + fn published(fingerprint: &str) -> CreateSshSessionResponse { + CreateSshSessionResponse { + host_key_fingerprint: fingerprint.to_string(), + ..Default::default() + } + } + + fn with_config_dir(assertions: impl FnOnce(&Path)) { + let config_home = fixture_dir().unwrap(); + Environment::new() + .set("XDG_CONFIG_HOME", config_home.path()) + .run(|| assertions(&config_home.path().join("openshell"))); + } + + const BANNER: &[u8] = b"SSH-2.0-OpenShell_0.0.0\r\n"; + /// Stands in for the encrypted stream: read as a packet, its length is out + /// of range. + const ENCRYPTED: [u8; 48] = [0xff; 48]; + + fn string(bytes: &[u8]) -> Vec { + [&u32::try_from(bytes.len()).unwrap().to_be_bytes(), bytes].concat() + } + + fn packet(payload: &[u8]) -> Vec { + let padding = 4 + (8 - (5 + payload.len() + 4) % 8) % 8; + let mut packet = u32::try_from(1 + payload.len() + padding) + .unwrap() + .to_be_bytes() + .to_vec(); + packet.push(u8::try_from(padding).unwrap()); + packet.extend_from_slice(payload); + packet.resize(packet.len() + padding, 0); + packet + } + + fn kexinit() -> Vec { + kexinit_ending(&[0; 5]) + } + + /// A KEXINIT whose ten name-lists are followed by `ending`: normally + /// `first_kex_packet_follows` and the reserved word. + fn kexinit_ending(ending: &[u8]) -> Vec { + let mut payload = vec![20]; + payload.extend_from_slice(&[0x11; 16]); + for length in [17, 11, 29, 29, 13, 13, 4, 4, 0, 0] { + payload.extend(string(&vec![b'a'; length])); + } + payload.extend_from_slice(ending); + packet(&payload) + } + + fn group() -> Vec { + let prime = [&[0][..], &[0xff; 256]].concat(); + packet(&[vec![31], string(&prime), string(&[2])].concat()) + } + + fn kex_reply(message: u8, host_key: &[u8]) -> Vec { + let signature = [string(b"ssh-ed25519"), string(&[0x5a; 64])].concat(); + packet( + &[ + vec![message], + string(host_key), + string(&[0x42; 32]), + string(&signature), + ] + .concat(), + ) + } + + fn newkeys() -> Vec { + packet(&[21]) + } + + /// What the server sends before its reply, the reply, and what follows it. + fn curve25519_exchange(host_key: [u8; 32]) -> [Vec; 3] { + [ + [BANNER, &kexinit()].concat(), + kex_reply(31, &ed25519_blob(host_key)), + [newkeys(), ENCRYPTED.to_vec()].concat(), + ] + } + + fn group_exchange(host_key: [u8; 32]) -> [Vec; 3] { + [ + [BANNER, &kexinit(), &group()].concat(), + kex_reply(33, &ed25519_blob(host_key)), + [newkeys(), ENCRYPTED.to_vec()].concat(), + ] + } + + fn gate_in(config_dir: &Path, expectation: &str) -> HostKeyGate { + HostKeyCheck::for_relay(expectation.parse().unwrap(), pin_in(config_dir)) + .unwrap() + .into_gate() + } + + struct Relayed { + released: Vec, + result: Result<(), HostKeyError>, + pin: Option, + } + + fn relay<'a>(expectation: &str, chunks: impl IntoIterator) -> Relayed { + let config_dir = tempfile::tempdir().unwrap(); + let mut gate = gate_in(config_dir.path(), expectation); + let mut released = Vec::new(); + let result = chunks.into_iter().try_for_each(|chunk| { + released.extend(gate.push(chunk)?); + Ok(()) + }); + Relayed { + released, + result, + pin: fs::read_to_string(pin_in(config_dir.path()).path()).ok(), + } + } + + /// Every way to deliver `stream` in two chunks, then one byte at a time. + fn every_split(stream: &[u8]) -> impl Iterator> { + (0..=stream.len()) + .map(|at| vec![&stream[..at], &stream[at..]]) + .chain([stream.chunks(1).collect()]) + } + + #[test] + fn fingerprint_of_blob_matches_fixed_vector() { + let computed = HostKeyFingerprint::of_blob(&ed25519_blob(KEY_A)).unwrap(); + assert_eq!(computed.to_string(), FINGERPRINT_A); + assert_eq!(fingerprint(FINGERPRINT_A), computed); + assert_ne!(fingerprint(FINGERPRINT_B), computed); + } + + #[test] + fn fingerprint_rejects_every_other_text_form() { + for text in [ + "", + "SHA256:", + "ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA", + "sha256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA", + "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA=", + "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncC", + "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCAA", + "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCB", + "SHA256:ZkAslGjFiUHdGf_WUL8rQvkib4PTvQatUV0OUQSncCA", + "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA\n", + " SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA", + "MD5:b4:f6:6e:5f:3c:0a:9d:11:27:35:5d:42:8e:0c:71:aa", + ] { + assert!( + matches!( + text.parse::(), + Err(HostKeyError::BadFingerprint) + ), + "{text:?}" + ); + } + } + + #[test] + fn expectation_text_round_trips() { + let verified: HostKeyExpectation = FINGERPRINT_A.parse().unwrap(); + assert_eq!( + verified, + HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)) + ); + assert_eq!(verified.to_string(), FINGERPRINT_A); + + let unverified: HostKeyExpectation = "unverified".parse().unwrap(); + assert_eq!(unverified, HostKeyExpectation::AcceptAny); + assert_eq!(unverified.to_string(), "unverified"); + + for text in ["", "Unverified", "any", "SHA256:short"] { + assert!( + matches!( + text.parse::(), + Err(HostKeyError::BadFingerprint) + ), + "{text:?}" + ); + } + } + + #[test] + fn expectation_policy_never_changes_a_published_outcome() { + for policy in [UnverifiedPolicy::Refuse, UnverifiedPolicy::Allow] { + assert_eq!( + HostKeyExpectation::from_published(FINGERPRINT_A, "my-sb", policy).unwrap(), + HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)), + "{policy:?}" + ); + for malformed in ["unverified", "SHA256:inconsistent", "MD5:00:11"] { + assert!( + matches!( + HostKeyExpectation::from_published(malformed, "my-sb", policy), + Err(HostKeyError::BadFingerprint) + ), + "{malformed:?} {policy:?}" + ); + } + } + } + + #[test] + fn expectation_for_an_unpublished_key_follows_the_policy() { + assert_eq!( + HostKeyExpectation::from_published("", "my-sb", UnverifiedPolicy::Allow).unwrap(), + HostKeyExpectation::AcceptAny + ); + + let refused = + HostKeyExpectation::from_published("", "my-sb", UnverifiedPolicy::Refuse).unwrap_err(); + assert!(matches!( + &refused, + HostKeyError::Unpublished { sandbox } if sandbox == "my-sb" + )); + assert_eq!( + refused.to_string(), + "sandbox 'my-sb' has no published SSH host key, so its SSH server cannot be verified.\n \ + If the gateway predates sandbox SSH identities, upgrade it. Otherwise stop and start the sandbox so the gateway assigns a key.\n \ + To connect without verification, set OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY=1." + ); + } + + #[test] + fn unverified_policy_allows_only_when_the_variable_is_one() { + Environment::new() + .set(ALLOW_UNVERIFIED_ENV, "1") + .run(|| assert_eq!(UnverifiedPolicy::from_env(), UnverifiedPolicy::Allow)); + for value in ["", "0", "true", "yes", "11"] { + Environment::new() + .set(ALLOW_UNVERIFIED_ENV, value) + .run(|| assert_eq!(UnverifiedPolicy::from_env(), UnverifiedPolicy::Refuse)); + } + Environment::new() + .remove(ALLOW_UNVERIFIED_ENV) + .run(|| assert_eq!(UnverifiedPolicy::from_env(), UnverifiedPolicy::Refuse)); + } + + #[test] + fn host_key_alias_names_sandbox_then_workspace() { + assert_eq!(host_key_alias("demo", "default"), "openshell-demo.default"); + assert_eq!(host_key_alias("demo", "beta"), "openshell-demo.beta"); + } + + #[test] + fn pin_for_target_lives_under_the_config_dir() { + with_config_dir(|config_dir| { + let pin = KnownHostsPin::for_target(&TARGET).unwrap(); + assert_eq!( + pin.path(), + config_dir.join("known_hosts/prod/openshell-my-sb.default") + ); + }); + } + + #[test] + fn pin_for_target_resolves_a_relative_config_dir_against_the_working_directory() { + Environment::new() + .set("XDG_CONFIG_HOME", "relative/.config") + .run(|| { + let pin = KnownHostsPin::for_target(&TARGET).unwrap(); + assert_eq!( + pin.path(), + std::env::current_dir().unwrap().join( + "relative/.config/openshell/known_hosts/prod/openshell-my-sb.default" + ) + ); + }); + } + + #[test] + fn pin_for_a_gateway_named_by_its_endpoint_lives_in_one_directory() { + let config_dir = Path::new("/home/u/.config/openshell"); + for (gateway, directory) in [ + ("http://127.0.0.1:18080", "http___127.0.0.1_18080"), + ("https://gw.example.com/a b%", "https___gw.example.com_a_b_"), + ("../../etc", ".._.._etc"), + ] { + let target = SandboxSshTarget { gateway, ..TARGET }; + assert_eq!( + KnownHostsPin::under(config_dir, &target).unwrap().path(), + config_dir + .join("known_hosts") + .join(directory) + .join("openshell-my-sb.default"), + "gateway {gateway:?}" + ); + } + } + + #[test] + fn pin_rejects_names_that_are_not_one_path_component() { + let config_dir = Path::new("/home/u/.config/openshell"); + for gateway in ["", ".", ".."] { + let target = SandboxSshTarget { gateway, ..TARGET }; + assert!( + matches!( + KnownHostsPin::under(config_dir, &target), + Err(HostKeyError::UnusablePath(_)) + ), + "gateway {gateway:?}" + ); + } + for (sandbox, workspace) in [("a/b", "default"), ("my-sb", "../x"), ("my-sb", "x/")] { + let target = SandboxSshTarget { + workspace, + sandbox, + ..TARGET + }; + assert!( + matches!( + KnownHostsPin::under(config_dir, &target), + Err(HostKeyError::UnusablePath(_)) + ), + "sandbox {sandbox:?} workspace {workspace:?}" + ); + } + } + + #[test] + fn pin_rejects_paths_ssh_would_expand_or_split() { + for config_dir in [ + "/home/100%/openshell", + "/home/a\"b/openshell", + "/home/${HOME}/openshell", + "/home/a\nb/openshell", + #[cfg(unix)] + "/home/a\\b/openshell", + "relative/openshell", + ] { + assert!( + matches!( + KnownHostsPin::under(Path::new(config_dir), &TARGET), + Err(HostKeyError::UnusablePath(_)) + ), + "{config_dir:?}" + ); + } + for path in ["/tmp/pin%h", "pin", "", "/", "/tmp/.."] { + assert!( + matches!( + KnownHostsPin::at(PathBuf::from(path), "my-sb", "default"), + Err(HostKeyError::UnusablePath(_)) + ), + "{path:?}" + ); + } + } + + #[test] + fn pin_store_writes_the_header_and_one_key_line() { + let config_dir = tempfile::tempdir().unwrap(); + let pin = pin_in(config_dir.path()); + + pin.store(&ed25519_blob(KEY_A)).unwrap(); + + assert_eq!( + fs::read_to_string(pin.path()).unwrap(), + format!( + "# Managed by OpenShell. Rewritten from the gateway-published fingerprint; do not edit.\n\ + openshell-my-sb.default ssh-ed25519 {KNOWN_HOSTS_KEY_A}\n" + ) + ); + } + + #[cfg(unix)] + #[test] + fn pin_file_is_owner_only_in_the_owner_only_directory_it_creates() { + use std::os::unix::fs::PermissionsExt as _; + let config_dir = tempfile::tempdir().unwrap(); + let pin = pin_in(config_dir.path()); + let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777; + + pin.store(&ed25519_blob(KEY_A)).unwrap(); + assert_eq!(mode(pin.path()), 0o600); + assert_eq!(mode(pin.path().parent().unwrap()), 0o700); + + fs::set_permissions(pin.path(), fs::Permissions::from_mode(0o644)).unwrap(); + pin.store(&ed25519_blob(KEY_B)).unwrap(); + assert_eq!(mode(pin.path()), 0o600); + } + + #[cfg(unix)] + #[test] + fn pin_store_leaves_the_mode_of_an_existing_directory_alone() { + use std::os::unix::fs::PermissionsExt as _; + let shared = tempfile::tempdir().unwrap(); + fs::set_permissions(shared.path(), fs::Permissions::from_mode(0o755)).unwrap(); + let pin = KnownHostsPin::at(shared.path().join("pin"), "my-sb", "default").unwrap(); + + pin.store(&ed25519_blob(KEY_A)).unwrap(); + + let mode = fs::metadata(shared.path()).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o755); + } + + #[cfg(unix)] + #[test] + fn pin_store_replaces_the_file_only_when_the_key_differs() { + use std::os::unix::fs::MetadataExt as _; + let config_dir = tempfile::tempdir().unwrap(); + let pin = pin_in(config_dir.path()); + let inode = || fs::metadata(pin.path()).unwrap().ino(); + + pin.store(&ed25519_blob(KEY_A)).unwrap(); + let first = inode(); + pin.store(&ed25519_blob(KEY_A)).unwrap(); + assert_eq!(inode(), first); + + pin.store(&ed25519_blob(KEY_B)).unwrap(); + assert_ne!(inode(), first); + assert_eq!( + fs::read_to_string(pin.path()).unwrap(), + pin_file(KNOWN_HOSTS_KEY_B) + ); + let entries: Vec<_> = fs::read_dir(pin.path().parent().unwrap()) + .unwrap() + .map(|entry| entry.unwrap().file_name()) + .collect(); + assert_eq!(entries, ["openshell-my-sb.default"]); + } + + #[test] + fn pin_drops_anything_that_is_not_exactly_one_matching_key() { + let matching = pin_file(KNOWN_HOSTS_KEY_A); + let other_line = format!("openshell-my-sb.default ssh-ed25519 {KNOWN_HOSTS_KEY_B}\n"); + for content in [ + String::new(), + "not a known_hosts file".to_string(), + format!("{matching}{other_line}"), + format!("{PIN_HEADER}{other_line}{}", &matching[PIN_HEADER.len()..]), + format!("{PIN_HEADER}* ssh-ed25519 {KNOWN_HOSTS_KEY_A}\n"), + format!("{PIN_HEADER}openshell-other.default ssh-ed25519 {KNOWN_HOSTS_KEY_A}\n"), + format!( + "{PIN_HEADER}@cert-authority openshell-my-sb.default ssh-ed25519 {KNOWN_HOSTS_KEY_A}\n" + ), + format!("{PIN_HEADER}openshell-my-sb.default ssh-ed25519 not-base64\n"), + matching[PIN_HEADER.len()..].to_string(), + matching.trim_end().to_string(), + ] { + let config_dir = tempfile::tempdir().unwrap(); + let pin = pin_in(config_dir.path()); + fs::create_dir_all(pin.path().parent().unwrap()).unwrap(); + fs::write(pin.path(), &content).unwrap(); + + pin.forget_unless(Some(&fingerprint(FINGERPRINT_A))) + .unwrap(); + + assert_eq!( + fs::read_to_string(pin.path()).unwrap(), + PIN_HEADER, + "{content:?}" + ); + } + } + + #[test] + fn check_for_session_drops_a_stale_key_before_ssh_can_read_it() { + with_config_dir(|config_dir| { + let pin = pin_in(config_dir); + pin.store(&ed25519_blob(KEY_B)).unwrap(); + + let check = HostKeyCheck::for_session( + &published(FINGERPRINT_A), + &TARGET, + UnverifiedPolicy::Refuse, + ) + .unwrap(); + + assert_eq!(fs::read_to_string(pin.path()).unwrap(), PIN_HEADER); + assert_eq!(check.unverified_warning("my-sb"), None); + let path = pin.path().display().to_string(); + assert_eq!( + check.proxy_flags(), + ["--host-key", FINGERPRINT_A, "--known-hosts", path.as_str()] + ); + assert_eq!(check.ssh_options(), pin.ssh_options()); + }); + } + + #[test] + fn check_for_session_keeps_the_published_key() { + with_config_dir(|config_dir| { + let pin = pin_in(config_dir); + pin.store(&ed25519_blob(KEY_A)).unwrap(); + + for policy in [UnverifiedPolicy::Refuse, UnverifiedPolicy::Allow] { + let check = + HostKeyCheck::for_session(&published(FINGERPRINT_A), &TARGET, policy).unwrap(); + assert_eq!(check.unverified_warning("my-sb"), None); + assert_eq!( + fs::read_to_string(pin.path()).unwrap(), + pin_file(KNOWN_HOSTS_KEY_A) + ); + } + }); + } + + #[test] + fn check_for_session_refuses_an_unpublished_key_without_touching_the_pin() { + with_config_dir(|config_dir| { + let error = + HostKeyCheck::for_session(&published(""), &TARGET, UnverifiedPolicy::Refuse) + .unwrap_err(); + + assert!(matches!(error, HostKeyError::Unpublished { .. })); + assert!(!config_dir.exists()); + }); + } + + #[test] + fn check_for_session_never_trusts_a_cached_pin_when_unverified() { + with_config_dir(|config_dir| { + let pin = pin_in(config_dir); + pin.store(&ed25519_blob(KEY_A)).unwrap(); + + let check = HostKeyCheck::for_session(&published(""), &TARGET, UnverifiedPolicy::Allow) + .unwrap(); + + assert_eq!(fs::read_to_string(pin.path()).unwrap(), PIN_HEADER); + assert_eq!( + check.unverified_warning("my-sb").unwrap(), + "warning: SSH host key of sandbox 'my-sb' is NOT verified (the gateway published no fingerprint)" + ); + assert_eq!(check.proxy_flags()[..2], ["--host-key", "unverified"]); + }); + } + + #[test] + fn check_for_relay_drops_a_stale_key_before_the_first_byte() { + let config_dir = tempfile::tempdir().unwrap(); + let path = config_dir.path().join("known_hosts/prod/pin"); + let stale = KnownHostsPin::at(path.clone(), "my-sb", "default").unwrap(); + stale.store(&ed25519_blob(KEY_B)).unwrap(); + + let check = HostKeyCheck::for_relay( + HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)), + stale, + ) + .unwrap(); + + assert_eq!(fs::read_to_string(&path).unwrap(), PIN_HEADER); + assert_eq!(check.unverified_warning("my-sb"), None); + } + + /// Other connections to the sandbox start while one stores the key and + /// hands the reply to `ssh`, which must then find the key in the pin. + #[test] + fn check_for_relay_keeps_the_key_another_connection_just_stored() { + use std::sync::atomic::{AtomicBool, Ordering}; + + struct StopOnDrop<'a>(&'a AtomicBool); + impl Drop for StopOnDrop<'_> { + fn drop(&mut self) { + self.0.store(true, Ordering::Relaxed); + } + } + + let config_dir = tempfile::tempdir().unwrap(); + let pin = pin_in(config_dir.path()); + let stream = curve25519_exchange(KEY_A).concat(); + let stop = AtomicBool::new(false); + pin.forget_unless(None).unwrap(); + + std::thread::scope(|scope| { + let _stop_others = StopOnDrop(&stop); + for _ in 0..4 { + scope.spawn(|| { + while !stop.load(Ordering::Relaxed) { + gate_in(config_dir.path(), FINGERPRINT_A); + } + }); + } + for connection in 0..500 { + pin.forget_unless(None).unwrap(); + let mut gate = gate_in(config_dir.path(), FINGERPRINT_A); + + assert_eq!(gate.push(&stream).unwrap(), stream, "{connection}"); + assert_eq!( + fs::read_to_string(pin.path()).unwrap(), + pin_file(KNOWN_HOSTS_KEY_A), + "{connection}" + ); + } + }); + } + + #[test] + fn check_reports_a_pin_directory_that_cannot_be_created() { + let config_dir = tempfile::tempdir().unwrap(); + let blocker = config_dir.path().join("known_hosts"); + fs::write(&blocker, "a file where the directory should be").unwrap(); + let pin = KnownHostsPin::at(blocker.join("prod/pin"), "my-sb", "default").unwrap(); + + let error = HostKeyCheck::for_relay( + HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)), + pin, + ) + .unwrap_err(); + + assert!(matches!(error, HostKeyError::Io(_))); + assert!( + error.to_string().contains("known_hosts/prod/pin"), + "{error}" + ); + } + + /// Asserts that `stream` is refused as `is_expected` says, that nothing + /// past `before` is released and that the pin file is left as `pin`, + /// however `stream` is split. + fn assert_refused( + name: &str, + expectation: &str, + before: &[u8], + stream: &[u8], + is_expected: impl Fn(&HostKeyError) -> bool, + pin: &str, + ) { + for chunks in every_split(stream) { + let at = chunks[0].len(); + let relayed = relay(expectation, chunks); + + assert!( + relayed.result.as_ref().is_err_and(&is_expected), + "{name} split at {at}: {:?}", + relayed.result + ); + assert!( + before.starts_with(&relayed.released), + "{name} split at {at}" + ); + assert_eq!(relayed.pin.as_deref(), Some(pin), "{name} split at {at}"); + } + } + + #[test] + fn gate_relays_an_exchange_unchanged_and_pins_its_key() { + let greeting = b"Welcome\r\nnotice: maintenance at noon\n"; + for (name, expectation, stream, key) in [ + ( + "curve25519", + FINGERPRINT_A, + curve25519_exchange(KEY_A).concat(), + KNOWN_HOSTS_KEY_A, + ), + ( + "group exchange", + FINGERPRINT_A, + group_exchange(KEY_A).concat(), + KNOWN_HOSTS_KEY_A, + ), + ( + "unverified", + "unverified", + curve25519_exchange(KEY_B).concat(), + KNOWN_HOSTS_KEY_B, + ), + ( + "lines before the identification line", + FINGERPRINT_A, + [greeting, curve25519_exchange(KEY_A).concat().as_slice()].concat(), + KNOWN_HOSTS_KEY_A, + ), + ] { + for chunks in every_split(&stream) { + let at = chunks[0].len(); + let relayed = relay(expectation, chunks); + + assert!(relayed.result.is_ok(), "{name} split at {at}"); + assert_eq!(relayed.released, stream, "{name} split at {at}"); + assert_eq!(relayed.pin, Some(pin_file(key)), "{name} split at {at}"); + } + } + } + + #[test] + fn gate_holds_the_reply_until_its_key_is_pinned() { + let [before, reply, after] = curve25519_exchange(KEY_A); + let (reply_head, reply_last_byte) = reply.split_at(reply.len() - 1); + let config_dir = tempfile::tempdir().unwrap(); + let pin = pin_in(config_dir.path()); + let mut gate = gate_in(config_dir.path(), FINGERPRINT_A); + + assert_eq!(gate.push(&before).unwrap(), before); + assert_eq!(gate.push(reply_head).unwrap(), b""); + assert_eq!(fs::read_to_string(pin.path()).unwrap(), PIN_HEADER); + + assert_eq!(gate.push(reply_last_byte).unwrap(), reply); + assert_eq!( + fs::read_to_string(pin.path()).unwrap(), + pin_file(KNOWN_HOSTS_KEY_A) + ); + assert_eq!(gate.push(&after).unwrap(), after); + } + + #[test] + fn gate_releases_whole_lines_that_precede_the_identification_line() { + let config_dir = tempfile::tempdir().unwrap(); + let mut gate = gate_in(config_dir.path(), FINGERPRINT_A); + assert_eq!(gate.push(b"Welcome\r\nnot").unwrap(), b"Welcome\r\n"); + assert_eq!(gate.push(b"ice\nSSH-2.0-Open").unwrap(), b"notice\n"); + assert_eq!(gate.push(b"Shell\r\n").unwrap(), b"SSH-2.0-OpenShell\r\n"); + } + + #[test] + fn gate_withholds_a_reply_carrying_another_key() { + for exchange in [curve25519_exchange(KEY_B), group_exchange(KEY_B)] { + assert_refused( + "another key", + FINGERPRINT_A, + &exchange[0], + &exchange.concat(), + |error| { + matches!( + error, + HostKeyError::Mismatch { expected, presented, .. } + if *expected == fingerprint(FINGERPRINT_A) + && *presented == fingerprint(FINGERPRINT_B) + ) + }, + PIN_HEADER, + ); + } + } + + #[test] + fn gate_fails_when_new_keys_come_before_an_ed25519_host_key() { + let rsa = [string(b"ssh-rsa"), string(&[1, 0, 1]), string(&[0x7f; 257])].concat(); + let short = [string(b"ssh-ed25519"), string(&KEY_A[..31])].concat(); + let truncated = ed25519_blob(KEY_A)[..50].to_vec(); + let trailing = [ed25519_blob(KEY_A), vec![0]].concat(); + let mut befores = vec![ + [BANNER, &kexinit()].concat(), + [BANNER, &kexinit(), &group()].concat(), + ]; + befores.extend( + [rsa, short, truncated, trailing, Vec::new()] + .map(|host_key| [BANNER, &kexinit(), &kex_reply(31, &host_key)].concat()), + ); + for before in befores { + let stream = [before.as_slice(), &newkeys(), &ENCRYPTED].concat(); + for expectation in [FINGERPRINT_A, "unverified"] { + assert_refused( + "no host key", + expectation, + &before, + &stream, + |error| matches!(error, HostKeyError::Malformed(NO_HOST_KEY)), + PIN_HEADER, + ); + } + } + } + + const NO_HOST_KEY: &str = "key exchange finished without an Ed25519 host key"; + const OUT_OF_ORDER: &str = "packet outside the key exchange order"; + + /// `ssh` discards the decoy, a packet it does not expect or was told to + /// skip, and would act on the reply behind it. + #[test] + fn gate_withholds_a_reply_carrying_another_key_behind_a_decoy() { + let decoy = kex_reply(31, &ed25519_blob(KEY_A)); + let other = ed25519_blob(KEY_B); + let decoy_pinned = pin_file(KNOWN_HOSTS_KEY_A); + for (name, before, withheld, pin) in [ + ( + "decoy before KEXINIT", + BANNER.to_vec(), + [decoy.as_slice(), &kexinit(), &kex_reply(31, &other)].concat(), + PIN_HEADER, + ), + ( + "decoy as the announced wrong guess", + BANNER.to_vec(), + [ + kexinit_ending(&[1, 0, 0, 0, 0]), + decoy.clone(), + kex_reply(31, &other), + ] + .concat(), + PIN_HEADER, + ), + ( + "decoy before the reply", + [BANNER, &kexinit(), &decoy].concat(), + kex_reply(31, &other), + decoy_pinned.as_str(), + ), + ( + "decoy after the group", + [BANNER, &kexinit(), &group(), &decoy].concat(), + kex_reply(33, &other), + decoy_pinned.as_str(), + ), + ] { + assert_refused( + name, + FINGERPRINT_A, + &before, + &[before.as_slice(), &withheld, &newkeys(), &ENCRYPTED].concat(), + |error| matches!(error, HostKeyError::Malformed(OUT_OF_ORDER)), + pin, + ); + } + } + + #[test] + fn gate_rejects_a_packet_outside_the_key_exchange_order() { + let ignore = packet(&[2, 0, 0, 0, 0]); + let reply = kex_reply(31, &ed25519_blob(KEY_A)); + let rsa = [string(b"ssh-rsa"), string(&[1, 0, 1]), string(&[0x7f; 257])].concat(); + let after_kexinit = [BANNER, &kexinit()].concat(); + let after_group = [BANNER, &kexinit(), &group()].concat(); + let after_reply = [BANNER, &kexinit(), &reply].concat(); + let pinned = pin_file(KNOWN_HOSTS_KEY_A); + for (name, before, offending) in [ + ("IGNORE first", BANNER, &ignore), + ("NEWKEYS first", BANNER, &newkeys()), + ("KEXINIT cut short", BANNER, &kexinit_ending(&[])), + ("IGNORE before the reply", &after_kexinit, &ignore), + ("second KEXINIT", &after_kexinit, &kexinit()), + ("second group", &after_group, &group()), + ( + "group exchange reply without an Ed25519 key", + &after_group, + &kex_reply(33, &rsa), + ), + ("IGNORE before NEWKEYS", &after_reply, &ignore), + ("second reply", &after_reply, &reply), + ] { + assert_refused( + name, + FINGERPRINT_A, + before, + &[before, offending, &newkeys(), &ENCRYPTED].concat(), + |error| matches!(error, HostKeyError::Malformed(OUT_OF_ORDER)), + if before == after_reply { + &pinned + } else { + PIN_HEADER + }, + ); + } + } + + #[test] + fn gate_stays_closed_after_a_refusal() { + let [before, reply, after] = curve25519_exchange(KEY_B); + for refused in [ + [before.as_slice(), &reply].concat(), + [BANNER, &kexinit_ending(&[1, 0, 0, 0, 0])].concat(), + ] { + let config_dir = tempfile::tempdir().unwrap(); + let mut gate = gate_in(config_dir.path(), FINGERPRINT_A); + let first = gate.push(&refused).unwrap_err().to_string(); + + for later in [after.as_slice(), b"", &curve25519_exchange(KEY_A).concat()] { + assert_eq!(gate.push(later).unwrap_err().to_string(), first); + } + assert_eq!( + fs::read_to_string(pin_in(config_dir.path()).path()).unwrap(), + PIN_HEADER + ); + } + } + + #[test] + fn gate_withholds_the_reply_when_the_key_cannot_be_pinned() { + let [before, reply, _] = curve25519_exchange(KEY_A); + let config_dir = tempfile::tempdir().unwrap(); + let pin = pin_in(config_dir.path()); + let mut gate = gate_in(config_dir.path(), FINGERPRINT_A); + gate.push(&before).unwrap(); + let pin_dir = pin.path().parent().unwrap(); + fs::remove_dir_all(pin_dir).unwrap(); + fs::write(pin_dir, "a file where the directory should be").unwrap(); + + assert!(matches!(gate.push(&reply), Err(HostKeyError::Io(_)))); + } + + #[test] + fn gate_rejects_a_malformed_packet() { + let length = |length: u32| [&length.to_be_bytes()[..], &[0; 16]].concat(); + for (packet, expected) in [ + (length(0), "packet length is out of range"), + (length(1), "packet length is out of range"), + (length(262_145), "packet length is out of range"), + (length(u32::MAX), "packet length is out of range"), + (vec![0, 0, 0, 2, 1, 31], "packet padding leaves no payload"), + (vec![0, 0, 0, 2, 9, 31], "packet padding leaves no payload"), + ] { + assert_refused( + expected, + FINGERPRINT_A, + BANNER, + &[BANNER, &packet].concat(), + |error| matches!(error, HostKeyError::Malformed(message) if *message == expected), + PIN_HEADER, + ); + } + } + + #[test] + fn gate_relays_the_largest_packet_ssh_accepts() { + let mut largest_group = 262_144_u32.to_be_bytes().to_vec(); + largest_group.extend_from_slice(&[4, 31]); + largest_group.resize(4 + 262_144, 2); + let [_, reply, after] = group_exchange(KEY_A); + let stream = [BANNER, &kexinit(), &largest_group, &reply, &after].concat(); + + let relayed = relay(FINGERPRINT_A, stream.chunks(4096)); + + assert!(relayed.result.is_ok()); + assert!(relayed.released == stream); + assert_eq!(relayed.pin, Some(pin_file(KNOWN_HOSTS_KEY_A))); + } + + #[test] + fn gate_rejects_an_identification_line_that_is_too_long() { + let mut too_long = b"SSH-2.0-".to_vec(); + too_long.resize(8192, b'a'); + assert_refused( + "line of 8192 bytes", + FINGERPRINT_A, + b"", + &too_long, + |error| { + matches!( + error, + HostKeyError::Malformed("identification line is too long") + ) + }, + PIN_HEADER, + ); + + let mut longest = vec![b'a'; 8191]; + longest.push(b'\n'); + let stream = [longest, curve25519_exchange(KEY_A).concat()].concat(); + let relayed = relay(FINGERPRINT_A, [stream.as_slice()]); + assert!(relayed.result.is_ok()); + assert!(relayed.released == stream); + } +} diff --git a/crates/openshell-tui/src/lib.rs b/crates/openshell-tui/src/lib.rs index 0e807cd452..cb41900651 100644 --- a/crates/openshell-tui/src/lib.rs +++ b/crates/openshell-tui/src/lib.rs @@ -994,14 +994,19 @@ async fn handle_shell_connect( return Ok(()); } }; - let proxy_command = build_proxy_command( - &exe.to_string_lossy(), - &gateway_url, - &sandbox_name, - &workspace, - &session.token, - &app.gateway_name, - ); + let target = SandboxSshTarget { + gateway: &app.gateway_name, + workspace: &workspace, + sandbox: &sandbox_name, + }; + let (proxy_command, unverified_warning) = + match sandbox_proxy_for_session(&session, &exe, &gateway_url, &target) { + Ok(proxy) => proxy, + Err(refusal) => { + app.status_text = host_key_refused_status(&refusal); + return Ok(()); + } + }; // Step 5: Build the SSH command. let mut command = std::process::Command::new("ssh"); command @@ -1033,6 +1038,9 @@ async fn handle_shell_connect( disable_raw_mode().into_diagnostic()?; // Step 8: Spawn SSH as child process and wait. + if let Some(warning) = unverified_warning { + eprintln!("{warning}"); + } let status = tokio::task::spawn_blocking(move || command.status()).await; match &status { Ok(Ok(s)) if !s.success() => { @@ -1124,14 +1132,19 @@ async fn handle_exec_command( return Ok(()); } }; - let proxy_command = build_proxy_command( - &exe.to_string_lossy(), - &gateway_url, - sandbox_name, + let target = SandboxSshTarget { + gateway: &app.gateway_name, workspace, - &session.token, - &app.gateway_name, - ); + sandbox: sandbox_name, + }; + let (proxy_command, unverified_warning) = + match sandbox_proxy_for_session(&session, &exe, &gateway_url, &target) { + Ok(proxy) => proxy, + Err(refusal) => { + app.status_text = format!("exec: {}", host_key_refused_status(&refusal)); + return Ok(()); + } + }; // Step 3: Build SSH command — same flags as handle_shell_connect but with // the parsed command appended. Each argument is escaped individually so the @@ -1165,6 +1178,9 @@ async fn handle_exec_command( disable_raw_mode().into_diagnostic()?; // Step 5: Run command — blocks until user Ctrl-C's or command exits. + if let Some(warning) = unverified_warning { + eprintln!("{warning}"); + } let status = tokio::task::spawn_blocking(move || ssh.status()).await; match &status { Ok(Ok(s)) if !s.success() => { @@ -1199,9 +1215,40 @@ async fn handle_exec_command( // SSH utility functions are shared via openshell_core::forward. use openshell_core::forward::{ - build_proxy_command, format_gateway_url, resolve_ssh_gateway, sandbox_ssh_args, shell_escape, - validate_ssh_session_response, + SandboxProxyCommand, format_gateway_url, resolve_ssh_gateway, sandbox_proxy_command, + sandbox_ssh_args, shell_escape, validate_ssh_session_response, }; +use openshell_core::ssh_host_key::{ + HostKeyCheck, HostKeyError, SandboxSshTarget, UnverifiedPolicy, +}; + +/// Decide how the sandbox's SSH host key is checked and build the +/// `ProxyCommand` that enforces it. The warning is present when the key cannot +/// be verified. +fn sandbox_proxy_for_session( + session: &openshell_core::proto::CreateSshSessionResponse, + exe: &std::path::Path, + gateway_url: &str, + target: &SandboxSshTarget<'_>, +) -> std::result::Result<(SandboxProxyCommand, Option), HostKeyError> { + let host_key = HostKeyCheck::for_session(session, target, UnverifiedPolicy::from_env())?; + let unverified_warning = host_key.unverified_warning(target.sandbox); + let proxy_command = sandbox_proxy_command( + &exe.to_string_lossy(), + gateway_url, + target, + &session.token, + host_key, + ); + Ok((proxy_command, unverified_warning)) +} + +/// The status line shows one row, so it carries the first line of a refusal. +/// `openshell sandbox connect` prints the rest. +fn host_key_refused_status(refusal: &HostKeyError) -> String { + let refusal = refusal.to_string(); + refusal.lines().next().unwrap_or_default().to_string() +} fn build_exec_command(command: &[String]) -> String { command @@ -1636,14 +1683,20 @@ async fn start_port_forwards( return warnings; } }; - let proxy_command = build_proxy_command( - &exe.to_string_lossy(), - &gateway_url, - sandbox_name, + let target = SandboxSshTarget { + gateway: gateway_name, workspace, - &session.token, - gateway_name, - ); + sandbox: sandbox_name, + }; + let (proxy_command, unverified_warning) = + match sandbox_proxy_for_session(&session, &exe, &gateway_url, &target) { + Ok(proxy) => proxy, + Err(refusal) => { + warnings.push(host_key_refused_status(&refusal)); + return warnings; + } + }; + warnings.extend(unverified_warning); // Start a forward for each spec. for spec in specs { @@ -3218,6 +3271,23 @@ mod exec_command_tests { } } +#[cfg(test)] +mod host_key_refusal_tests { + use super::*; + + #[test] + fn host_key_refusal_fits_the_status_line() { + let refusal = HostKeyError::Unpublished { + sandbox: "my-sb".to_string(), + }; + + assert_eq!( + host_key_refused_status(&refusal), + "sandbox 'my-sb' has no published SSH host key, so its SSH server cannot be verified." + ); + } +} + #[cfg(test)] mod draft_approve_all_message_tests { use super::*; diff --git a/docs/how-it-works/sandboxes/overview.mdx b/docs/how-it-works/sandboxes/overview.mdx index 9f61089f13..776c006589 100644 --- a/docs/how-it-works/sandboxes/overview.mdx +++ b/docs/how-it-works/sandboxes/overview.mdx @@ -897,9 +897,17 @@ gateway, and supervisor must use the matching OpenShell release. Older runtimes can return an empty fingerprint; an empty value cannot verify an SSH server's identity. -The CLI and TUI continue to use their existing SSH connection settings. -Exposing the fingerprint does not enable automatic host-key verification in -those clients. +The CLI, the TUI, and generated SSH config entries check the sandbox's host +key against this fingerprint on every SSH connection they open. When the SSH +server presents a different key, they stop before authentication and report +`SSH host key mismatch`. + +They refuse a sandbox that has no published fingerprint. If the gateway +predates sandbox SSH identities, upgrade it. Otherwise stop and start the +sandbox so the gateway assigns it a key. To connect without verification, set +`OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY=1`. OpenShell then prints a warning +on every such connection. For an editor, set the variable in the editor's +environment. Generate an SSH config entry for a sandbox so tools like VS Code Remote-SSH can connect directly: @@ -909,6 +917,11 @@ openshell sandbox ssh-config my-sandbox Append the output to `~/.ssh/config` or use `--editor` on `sandbox create`/`sandbox connect` for automatic setup. +OpenShell refuses connections through SSH config entries that an earlier +release generated, because those entries accept any host key. Replace each +pasted entry with new `openshell sandbox ssh-config` output, or use `--editor` +on `sandbox connect` again for that sandbox. + ## Transfer Files Upload files from your host into the sandbox: diff --git a/examples/vscode-remote-sandbox.md b/examples/vscode-remote-sandbox.md index b5cfe1bdd2..d39581ec2f 100644 --- a/examples/vscode-remote-sandbox.md +++ b/examples/vscode-remote-sandbox.md @@ -37,19 +37,7 @@ managed SSH config include file. ### 3. Optional manual workflow -If you want to inspect the generated SSH stanza directly: - -```text -Host openshell-my-sandbox.default - User sandbox - StrictHostKeyChecking no - UserKnownHostsFile /dev/null - GlobalKnownHostsFile /dev/null - LogLevel ERROR - ProxyCommand openshell ssh-proxy --gateway --name my-sandbox -``` - -You can still print it manually with: +To inspect the generated SSH stanza, print it with: ```bash openshell sandbox ssh-config my-sandbox diff --git a/scripts/smoke-test-network-policy.sh b/scripts/smoke-test-network-policy.sh index 49376fd36a..89ad5c3f57 100755 --- a/scripts/smoke-test-network-policy.sh +++ b/scripts/smoke-test-network-policy.sh @@ -178,8 +178,6 @@ sandbox_exec() { echo "$ssh_config" > "$ssh_config_file" ssh -F "$ssh_config_file" \ - -o StrictHostKeyChecking=no \ - -o UserKnownHostsFile=/dev/null \ -o LogLevel=ERROR \ -o ConnectTimeout=15 \ "$ssh_host" "$@" 2>&1 diff --git a/skills/debug-openshell-cluster/SKILL.md b/skills/debug-openshell-cluster/SKILL.md index 578097b5ad..cd22f6e6c3 100644 --- a/skills/debug-openshell-cluster/SKILL.md +++ b/skills/debug-openshell-cluster/SKILL.md @@ -340,6 +340,7 @@ Common findings: - Docker driver cannot initialize because it cannot find `openshell-sandbox`: verify the sibling binary next to `openshell-gateway`, or that the configured `sandbox_runtime_image` contains `/openshell-sandbox`. - Sandbox never registers: check gateway logs and the supervisor's gateway endpoint. - SSH host-key startup errors: use matching gateway, compute-driver, and supervisor releases. The gateway retains each sandbox's SSH key in its configured credential store and sends it only through the supervisor bootstrap bundle. Check credential-driver availability and compare the public `host_key_fingerprint` from sandbox JSON output; never print the bootstrap bundle or private key. A missing stored key for a sandbox with a fingerprint is an error, not permission to replace its identity. See the [sandbox SSH identity documentation](https://docs.nvidia.com/openshell/latest/how-it-works/sandboxes/overview). +- Client reports `SSH host key mismatch for sandbox ''`: the SSH server reached through the gateway presented a key other than the one the gateway publishes, and the client refused it before authentication. Read the published `host_key_fingerprint` with `openshell sandbox get --output json` and compare it with the `gateway published` and `server presented` values in the error. Use matching gateway, compute-driver, and supervisor releases, then stop and start the sandbox so its supervisor restarts with the assigned key. Do not work around the check. - Calls to an external tool server fail while the sandbox is Ready: inspect `Tool server connections` in `openshell sandbox get `. For configured MCP-over-HTTP endpoints, JSON output exposes each address together with `last_result` and `last_reported_at` in `endpoint_statuses`. Select the endpoint by host, path, and ports, then check the reported failure boundary. `last_reported_at` records gateway acceptance time and can advance when retained evidence is accepted after a reset. Results do not expire or prove current availability; `HttpResponseReceived` can still contain a tool error. For token grant failures on any HTTP protocol, including failures before endpoint selection, inspect `last_token_grant_failure` in JSON output. It contains the bounded destination and cause, survives unrelated successful traffic, and clears on matching grant success or a policy, provider, or supervisor reset. If several paths share a host and port, other failures before the path is known, such as TLS failures, remain only in logs. A `CredentialUnavailable` result from a failed dynamic token grant also carries `last_failure` with a closed-set `code`, `stage`, `recovery_action`, the provider and credential names, and the `request_id` that the workload received in its `x-openshell-request-id` response header. Search `openshell logs ` for that ID: the supervisor's `Token grant failed` line carries the code and stage for every endpoint, and the gateway's `dynamic token grant failed` line appears when the gateway hop failed. Verify the actual operation when current tool availability matters. - On Docker Desktop, repeated `Policy fetch failed after 5 attempts` messages can mean the supervisor's selected network mode cannot reach the gateway. diff --git a/skills/openshell-cli/SKILL.md b/skills/openshell-cli/SKILL.md index 82e1000395..b6c889cda7 100644 --- a/skills/openshell-cli/SKILL.md +++ b/skills/openshell-cli/SKILL.md @@ -553,6 +553,12 @@ sandbox, even under the same name, creates a different identity. See the [sandbox SSH documentation](https://docs.nvidia.com/openshell/latest/how-it-works/sandboxes/overview.md) for verification behavior and release compatibility. +The CLI, the TUI, and generated SSH config entries verify the host key against +that fingerprint on every SSH connection they open and fail closed. Each +refusal (`SSH host key mismatch`, `has no published SSH host key`, `predates +host-key verification`) prints its fix. The TUI status line shows only the +first line; run `openshell sandbox connect ` to read the rest. + For an infrastructure `Error` on a backend that supports restarting stopped sandboxes, fix the reported cause, run `sandbox stop`, and wait for it to succeed before running `sandbox start`. This retains the sandbox and persistent workspace but cannot recover deleted resources or storage. For a provisioning timeout, wait for cleanup to finish, then run `sandbox start` directly. ### Suspend and resume sandboxes From 3cdb3a9710e393e622c35c2bb339dc206a30dbdf Mon Sep 17 00:00:00 2001 From: Adrien Langou Date: Wed, 18 Nov 2026 13:54:21 +0100 Subject: [PATCH 2/6] refactor(cli): trim SSH host key verification Restore the base shape of the SSH option and proxy-command helpers where the feature does not need a new one, fold duplicate tests into tables, and drop tests that repeat a proof at a more expensive level. No change to what is verified or refused. Signed-off-by: Adrien Langou --- crates/openshell-cli/src/ssh.rs | 209 ++---- crates/openshell-cli/src/tls.rs | 17 +- .../sandbox_create_lifecycle_integration.rs | 36 +- .../tests/ssh_proxy_host_key_integration.rs | 514 ++++++-------- crates/openshell-core/src/forward.rs | 164 ++--- crates/openshell-core/src/paths.rs | 16 - crates/openshell-core/src/ssh_host_key.rs | 646 ++++-------------- crates/openshell-tui/src/lib.rs | 21 +- 8 files changed, 481 insertions(+), 1142 deletions(-) diff --git a/crates/openshell-cli/src/ssh.rs b/crates/openshell-cli/src/ssh.rs index 289de94946..1b88f030ea 100644 --- a/crates/openshell-cli/src/ssh.rs +++ b/crates/openshell-cli/src/ssh.rs @@ -24,7 +24,7 @@ use openshell_core::proto::{ }; use openshell_core::ssh_host_key::{ HostKeyCheck, HostKeyExpectation, HostKeyGate, KnownHostsPin, SandboxSshTarget, - UnverifiedPolicy, host_key_alias, + allow_unverified_host_key, host_key_alias as host_alias, }; use openshell_core::upload::{ DestinationKind as UploadDestinationKind, ExpectedDestination, UploadPlan, @@ -229,7 +229,7 @@ async fn ssh_session_config( sandbox: name, }; let host_key = - HostKeyCheck::for_session(&session.response, &target, UnverifiedPolicy::from_env()) + HostKeyCheck::for_session(&session.response, &target, allow_unverified_host_key()) .into_diagnostic()?; if let Some(warning) = host_key.unverified_warning(name) { eprintln!("{warning}"); @@ -754,7 +754,7 @@ pub async fn sandbox_connect_editor( let session = ssh_session_config(server, name, tls, workspace, None).await?; let workspace_root = discover_workspace_root(&session).await?; - let host_alias = host_key_alias(name, workspace); + let host_alias = host_alias(name, workspace); install_ssh_config(gateway, name, workspace)?; launch_editor(editor, &host_alias, &workspace_root)?; eprintln!( @@ -2193,7 +2193,7 @@ pub async fn sandbox_ssh_proxy_by_name( let expectation = HostKeyExpectation::from_published( &session.response.host_key_fingerprint, name, - UnverifiedPolicy::from_env(), + allow_unverified_host_key(), ) .into_diagnostic()?; let pin = KnownHostsPin::at(known_hosts, name, workspace).into_diagnostic()?; @@ -2244,7 +2244,7 @@ fn render_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result Result>() .join("\n"); Ok(format!( - "Host {host_alias}\n User sandbox\n{options}\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {proxy_cmd}\n" + "Host {host_alias}\n User sandbox\n{options}\n ControlMaster no\n ControlPath none\n ControlPersist no\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {proxy_cmd}\n" )) } @@ -2386,7 +2386,7 @@ pub fn install_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result< openshell_core::paths::create_dir_restricted(parent)?; } - let alias = host_key_alias(name, workspace); + let alias = host_alias(name, workspace); let contents = fs::read_to_string(&managed_config).unwrap_or_default(); let updated = upsert_host_block(&contents, &alias, &block); fs::write(&managed_config, updated) @@ -2457,8 +2457,7 @@ mod tests { environment.run(|| { set_default_ssh_log_level("DEBUG"); let expected = override_level.unwrap_or("DEBUG"); - let proxy = PinnedProxy::new(); - let command = ssh_base_command(&proxy.command); + let command = ssh_base_command(&pinned_proxy("known_hosts").0); assert!( command .get_args() @@ -2576,39 +2575,21 @@ mod tests { const PUBLISHED_FINGERPRINT: &str = "SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA"; - /// A proxy command for sandbox `demo` whose host key is pinned at - /// `known_hosts`. - struct PinnedProxy { - command: SandboxProxyCommand, - known_hosts: PathBuf, - _dir: tempfile::TempDir, - } - - impl PinnedProxy { - fn new() -> Self { - Self::in_directory("known_hosts") - } - - fn in_directory(name: &str) -> Self { - let dir = tempfile::tempdir().unwrap(); - let known_hosts = dir.path().join(name).join("openshell-demo.default"); - let target = SandboxSshTarget { - gateway: "prod", - workspace: "default", - sandbox: "demo", - }; - let pin = - KnownHostsPin::at(known_hosts.clone(), target.sandbox, target.workspace).unwrap(); - let host_key = - HostKeyCheck::for_relay(PUBLISHED_FINGERPRINT.parse().unwrap(), pin).unwrap(); - let command = - sandbox_proxy_command("openshell", "https://gw:443", &target, "tok", host_key); - Self { - command, - known_hosts, - _dir: dir, - } - } + /// A proxy command for sandbox `demo`, and the file under `directory` in + /// which its host key is pinned. + fn pinned_proxy(directory: &str) -> (SandboxProxyCommand, PathBuf) { + let dir = tempfile::tempdir().unwrap(); + let known_hosts = dir.path().join(directory).join("openshell-demo.default"); + let target = SandboxSshTarget { + gateway: "prod", + workspace: "default", + sandbox: "demo", + }; + let pin = KnownHostsPin::at(known_hosts.clone(), "demo", "default").unwrap(); + let host_key = + HostKeyCheck::for_relay(PUBLISHED_FINGERPRINT.parse().unwrap(), pin).unwrap(); + let proxy = sandbox_proxy_command("openshell", "https://gw:443", &target, "tok", host_key); + (proxy, known_hosts) } #[test] @@ -2700,9 +2681,8 @@ mod tests { let setup = phase == "setup"; async move { if setup { - let PinnedProxy { command, _dir, .. } = PinnedProxy::new(); let session = SshSessionConfig { - proxy_command: command, + proxy_command: pinned_proxy("known_hosts").0, sandbox_id: String::new(), main_terminal: false, }; @@ -2935,21 +2915,6 @@ mod tests { }); } - #[test] - fn host_key_refusals_before_ssh_starts_are_not_retried() { - use openshell_core::ssh_host_key::HostKeyError; - for refusal in [ - HostKeyError::Unpublished { - sandbox: "demo".to_string(), - }, - HostKeyError::BadFingerprint, - ] { - let error = Err::<(), _>(refusal).into_diagnostic().unwrap_err(); - assert!(!connect_error_is_retryable(&error), "{error:?}"); - assert!(!sync_error_is_retryable(&error), "{error:?}"); - } - } - #[test] fn stale_ssh_config_refusal_names_the_gateway_and_workspace_of_the_entry() { assert_eq!( @@ -2983,9 +2948,14 @@ mod tests { ) .unwrap(); - let effective = ssh_effective_options(&config, ["openshell-demo.beta"]); + let output = Command::new("ssh") + .arg("-F") + .arg(&config) + .args(["-G", "openshell-demo.beta"]) + .output() + .unwrap(); + let effective = String::from_utf8(output.stdout).unwrap(); - assert_effective_options(&effective, &["user sandbox"]); assert_only_the_pin_is_trusted( &effective, Path::new(&known_hosts), @@ -3006,6 +2976,12 @@ mod tests { }); } + #[test] + fn host_alias_includes_workspace() { + assert_eq!(host_alias("demo", "default"), "openshell-demo.default"); + assert_eq!(host_alias("demo", "beta"), "openshell-demo.beta"); + } + #[test] fn launch_editor_returns_friendly_error_when_binary_missing() { let err = launch_editor_command( @@ -3051,47 +3027,14 @@ mod tests { assert_eq!(forward_probe_host(&loopback), "127.0.0.1"); } - /// The options `ssh -G` reports for `args` when `config` is the user's - /// SSH config. + /// Resolve `ssh -G` for `command`'s arguments against a user config that + /// enables connection sharing and forking for the host `sandbox`. `extra_args` + /// supplies the host and, for the forking option, `-N`, when `command` lacks them. #[cfg(unix)] - fn ssh_effective_options>( - config: &Path, - args: impl IntoIterator, - ) -> String { + fn effective_ssh_config(command: &Command, extra_args: &[&str]) -> String { let _guard = TEST_ENV_LOCK .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); - let output = Command::new("ssh") - .arg("-F") - .arg(config) - .arg("-G") - .args(args) - .output() - .unwrap(); - assert!( - output.status.success(), - "ssh -G failed: {}", - String::from_utf8_lossy(&output.stderr) - ); - String::from_utf8(output.stdout).unwrap() - } - - #[cfg(unix)] - fn assert_effective_options(effective: &str, expected: &[&str]) { - for option in expected { - assert!( - effective.lines().any(|line| line == *option), - "ssh does not report `{option}`:\n{effective}" - ); - } - } - - /// Resolve `ssh -G` for `command`'s arguments against a user config that - /// enables connection sharing and forking for the host `sandbox` and trusts - /// any host key. `extra_args` supplies the host and, for the forking option, - /// `-N`, when `command` lacks them. - #[cfg(unix)] - fn effective_ssh_config(command: &Command, extra_args: &[&str]) -> String { let dir = tempfile::tempdir().unwrap(); let config = dir.path().join("ssh_config"); fs::write( @@ -3105,8 +3048,20 @@ mod tests { ) .unwrap(); - let extra_args = extra_args.iter().map(std::ffi::OsStr::new); - ssh_effective_options(&config, command.get_args().chain(extra_args)) + let output = Command::new("ssh") + .arg("-F") + .arg(&config) + .arg("-G") + .args(command.get_args()) + .args(extra_args) + .output() + .unwrap(); + assert!( + output.status.success(), + "ssh -G failed: {}", + String::from_utf8_lossy(&output.stderr) + ); + String::from_utf8(output.stdout).unwrap() } #[cfg(unix)] @@ -3122,51 +3077,37 @@ mod tests { #[cfg(unix)] fn assert_only_the_pin_is_trusted(effective: &str, known_hosts: &Path, alias: &str) { - assert_effective_options( - effective, - &[ - "stricthostkeychecking true", - &format!("userknownhostsfile {}", known_hosts.display()), - "globalknownhostsfile /dev/null", - &format!("hostkeyalias {alias}"), - "hostkeyalgorithms ssh-ed25519", - "verifyhostkeydns false", - "updatehostkeys false", - ], - ); - } - - #[cfg(unix)] - #[test] - fn ssh_base_command_overrides_user_connection_sharing() { - let proxy = PinnedProxy::new(); - let effective = effective_ssh_config(&ssh_base_command(&proxy.command), &["-N", "sandbox"]); - assert_connection_sharing_disabled(&effective); + for option in [ + "stricthostkeychecking true", + &format!("userknownhostsfile {}", known_hosts.display()), + "globalknownhostsfile /dev/null", + &format!("hostkeyalias {alias}"), + "hostkeyalgorithms ssh-ed25519", + "verifyhostkeydns false", + "updatehostkeys false", + ] { + assert!(effective.lines().any(|line| line == option), "{option}"); + } } #[cfg(unix)] #[test] - fn ssh_base_command_overrides_user_host_key_trust() { + fn ssh_base_command_overrides_user_connection_sharing_and_host_key_trust() { for directory in ["known_hosts", "known hosts", "o'brien known=hosts"] { - let proxy = PinnedProxy::in_directory(directory); - let effective = - effective_ssh_config(&ssh_base_command(&proxy.command), &["-N", "sandbox"]); - assert_only_the_pin_is_trusted( - &effective, - &proxy.known_hosts, - "openshell-demo.default", - ); + let (proxy, known_hosts) = pinned_proxy(directory); + let effective = effective_ssh_config(&ssh_base_command(&proxy), &["-N", "sandbox"]); + assert_connection_sharing_disabled(&effective); + assert_only_the_pin_is_trusted(&effective, &known_hosts, "openshell-demo.default"); } } #[cfg(unix)] #[test] fn forward_ssh_command_overrides_user_multiplexing_and_forking() { - let proxy = PinnedProxy::new(); - let forward = ssh_forward_command(&proxy.command, "sandbox-id", &ForwardSpec::new(18080)); + let (proxy, _) = pinned_proxy("known_hosts"); + let forward = ssh_forward_command(&proxy, "sandbox-id", &ForwardSpec::new(18080)); let effective = effective_ssh_config(&forward, &[]); assert_connection_sharing_disabled(&effective); - assert_only_the_pin_is_trusted(&effective, &proxy.known_hosts, "openshell-demo.default"); assert!( effective .lines() @@ -3177,11 +3118,9 @@ mod tests { #[cfg(unix)] #[test] fn upload_ssh_command_overrides_user_multiplexing_and_forking() { - let proxy = PinnedProxy::new(); - let upload = ssh_upload_command(&proxy.command); + let upload = ssh_upload_command(&pinned_proxy("known_hosts").0); let effective = effective_ssh_config(&upload, &[]); assert_connection_sharing_disabled(&effective); - assert_only_the_pin_is_trusted(&effective, &proxy.known_hosts, "openshell-demo.default"); for expected in [ "forkafterauthentication no", "requesttty false", diff --git a/crates/openshell-cli/src/tls.rs b/crates/openshell-cli/src/tls.rs index d294f5303e..fd138aae5b 100644 --- a/crates/openshell-cli/src/tls.rs +++ b/crates/openshell-cli/src/tls.rs @@ -131,7 +131,7 @@ pub struct TlsMaterials { /// Resolve the TLS cert directory for a known gateway name. fn tls_dir_for_gateway(name: &str) -> Option { - let safe_name = openshell_core::paths::gateway_dir_name(name); + let safe_name = sanitize_name(name); let base = xdg_config_dir().ok()?.join("openshell").join("gateways"); Some(base.join(safe_name).join("mtls")) } @@ -158,11 +158,24 @@ fn default_tls_dir(server: &str) -> Option { } let name = name.unwrap_or_else(|| "openshell".to_string()); - let safe_name = openshell_core::paths::gateway_dir_name(&name); + let safe_name = sanitize_name(&name); let base = xdg_config_dir().ok()?.join("openshell").join("gateways"); Some(base.join(safe_name).join("mtls")) } +fn sanitize_name(value: &str) -> String { + value + .chars() + .map(|ch| { + if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_') { + ch + } else { + '_' + } + }) + .collect() +} + fn xdg_config_dir() -> Result { openshell_core::paths::xdg_config_dir() } diff --git a/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs b/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs index d8b668a47a..df3cb8b923 100644 --- a/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs +++ b/crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs @@ -3323,7 +3323,7 @@ async fn sandbox_forward_background_tracks_owned_child_when_pid_discovery_fails( } #[tokio::test] -async fn editor_connect_pins_the_published_host_key_for_ssh_and_the_config_entry() { +async fn editor_connect_hands_ssh_the_published_host_key_and_its_pin() { let server = run_server().await; let fake_ssh_dir = helpers::tempdir().unwrap(); let xdg_dir = helpers::tempdir().unwrap(); @@ -3348,37 +3348,13 @@ async fn editor_connect_pins_the_published_host_key_for_ssh_and_the_config_entry .unwrap(); let ssh_arguments = fs::read_to_string(ssh_arguments).unwrap(); - let proxy_command = ssh_arguments - .lines() - .find(|argument| argument.starts_with("ProxyCommand=")) - .unwrap(); - assert!( - proxy_command.ends_with(&format!( - " --host-key '{PUBLISHED_HOST_KEY}' --known-hosts {}", - pin.display() - )), - "{proxy_command}" - ); - for option in [ - "StrictHostKeyChecking=yes".to_string(), - format!("UserKnownHostsFile=\"{}\"", pin.display()), - "HostKeyAlias=openshell-pinned.default".to_string(), + let pin = pin.display(); + for expected in [ + format!(" --host-key '{PUBLISHED_HOST_KEY}' --known-hosts {pin}\n"), + format!("\nUserKnownHostsFile=\"{pin}\"\n"), ] { - assert!( - ssh_arguments.lines().any(|argument| argument == option), - "ssh was not passed {option}:\n{ssh_arguments}" - ); + assert!(ssh_arguments.contains(&expected), "{ssh_arguments}"); } - let config_entry = - fs::read_to_string(xdg_dir.path().join("openshell/ssh_config")).unwrap(); - assert!( - config_entry.contains(&format!(" UserKnownHostsFile \"{}\"\n", pin.display())), - "{config_entry}" - ); - assert!( - config_entry.contains(&format!(" --known-hosts {}\n", pin.display())), - "{config_entry}" - ); }) .await; } diff --git a/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs b/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs index 0a429109e3..15e34f332c 100644 --- a/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs +++ b/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs @@ -14,7 +14,7 @@ use russh::keys::ssh_key::private::Ed25519Keypair; use russh::keys::{HashAlg, PrivateKey}; use std::convert::Infallible; use std::net::SocketAddr; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::{Output, Stdio}; use std::sync::Arc; use std::time::Duration; @@ -51,30 +51,6 @@ fn pin_content(key: Option<&PrivateKey>) -> String { ) } -struct Scratch { - dir: tempfile::TempDir, -} - -impl Scratch { - fn new() -> Self { - Self { - dir: tempfile::tempdir().unwrap(), - } - } - - fn config(&self) -> PathBuf { - self.dir.path().join("config") - } - - fn pin(&self) -> PathBuf { - self.pin_named("pin") - } - - fn pin_named(&self, name: &str) -> PathBuf { - self.dir.path().join("known_hosts").join(name) - } -} - /// A gateway that publishes one host-key fingerprint for the sandbox and /// relays every SSH connection to `sandbox`. struct Gateway { @@ -136,11 +112,7 @@ impl Gateway { (self.sandbox)(sandbox_end); tokio::spawn(relay(request.into_body(), relay_end, frames)); } - _ => { - let _ = frames - .send(Ok(grpc_status(tonic::Status::unimplemented("")))) - .await; - } + _ => {} } let mut response = hyper::Response::new(StreamBody::new(ReceiverStream::new(body))); response @@ -208,20 +180,15 @@ async fn relay(mut inbound: Incoming, sandbox: DuplexStream, frames: GrpcFrames) to_sandbox.abort(); } -fn name_mode(gateway: SocketAddr, known_hosts: &Path) -> Vec { - [ - "ssh-proxy", - "--server", - &format!("http://{gateway}"), - "--name", - SANDBOX, - "--workspace", - WORKSPACE, - "--known-hosts", - &known_hosts.display().to_string(), - ] - .map(str::to_string) - .to_vec() +/// The `ssh-proxy` arguments of a generated config entry. +fn name_mode(gateway: SocketAddr, known_hosts: &Path) -> String { + let known_hosts = known_hosts.display(); + format!("{} --known-hosts {known_hosts}", old_name_mode(gateway)) +} + +/// The same, as releases before host-key verification generated them. +fn old_name_mode(gateway: SocketAddr) -> String { + format!("ssh-proxy --server http://{gateway} --name {SANDBOX} --workspace {WORKSPACE}") } /// Runs `command` to completion. `allow_unverified` is the value of the @@ -250,10 +217,11 @@ async fn finished( .unwrap() } -async fn run_proxy(args: &[String], config: &Path) -> Output { +/// Runs `openshell` with `args`, which are separated by single spaces. +async fn openshell(args: &str, config: &Path, allow_unverified: Option<&str>) -> Output { let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell")); - command.args(args); - finished(command, config, None).await + command.args(args.split(' ')); + finished(command, config, allow_unverified).await } fn mismatch_message(published: &PrivateKey, presented: &PrivateKey) -> String { @@ -269,212 +237,136 @@ fn mismatch_message(published: &PrivateKey, presented: &PrivateKey) -> String { ) } -fn ssh_string(bytes: &[u8]) -> Vec { - let mut string = u32::try_from(bytes.len()).unwrap().to_be_bytes().to_vec(); - string.extend_from_slice(bytes); - string -} - -fn ssh_packet(payload: &[u8]) -> Vec { - let padding = 8 - (5 + payload.len()) % 8; - let padding = if padding < 4 { padding + 8 } else { padding }; - let mut packet = u32::try_from(1 + payload.len() + padding) - .unwrap() - .to_be_bytes() - .to_vec(); - packet.push(u8::try_from(padding).unwrap()); - packet.extend_from_slice(payload); - packet.resize(packet.len() + padding, 0); - packet -} - -/// What an SSH server presenting `key` sends up to its first encrypted bytes. -struct Handshake { - before_reply: Vec, - reply_onward: Vec, -} - -impl Handshake { - fn of_server_with(key: &PrivateKey) -> Self { - let mut before_reply = b"SSH-2.0-OpenShell_test\r\n".to_vec(); - before_reply.extend(ssh_packet(&[[20].as_slice(), &[0; 61]].concat())); - let reply = [ - [31].as_slice(), - &ssh_string(&key.public_key().to_bytes().unwrap()), - &ssh_string(&[7; 32]), - &ssh_string(&[9; 83]), +/// What an SSH server presenting `key` sends: up to its key exchange reply, +/// then from the reply to its first encrypted bytes. The proxy does not check +/// block alignment, so every packet has four bytes of padding. +fn handshake(key: &PrivateKey) -> [Vec; 2] { + let packet = |payload: &[u8]| { + let length = u32::try_from(payload.len() + 5).unwrap().to_be_bytes(); + [&length[..], &[4], payload, &[0; 4]].concat() + }; + let key = key.public_key().to_bytes().unwrap(); + let key_length = u32::try_from(key.len()).unwrap().to_be_bytes(); + let reply = [&[31][..], &key_length, &key, &[0; 127]].concat(); + [ + [ + &b"SSH-2.0-OpenShell_test\r\n"[..], + &packet(&[&[20][..], &[0; 61]].concat()), ] - .concat(); - let mut reply_onward = ssh_packet(&reply); - reply_onward.extend(ssh_packet(&[21])); - reply_onward.extend([0xff; 48]); - Self { - before_reply, - reply_onward, - } - } - - fn bytes(&self) -> Vec { - [self.before_reply.as_slice(), &self.reply_onward].concat() - } + .concat(), + [packet(&reply), packet(&[21]), vec![0xff; 48]].concat(), + ] +} - /// A sandbox whose SSH server sends this handshake and hangs up. - fn sandbox(&self) -> Box { - let bytes = self.bytes(); - Box::new(move |mut stream| { - let bytes = bytes.clone(); - tokio::spawn(async move { - let _ = stream.write_all(&bytes).await; - }); - }) - } +/// A sandbox whose SSH server sends `bytes` and hangs up. +fn sending(bytes: Vec) -> Box { + Box::new(move |mut stream| { + let bytes = bytes.clone(); + tokio::spawn(async move { + let _ = stream.write_all(&bytes).await; + }); + }) } #[tokio::test] async fn name_mode_proxy_withholds_a_key_the_gateway_did_not_publish() { let published = host_key(1); let presented = host_key(2); - let handshake = Handshake::of_server_with(&presented); + let [before_reply, reply_onward] = handshake(&presented); let gateway = Gateway { published: fingerprint(&published), - sandbox: handshake.sandbox(), + sandbox: sending([before_reply.clone(), reply_onward].concat()), } .serve() .await; - let scratch = Scratch::new(); + let scratch = tempfile::tempdir().unwrap(); + let pin = scratch.path().join("known_hosts/pin"); - let proxy = run_proxy(&name_mode(gateway, &scratch.pin()), &scratch.config()).await; + let proxy = openshell(&name_mode(gateway, &pin), scratch.path(), None).await; let stderr = String::from_utf8_lossy(&proxy.stderr); assert_eq!(proxy.status.code(), Some(1), "{stderr}"); assert!( - handshake.before_reply.starts_with(&proxy.stdout), + before_reply.starts_with(&proxy.stdout), "no byte of the key exchange reply may reach ssh" ); assert!( stderr.contains(&mismatch_message(&published, &presented)), "{stderr}" ); - assert_eq!( - std::fs::read_to_string(scratch.pin()).unwrap(), - pin_content(None) - ); + assert_eq!(std::fs::read_to_string(&pin).unwrap(), pin_content(None)); } #[tokio::test] -async fn name_mode_proxy_refuses_a_sandbox_with_no_published_key() { - let handshake = Handshake::of_server_with(&host_key(1)); +async fn name_mode_proxy_reaches_a_sandbox_with_no_published_key_only_when_allowed() { + let key = host_key(1); + let transcript = handshake(&key).concat(); let gateway = Gateway { published: String::new(), - sandbox: handshake.sandbox(), + sandbox: sending(transcript.clone()), } .serve() .await; - let scratch = Scratch::new(); + let scratch = tempfile::tempdir().unwrap(); + let pin = scratch.path().join("known_hosts/pin"); + let args = name_mode(gateway, &pin); - let proxy = run_proxy(&name_mode(gateway, &scratch.pin()), &scratch.config()).await; + let refused = openshell(&args, scratch.path(), None).await; - let stderr = String::from_utf8_lossy(&proxy.stderr); - assert!(!proxy.status.success()); - assert_eq!(proxy.stdout, b""); + let stderr = String::from_utf8_lossy(&refused.stderr); + assert!(!refused.status.success()); + assert_eq!(refused.stdout, b""); assert!( stderr.contains("sandbox 'my-sb' has no published SSH host key"), "{stderr}" ); - assert!(!scratch.pin().exists()); -} - -#[tokio::test] -async fn name_mode_proxy_reaches_a_sandbox_with_no_published_key_only_with_a_warning() { - let key = host_key(1); - let handshake = Handshake::of_server_with(&key); - let gateway = Gateway { - published: String::new(), - sandbox: handshake.sandbox(), - } - .serve() - .await; - let scratch = Scratch::new(); - let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell")); - command.args(name_mode(gateway, &scratch.pin())); + assert!(!pin.exists()); - let proxy = finished(command, &scratch.config(), Some("1")).await; + let allowed = openshell(&args, scratch.path(), Some("1")).await; - let stderr = String::from_utf8_lossy(&proxy.stderr); - assert!(proxy.status.success(), "{stderr}"); - assert_eq!(proxy.stdout, handshake.bytes()); + let stderr = String::from_utf8_lossy(&allowed.stderr); + assert!(allowed.status.success(), "{stderr}"); + assert_eq!(allowed.stdout, transcript); assert!( - stderr.contains( - "warning: SSH host key of sandbox 'my-sb' is NOT verified (the gateway published no fingerprint)" - ), + stderr.contains("warning: SSH host key of sandbox 'my-sb' is NOT verified"), "{stderr}" ); assert_eq!( - std::fs::read_to_string(scratch.pin()).unwrap(), + std::fs::read_to_string(&pin).unwrap(), pin_content(Some(&key)) ); } -/// An `ssh` started by a client from before host-key verification accepts any -/// key, so its proxy must not relay. +/// An `ssh` started by a client or a config entry from before host-key +/// verification accepts any key, so its proxy must not relay. #[tokio::test] -async fn proxy_refuses_a_client_that_sends_no_host_key_check() { - let handshake = Handshake::of_server_with(&host_key(1)); +async fn proxy_refuses_a_caller_that_predates_host_key_verification() { let gateway = Gateway { published: fingerprint(&host_key(1)), - sandbox: handshake.sandbox(), + sandbox: sending(handshake(&host_key(1)).concat()), } .serve() .await; - let scratch = Scratch::new(); - let args = [ - "ssh-proxy", - "--gateway", - &format!("http://{gateway}/proxy/connect"), - "--sandbox", - SANDBOX, - "--workspace", - WORKSPACE, - "--token", - "test-token", - ] - .map(str::to_string); - - let proxy = run_proxy(&args, &scratch.config()).await; - - assert_eq!(proxy.status.code(), Some(1)); - assert_eq!(proxy.stdout, b""); - assert_eq!( - String::from_utf8_lossy(&proxy.stderr), - "openshell: this SSH connection was started by an OpenShell client that predates host-key verification and is refused.\n\ - Restart that client (the openshell command or openshell term) and connect again.\n" + let scratch = tempfile::tempdir().unwrap(); + let old_client = format!( + "ssh-proxy --gateway http://{gateway}/proxy/connect --sandbox {SANDBOX} --workspace {WORKSPACE} --token test-token" ); -} -/// A config entry from before host-key verification tells `ssh` to accept any -/// key, so its proxy must not relay. -#[tokio::test] -async fn name_mode_proxy_refuses_a_config_entry_without_a_known_hosts_file() { - let handshake = Handshake::of_server_with(&host_key(1)); - let gateway = Gateway { - published: fingerprint(&host_key(1)), - sandbox: handshake.sandbox(), + for (args, refused) in [ + (old_client, "started by an OpenShell client that predates"), + ( + old_name_mode(gateway), + "SSH config entry for sandbox 'my-sb'", + ), + ] { + let proxy = openshell(&args, scratch.path(), None).await; + + let stderr = String::from_utf8_lossy(&proxy.stderr); + assert_eq!(proxy.status.code(), Some(1), "{stderr}"); + assert_eq!(proxy.stdout, b""); + assert!(stderr.contains(refused), "{stderr}"); + assert!(stderr.contains("and is refused"), "{stderr}"); } - .serve() - .await; - let scratch = Scratch::new(); - let mut args = name_mode(gateway, &scratch.pin()); - args.truncate(args.len() - 2); - - let proxy = run_proxy(&args, &scratch.config()).await; - - let stderr = String::from_utf8_lossy(&proxy.stderr); - assert_eq!(proxy.status.code(), Some(1), "{stderr}"); - assert_eq!(proxy.stdout, b""); - assert!( - stderr.contains("predates host-key verification and is refused"), - "{stderr}" - ); } /// The real `ssh` client against a real SSH server, with `ssh-proxy` as its @@ -531,107 +423,91 @@ mod openssh { } } - /// A sandbox whose SSH server presents `key`, behind a gateway. - struct SshSandbox { - gateway: SocketAddr, + /// The gateway in front of a sandbox whose SSH server presents `key`. + async fn sandbox_presenting(key: &PrivateKey) -> SocketAddr { + // The supervisor's server also keeps the default algorithms. + let config = Arc::new(russh::server::Config { + keys: vec![key.clone()], + ..Default::default() + }); + Gateway { + published: fingerprint(key), + sandbox: Box::new(move |stream| { + let config = config.clone(); + tokio::spawn(async move { + if let Ok(session) = russh::server::run_stream(config, stream, Shell).await { + let _ = session.await; + } + }); + }), + } + .serve() + .await } - impl SshSandbox { - async fn presenting(key: &PrivateKey) -> Self { - // The supervisor's server also keeps the default algorithms. - let config = Arc::new(russh::server::Config { - keys: vec![key.clone()], - ..Default::default() - }); - let gateway = Gateway { - published: fingerprint(key), - sandbox: Box::new(move |stream| { - let config = config.clone(); - tokio::spawn(async move { - if let Ok(session) = russh::server::run_stream(config, stream, Shell).await - { - let _ = session.await; - } - }); - }), - } - .serve() - .await; - Self { gateway } - } + /// Runs [`COMMAND`] with the `ssh` arguments the CLI and TUI build for + /// a session whose published fingerprint is `published`. `ssh` takes + /// the first value of an option, so `overrides` win. + async fn ssh_as_client( + gateway: SocketAddr, + published: &str, + pin: &Path, + config: &Path, + overrides: &[String], + ) -> Output { + let target = SandboxSshTarget { + gateway: GATEWAY, + workspace: WORKSPACE, + sandbox: SANDBOX, + }; + let pin = KnownHostsPin::at(pin.to_path_buf(), SANDBOX, WORKSPACE).unwrap(); + let host_key = HostKeyCheck::for_relay(published.parse().unwrap(), pin).unwrap(); + let proxy_command = sandbox_proxy_command( + env!("CARGO_BIN_EXE_openshell"), + &format!("http://{gateway}"), + &target, + "test-token", + host_key, + ); + let mut ssh = tokio::process::Command::new("ssh"); + ssh.args(["-F", "/dev/null", "-o", "BatchMode=yes"]) + .args(overrides.iter().flat_map(|option| ["-o", option])) + .args(sandbox_ssh_args(&proxy_command, "ERROR")) + .args(["sandbox", COMMAND]); + finished(ssh, config, None).await + } - /// Runs [`COMMAND`] with the `ssh` arguments the CLI and TUI build for - /// a session whose published fingerprint is `published`. `ssh` takes - /// the first value of an option, so `overrides` win. - async fn ssh_as_client( - &self, - published: &str, - pin: &Path, - config: &Path, - overrides: &[String], - ) -> Output { - let target = SandboxSshTarget { - gateway: GATEWAY, - workspace: WORKSPACE, - sandbox: SANDBOX, - }; - let pin = KnownHostsPin::at(pin.to_path_buf(), SANDBOX, WORKSPACE).unwrap(); - let host_key = HostKeyCheck::for_relay(published.parse().unwrap(), pin).unwrap(); - let proxy_command = sandbox_proxy_command( - env!("CARGO_BIN_EXE_openshell"), - &format!("http://{}", self.gateway), - &target, - "test-token", - host_key, - ); - let mut ssh = tokio::process::Command::new("ssh"); - ssh.args(["-F", "/dev/null", "-o", "BatchMode=yes"]) - .args(overrides.iter().flat_map(|option| ["-o", option])) - .args(sandbox_ssh_args(&proxy_command, "ERROR")) - .args(["sandbox", COMMAND]); - finished(ssh, config, None).await - } + /// Runs [`COMMAND`] through the config entry `sandbox ssh-config` + /// prints, with no other configuration and no options of its own. + async fn ssh_through_config_entry(gateway: SocketAddr, config: &Path) -> Output { + let gateway_dir = config.join("openshell/gateways").join(GATEWAY); + std::fs::create_dir_all(&gateway_dir).unwrap(); + let metadata = serde_json::json!({ + "name": GATEWAY, + "gateway_endpoint": format!("http://{gateway}"), + "gateway_port": gateway.port(), + "is_remote": false, + "auth_mode": "plaintext", + }); + std::fs::write(gateway_dir.join("metadata.json"), metadata.to_string()).unwrap(); - /// Runs [`COMMAND`] through the config entry `sandbox ssh-config` - /// prints, with no other configuration and no options of its own. - async fn ssh_through_config_entry(&self, config: &Path) -> Output { - let gateway_dir = config.join("openshell/gateways").join(GATEWAY); - std::fs::create_dir_all(&gateway_dir).unwrap(); - let metadata = serde_json::json!({ - "name": GATEWAY, - "gateway_endpoint": format!("http://{}", self.gateway), - "gateway_port": self.gateway.port(), - "is_remote": false, - "auth_mode": "plaintext", - }); - std::fs::write(gateway_dir.join("metadata.json"), metadata.to_string()).unwrap(); - - let mut print_entry = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell")); - print_entry.args([ - "--gateway", - GATEWAY, - "--workspace", - WORKSPACE, - "sandbox", - "ssh-config", - SANDBOX, - ]); - let printed = finished(print_entry, config, None).await; - assert!( - printed.status.success(), - "{}", - String::from_utf8_lossy(&printed.stderr) - ); - let entry = config.join("ssh_config_entry"); - std::fs::write(&entry, printed.stdout).unwrap(); - - let mut ssh = tokio::process::Command::new("ssh"); - ssh.arg("-F") - .arg(entry) - .args(["-o", "BatchMode=yes"]) - .args([format!("openshell-{SANDBOX}.{WORKSPACE}").as_str(), COMMAND]); - finished(ssh, config, None).await - } + let print_entry = + format!("--gateway {GATEWAY} --workspace {WORKSPACE} sandbox ssh-config {SANDBOX}"); + let printed = openshell(&print_entry, config, None).await; + assert!( + printed.status.success(), + "{}", + String::from_utf8_lossy(&printed.stderr) + ); + let entry = config.join("ssh_config_entry"); + std::fs::write(&entry, printed.stdout).unwrap(); + + let mut ssh = tokio::process::Command::new("ssh"); + ssh.arg("-F") + .arg(entry) + .args(["-o", "BatchMode=yes"]) + .args([format!("openshell-{SANDBOX}.{WORKSPACE}").as_str(), COMMAND]); + finished(ssh, config, None).await } /// The proxy accepts and forwards any key here, so the refusal is `ssh`'s @@ -639,21 +515,21 @@ mod openssh { #[tokio::test] async fn ssh_refuses_a_key_missing_from_its_own_known_hosts_file() { let key = host_key(1); - let sandbox = SshSandbox::presenting(&key).await; - let scratch = Scratch::new(); - let proxy_pin = scratch.pin_named("proxy"); - let ssh_pin = scratch.pin_named("ssh"); + let gateway = sandbox_presenting(&key).await; + let scratch = tempfile::tempdir().unwrap(); + let proxy_pin = scratch.path().join("known_hosts/proxy"); + let ssh_pin = scratch.path().join("known_hosts/ssh"); let another_key = pin_content(Some(&host_key(2))); write_pin(&ssh_pin, &another_key); - let ssh = sandbox - .ssh_as_client( - "unverified", - &proxy_pin, - &scratch.config(), - &[format!("UserKnownHostsFile=\"{}\"", ssh_pin.display())], - ) - .await; + let ssh = ssh_as_client( + gateway, + "unverified", + &proxy_pin, + scratch.path(), + &[format!("UserKnownHostsFile=\"{}\"", ssh_pin.display())], + ) + .await; let stderr = String::from_utf8_lossy(&ssh.stderr); assert_eq!(ssh.status.code(), Some(255), "{stderr}"); @@ -673,13 +549,11 @@ mod openssh { #[tokio::test] async fn ssh_with_the_client_arguments_reaches_the_published_sandbox() { let key = host_key(1); - let sandbox = SshSandbox::presenting(&key).await; - let scratch = Scratch::new(); - let pin = scratch.pin(); + let gateway = sandbox_presenting(&key).await; + let scratch = tempfile::tempdir().unwrap(); + let pin = scratch.path().join("known_hosts/pin"); - let ssh = sandbox - .ssh_as_client(&fingerprint(&key), &pin, &scratch.config(), &[]) - .await; + let ssh = ssh_as_client(gateway, &fingerprint(&key), &pin, scratch.path(), &[]).await; let stderr = String::from_utf8_lossy(&ssh.stderr); assert!(ssh.status.success(), "{stderr}"); @@ -694,13 +568,11 @@ mod openssh { async fn ssh_with_the_client_arguments_never_reaches_another_server() { let presented = host_key(1); let published = host_key(2); - let sandbox = SshSandbox::presenting(&presented).await; - let scratch = Scratch::new(); - let pin = scratch.pin(); + let gateway = sandbox_presenting(&presented).await; + let scratch = tempfile::tempdir().unwrap(); + let pin = scratch.path().join("known_hosts/pin"); - let ssh = sandbox - .ssh_as_client(&fingerprint(&published), &pin, &scratch.config(), &[]) - .await; + let ssh = ssh_as_client(gateway, &fingerprint(&published), &pin, scratch.path(), &[]).await; let stderr = String::from_utf8_lossy(&ssh.stderr); assert_eq!(ssh.status.code(), Some(255), "{stderr}"); @@ -717,16 +589,16 @@ mod openssh { #[tokio::test] async fn ssh_connects_through_the_generated_config_entry_despite_a_stale_pin() { let key = host_key(1); - let sandbox = SshSandbox::presenting(&key).await; - let scratch = Scratch::new(); + let gateway = sandbox_presenting(&key).await; + let scratch = tempfile::tempdir().unwrap(); let pin = scratch - .config() + .path() .join("openshell/known_hosts") .join(GATEWAY) .join(format!("openshell-{SANDBOX}.{WORKSPACE}")); write_pin(&pin, &pin_content(Some(&host_key(2)))); - let ssh = sandbox.ssh_through_config_entry(&scratch.config()).await; + let ssh = ssh_through_config_entry(gateway, scratch.path()).await; let stderr = String::from_utf8_lossy(&ssh.stderr); assert!(ssh.status.success(), "{stderr}"); diff --git a/crates/openshell-core/src/forward.rs b/crates/openshell-core/src/forward.rs index c9fc794414..f99efed0a5 100644 --- a/crates/openshell-core/src/forward.rs +++ b/crates/openshell-core/src/forward.rs @@ -370,8 +370,9 @@ fn proxy_option_takes_value(arg: &str) -> bool { } fn proxy_option_has_inline_value(arg: &str) -> bool { - arg.split_once('=') - .is_some_and(|(option, _)| proxy_option_takes_value(option)) + ["--gateway=", "--workspace=", "--token=", "--gateway-name="] + .iter() + .any(|prefix| arg.starts_with(prefix)) } fn is_outer_ssh_option_start(arg: &str) -> bool { @@ -980,19 +981,26 @@ pub fn sandbox_proxy_command( /// Build the `ssh` options shared by every sandbox session. /// -/// Command line options beat `ssh_config`, so connection sharing stays off and -/// the host-key options hold whatever the user's config says. +/// Every sandbox is addressed as the host `sandbox`, so a user's `ControlMaster` +/// or `ControlPath` would reuse one sandbox's connection for another. Command +/// line options beat `ssh_config`, so connection sharing is turned off here. pub fn sandbox_ssh_args(proxy: &SandboxProxyCommand, log_level: &str) -> Vec { let host_key_options = proxy .host_key .ssh_options() .into_iter() .map(|(key, value)| format!("{key}={value}")); - std::iter::once(format!("ProxyCommand={}", proxy.command)) - .chain(host_key_options) - .chain([format!("LogLevel={log_level}")]) - .flat_map(|option| ["-o".to_string(), option]) - .collect() + [ + format!("ProxyCommand={}", proxy.command), + format!("LogLevel={log_level}"), + "ControlMaster=no".to_string(), + "ControlPath=none".to_string(), + "ControlPersist=no".to_string(), + ] + .into_iter() + .chain(host_key_options) + .flat_map(|option| ["-o".to_string(), option]) + .collect() } /// Error returned when a `CreateSshSessionResponse` fails validation. @@ -1437,33 +1445,10 @@ mod tests { } #[test] - fn sandbox_proxy_command_hands_the_host_key_check_to_the_proxy() { + fn sandbox_ssh_args_disable_connection_sharing() { let dir = tempfile::tempdir().unwrap(); - let known_hosts = dir.path().join("pin dir").join("openshell-sbx-1.team-a"); - let args = sandbox_ssh_args( - &sandbox_proxy("/Application Support/openshell", &known_hosts), - "ERROR", - ); - - let proxy_command = args[1].strip_prefix("ProxyCommand=").unwrap(); - let words = split_proxy_command_words(proxy_command); - assert_eq!( - words[words.len() - 4..], - [ - "--host-key", - HOST_KEY_FINGERPRINT, - "--known-hosts", - known_hosts.to_str().unwrap(), - ] - ); - } - - #[test] - fn sandbox_ssh_args_trust_only_the_pinned_host_key() { - let dir = tempfile::tempdir().unwrap(); - let known_hosts = dir.path().join("openshell-sbx-1.team-a"); - let args = sandbox_ssh_args(&sandbox_proxy("openshell", &known_hosts), "DEBUG"); - + let proxy = sandbox_proxy("openshell", &dir.path().join("pin")); + let args = sandbox_ssh_args(&proxy, "DEBUG"); let options: Vec<&str> = args .chunks(2) .map(|pair| { @@ -1472,22 +1457,10 @@ mod tests { }) .collect(); assert!(options[0].starts_with("ProxyCommand=openshell ssh-proxy ")); - assert_eq!( - options[1..], - [ - "StrictHostKeyChecking=yes", - &format!("UserKnownHostsFile=\"{}\"", known_hosts.display()), - "GlobalKnownHostsFile=/dev/null", - "HostKeyAlias=openshell-sbx-1.team-a", - "HostKeyAlgorithms=ssh-ed25519", - "VerifyHostKeyDNS=no", - "UpdateHostKeys=no", - "ControlMaster=no", - "ControlPath=none", - "ControlPersist=no", - "LogLevel=DEBUG", - ] - ); + assert!(options.contains(&"LogLevel=DEBUG")); + for option in ["ControlMaster=no", "ControlPath=none", "ControlPersist=no"] { + assert!(options.contains(&option), "missing {option}"); + } } /// Helper: return the concatenation of characters that appear outside @@ -1869,80 +1842,27 @@ mod tests { )); } - /// The argument list of a CLI background forward. - fn generated_forward_argv(proxy: &SandboxProxyCommand) -> Vec { - let mut argv = vec!["/usr/bin/ssh".to_string()]; - argv.extend(sandbox_ssh_args(proxy, "ERROR")); - argv.extend( - [ - "-o", - "ServerAliveInterval=15", - "-o", - "ServerAliveCountMax=3", - "-o", - "ForkAfterAuthentication=no", - "-N", - "-o", - "ExitOnForwardFailure=yes", - "-o", - "SetEnv=OPENSHELL_FORWARD_SANDBOX_ID=id-1", - "-L", - "127.0.0.1:80:127.0.0.1:80", - "sandbox", - ] - .map(str::to_string), - ); - argv - } - - fn assert_matches_only_its_own_forward(tokens: &[&str]) { - let matches = |port, workspace, sandbox, sandbox_id| { - args_match_ssh_forward( - tokens, - port, - Some(workspace), - Some(sandbox), - Some(sandbox_id), - ) - }; - assert!(matches(80, "team-a", "sbx-1", "id-1")); - assert!(!matches(8080, "team-a", "sbx-1", "id-1")); - assert!(!matches(80, "team-b", "sbx-1", "id-1")); - assert!(!matches(80, "team-a", "sbx-2", "id-1")); - assert!(!matches(80, "team-a", "sbx-1", "id-2")); - } - - #[test] - fn ssh_forward_command_matches_exact_argv_with_host_key_flags() { - let dir = tempfile::tempdir().unwrap(); - let known_hosts = dir.path().join("pin dir").join("openshell-sbx-1.team-a"); - let argv = generated_forward_argv(&sandbox_proxy( - "/Application Support/openshell", - &known_hosts, - )); - - let expanded: Vec = argv - .iter() - .flat_map(|arg| expand_proxy_command_arg(arg)) - .collect(); - let tokens: Vec<&str> = expanded.iter().map(String::as_str).collect(); - - assert_matches_only_its_own_forward(&tokens); - } - #[test] - fn ssh_forward_command_matches_whitespace_split_line_with_host_key_flags() { + fn ssh_forward_command_matches_generated_host_key_flags() { let dir = tempfile::tempdir().unwrap(); - let known_hosts = dir.path().join("openshell-sbx-1.team-a"); - let line = - generated_forward_argv(&sandbox_proxy("/path/openshell", &known_hosts)).join(" "); - - assert_matches_only_its_own_forward(&line.split_whitespace().collect::>()); + let proxy = sandbox_proxy("/path/openshell", &dir.path().join("pin")); + let mut argv = vec!["/usr/bin/ssh".to_string()]; + argv.extend(sandbox_ssh_args(&proxy, "ERROR")); + argv.extend(["-N", "-L", "80:127.0.0.1:80", "sandbox"].map(str::to_string)); + let line = argv.join(" "); - let inline = format!( - "ssh -o ProxyCommand=openshell ssh-proxy --sandbox=sbx-1 --workspace=team-a --host-key={HOST_KEY_FINGERPRINT} --known-hosts=/pins/openshell-sbx-1.team-a -N -o SetEnv=OPENSHELL_FORWARD_SANDBOX_ID=id-1 -L 80:127.0.0.1:80 sandbox" - ); - assert_matches_only_its_own_forward(&inline.split_whitespace().collect::>()); + assert!(command_matches_ssh_forward( + &line, + 80, + Some("team-a"), + Some("sbx-1") + )); + assert!(!command_matches_ssh_forward( + &line, + 80, + Some("team-b"), + Some("sbx-1") + )); } #[test] diff --git a/crates/openshell-core/src/paths.rs b/crates/openshell-core/src/paths.rs index c08b33b23c..0470ab6764 100644 --- a/crates/openshell-core/src/paths.rs +++ b/crates/openshell-core/src/paths.rs @@ -44,22 +44,6 @@ pub fn openshell_config_dir() -> Result { Ok(xdg_config_dir()?.join("openshell")) } -/// The directory name of a gateway under the `OpenShell` config directory. A -/// gateway reached by `--gateway-endpoint` alone is named by its URL, so -/// every character outside `[A-Za-z0-9._-]` becomes `_`. -pub fn gateway_dir_name(gateway: &str) -> String { - gateway - .chars() - .map(|ch| { - if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_') { - ch - } else { - '_' - } - }) - .collect() -} - /// Resolve the XDG state base directory. /// /// Returns `$XDG_STATE_HOME` if set, otherwise `$HOME/.local/state`. diff --git a/crates/openshell-core/src/ssh_host_key.rs b/crates/openshell-core/src/ssh_host_key.rs index 9b86074ba3..239776d68f 100644 --- a/crates/openshell-core/src/ssh_host_key.rs +++ b/crates/openshell-core/src/ssh_host_key.rs @@ -12,7 +12,7 @@ use std::fmt; use std::fs; use std::io::{self, Write as _}; -use std::path::{Component, MAIN_SEPARATOR, Path, PathBuf}; +use std::path::{MAIN_SEPARATOR, Path, PathBuf}; use std::str::FromStr; use std::sync::atomic::{AtomicU64, Ordering}; @@ -110,42 +110,29 @@ impl fmt::Display for HostKeyExpectation { } impl HostKeyExpectation { - /// Interprets the fingerprint field of a gateway response. The policy - /// decides only what an empty field means; it never changes the outcome - /// for a non-empty one. + /// Interprets the fingerprint field of a gateway response. + /// `allow_unverified` decides only what an empty field means. pub fn from_published( published: &str, sandbox: &str, - policy: UnverifiedPolicy, + allow_unverified: bool, ) -> Result { if !published.is_empty() { - return published.parse().map(Self::Fingerprint); - } - match policy { - UnverifiedPolicy::Allow => Ok(Self::AcceptAny), - UnverifiedPolicy::Refuse => Err(HostKeyError::Unpublished { + published.parse().map(Self::Fingerprint) + } else if allow_unverified { + Ok(Self::AcceptAny) + } else { + Err(HostKeyError::Unpublished { sandbox: sandbox.to_string(), - }), + }) } } } -/// Whether a sandbox with no published fingerprint may be reached unverified. -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum UnverifiedPolicy { - Refuse, - Allow, -} - -impl UnverifiedPolicy { - /// `Allow` only when `OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY` is `1`. - pub fn from_env() -> Self { - if std::env::var(ALLOW_UNVERIFIED_ENV).is_ok_and(|value| value == "1") { - Self::Allow - } else { - Self::Refuse - } - } +/// Whether a sandbox with no published fingerprint may be reached unverified: +/// only when `OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY` is `1`. +pub fn allow_unverified_host_key() -> bool { + std::env::var(ALLOW_UNVERIFIED_ENV).is_ok_and(|value| value == "1") } #[derive(Debug, thiserror::Error)] @@ -250,9 +237,14 @@ impl KnownHostsPin { fn under(config_dir: &Path, target: &SandboxSshTarget<'_>) -> Result { let alias = host_key_alias(target.sandbox, target.workspace); - let gateway = crate::paths::gateway_dir_name(target.gateway); + // A gateway reached by its endpoint alone is named by its URL. + let gateway = target.gateway.replace( + |ch: char| !ch.is_ascii_alphanumeric() && !matches!(ch, '.' | '-' | '_'), + "_", + ); let path = config_dir.join("known_hosts").join(&gateway).join(&alias); - if !is_single_normal_component(&gateway) || !is_single_normal_component(&alias) { + let is_one_component = |name: &str| Path::new(name).file_name() == Some(name.as_ref()); + if !is_one_component(&gateway) || !is_one_component(&alias) { return Err(HostKeyError::UnusablePath(path)); } Self::at(path, target.sandbox, target.workspace) @@ -280,11 +272,6 @@ impl KnownHostsPin { ("HostKeyAlgorithms", HOST_KEY_ALGORITHM.to_string()), ("VerifyHostKeyDNS", "no".to_string()), ("UpdateHostKeys", "no".to_string()), - // A shared connection is reused without running the ProxyCommand, - // so its host key is not checked. - ("ControlMaster", "no".to_string()), - ("ControlPath", "none".to_string()), - ("ControlPersist", "no".to_string()), ] } @@ -345,26 +332,20 @@ impl KnownHostsPin { } fn write_then_rename(&self, content: &str) -> io::Result<()> { - let (Some(dir), Some(name)) = (self.path.parent(), self.path.file_name()) else { - return Err(io::ErrorKind::InvalidInput.into()); - }; + let dir = self.path.parent().ok_or(io::ErrorKind::InvalidInput)?; create_owner_only_dir(dir)?; - let (temp_path, mut temp_file) = loop { - let sequence = TEMP_FILE_SEQUENCE.fetch_add(1, Ordering::Relaxed); - let candidate = dir.join(format!( - ".{}.{}.{sequence}.tmp", - name.to_string_lossy(), - std::process::id() - )); - match owner_only_create_new().open(&candidate) { - Ok(file) => break (candidate, file), - Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {} - Err(err) => return Err(err), - } - }; - let written = temp_file.write_all(content.as_bytes()); - drop(temp_file); - let replaced = written.and_then(|()| fs::rename(&temp_path, &self.path)); + // Unique among the writers of this process. `create_new` refuses a + // file or link someone else left there. + let sequence = TEMP_FILE_SEQUENCE.fetch_add(1, Ordering::Relaxed); + let temp_path = dir.join(format!(".{}.{sequence}.tmp", std::process::id())); + let mut options = fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); + let replaced = options + .open(&temp_path) + .and_then(|mut file| file.write_all(content.as_bytes())) + .and_then(|()| fs::rename(&temp_path, &self.path)); if replaced.is_err() { let _ = fs::remove_file(&temp_path); } @@ -376,25 +357,6 @@ impl KnownHostsPin { } } -fn is_single_normal_component(name: &str) -> bool { - let mut components = Path::new(name).components(); - matches!( - (components.next(), components.next()), - (Some(Component::Normal(component)), None) if component.to_str() == Some(name) - ) -} - -fn owner_only_create_new() -> fs::OpenOptions { - let mut options = fs::OpenOptions::new(); - options.write(true).create_new(true); - #[cfg(unix)] - { - use std::os::unix::fs::OpenOptionsExt as _; - options.mode(0o600); - } - options -} - fn create_owner_only_dir(dir: &Path) -> io::Result<()> { let mut builder = fs::DirBuilder::new(); builder.recursive(true); @@ -419,12 +381,12 @@ impl HostKeyCheck { pub fn for_session( session: &CreateSshSessionResponse, target: &SandboxSshTarget<'_>, - policy: UnverifiedPolicy, + allow_unverified: bool, ) -> Result { let expectation = HostKeyExpectation::from_published( &session.host_key_fingerprint, target.sandbox, - policy, + allow_unverified, )?; Self::for_relay(expectation, KnownHostsPin::for_target(target)?) } @@ -696,16 +658,10 @@ mod tests { [&u32::try_from(bytes.len()).unwrap().to_be_bytes(), bytes].concat() } + /// Four bytes of padding: the gate does not check block alignment. fn packet(payload: &[u8]) -> Vec { - let padding = 4 + (8 - (5 + payload.len() + 4) % 8) % 8; - let mut packet = u32::try_from(1 + payload.len() + padding) - .unwrap() - .to_be_bytes() - .to_vec(); - packet.push(u8::try_from(padding).unwrap()); - packet.extend_from_slice(payload); - packet.resize(packet.len() + padding, 0); - packet + let length = u32::try_from(payload.len() + 5).unwrap().to_be_bytes(); + [&length[..], &[4], payload, &[0; 4]].concat() } fn kexinit() -> Vec { @@ -769,13 +725,11 @@ mod tests { .into_gate() } - struct Relayed { - released: Vec, - result: Result<(), HostKeyError>, - pin: Option, - } - - fn relay<'a>(expectation: &str, chunks: impl IntoIterator) -> Relayed { + /// What the gate released, how it ended, and the pin file it left. + fn relay<'a>( + expectation: &str, + chunks: impl IntoIterator, + ) -> (Vec, Result<(), HostKeyError>, Option) { let config_dir = tempfile::tempdir().unwrap(); let mut gate = gate_in(config_dir.path(), expectation); let mut released = Vec::new(); @@ -783,11 +737,8 @@ mod tests { released.extend(gate.push(chunk)?); Ok(()) }); - Relayed { - released, - result, - pin: fs::read_to_string(pin_in(config_dir.path()).path()).ok(), - } + let pin = fs::read_to_string(pin_in(config_dir.path()).path()).ok(); + (released, result, pin) } /// Every way to deliver `stream` in two chunks, then one byte at a time. @@ -831,101 +782,39 @@ mod tests { } } - #[test] - fn expectation_text_round_trips() { - let verified: HostKeyExpectation = FINGERPRINT_A.parse().unwrap(); - assert_eq!( - verified, - HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)) - ); - assert_eq!(verified.to_string(), FINGERPRINT_A); - - let unverified: HostKeyExpectation = "unverified".parse().unwrap(); - assert_eq!(unverified, HostKeyExpectation::AcceptAny); - assert_eq!(unverified.to_string(), "unverified"); - - for text in ["", "Unverified", "any", "SHA256:short"] { - assert!( - matches!( - text.parse::(), - Err(HostKeyError::BadFingerprint) - ), - "{text:?}" - ); - } - } - #[test] fn expectation_policy_never_changes_a_published_outcome() { - for policy in [UnverifiedPolicy::Refuse, UnverifiedPolicy::Allow] { + for allow in [false, true] { assert_eq!( - HostKeyExpectation::from_published(FINGERPRINT_A, "my-sb", policy).unwrap(), + HostKeyExpectation::from_published(FINGERPRINT_A, "my-sb", allow).unwrap(), HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)), - "{policy:?}" + "{allow}" ); for malformed in ["unverified", "SHA256:inconsistent", "MD5:00:11"] { assert!( matches!( - HostKeyExpectation::from_published(malformed, "my-sb", policy), + HostKeyExpectation::from_published(malformed, "my-sb", allow), Err(HostKeyError::BadFingerprint) ), - "{malformed:?} {policy:?}" + "{malformed:?} {allow}" ); } } } - #[test] - fn expectation_for_an_unpublished_key_follows_the_policy() { - assert_eq!( - HostKeyExpectation::from_published("", "my-sb", UnverifiedPolicy::Allow).unwrap(), - HostKeyExpectation::AcceptAny - ); - - let refused = - HostKeyExpectation::from_published("", "my-sb", UnverifiedPolicy::Refuse).unwrap_err(); - assert!(matches!( - &refused, - HostKeyError::Unpublished { sandbox } if sandbox == "my-sb" - )); - assert_eq!( - refused.to_string(), - "sandbox 'my-sb' has no published SSH host key, so its SSH server cannot be verified.\n \ - If the gateway predates sandbox SSH identities, upgrade it. Otherwise stop and start the sandbox so the gateway assigns a key.\n \ - To connect without verification, set OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY=1." - ); - } - #[test] fn unverified_policy_allows_only_when_the_variable_is_one() { Environment::new() .set(ALLOW_UNVERIFIED_ENV, "1") - .run(|| assert_eq!(UnverifiedPolicy::from_env(), UnverifiedPolicy::Allow)); + .run(|| assert!(allow_unverified_host_key())); for value in ["", "0", "true", "yes", "11"] { Environment::new() .set(ALLOW_UNVERIFIED_ENV, value) - .run(|| assert_eq!(UnverifiedPolicy::from_env(), UnverifiedPolicy::Refuse)); + .run(|| assert!(!allow_unverified_host_key())); } Environment::new() .remove(ALLOW_UNVERIFIED_ENV) - .run(|| assert_eq!(UnverifiedPolicy::from_env(), UnverifiedPolicy::Refuse)); - } - - #[test] - fn host_key_alias_names_sandbox_then_workspace() { - assert_eq!(host_key_alias("demo", "default"), "openshell-demo.default"); - assert_eq!(host_key_alias("demo", "beta"), "openshell-demo.beta"); - } - - #[test] - fn pin_for_target_lives_under_the_config_dir() { - with_config_dir(|config_dir| { - let pin = KnownHostsPin::for_target(&TARGET).unwrap(); - assert_eq!( - pin.path(), - config_dir.join("known_hosts/prod/openshell-my-sb.default") - ); - }); + .run(|| assert!(!allow_unverified_host_key())); } #[test] @@ -966,52 +855,43 @@ mod tests { #[test] fn pin_rejects_names_that_are_not_one_path_component() { let config_dir = Path::new("/home/u/.config/openshell"); - for gateway in ["", ".", ".."] { - let target = SandboxSshTarget { gateway, ..TARGET }; - assert!( - matches!( - KnownHostsPin::under(config_dir, &target), - Err(HostKeyError::UnusablePath(_)) - ), - "gateway {gateway:?}" - ); - } - for (sandbox, workspace) in [("a/b", "default"), ("my-sb", "../x"), ("my-sb", "x/")] { + for (gateway, workspace, sandbox) in [ + ("", "default", "my-sb"), + (".", "default", "my-sb"), + ("..", "default", "my-sb"), + ("prod", "default", "a/b"), + ("prod", "../x", "my-sb"), + ("prod", "x/", "my-sb"), + ] { let target = SandboxSshTarget { + gateway, workspace, sandbox, - ..TARGET }; assert!( matches!( KnownHostsPin::under(config_dir, &target), Err(HostKeyError::UnusablePath(_)) ), - "sandbox {sandbox:?} workspace {workspace:?}" + "{target:?}" ); } } #[test] fn pin_rejects_paths_ssh_would_expand_or_split() { - for config_dir in [ - "/home/100%/openshell", - "/home/a\"b/openshell", - "/home/${HOME}/openshell", - "/home/a\nb/openshell", + for path in [ + "/home/100%/pin", + "/home/a\"b/pin", + "/home/${HOME}/pin", + "/home/a\nb/pin", #[cfg(unix)] - "/home/a\\b/openshell", - "relative/openshell", + "/home/a\\b/pin", + "relative/pin", + "", + "/", + "/tmp/..", ] { - assert!( - matches!( - KnownHostsPin::under(Path::new(config_dir), &TARGET), - Err(HostKeyError::UnusablePath(_)) - ), - "{config_dir:?}" - ); - } - for path in ["/tmp/pin%h", "pin", "", "/", "/tmp/.."] { assert!( matches!( KnownHostsPin::at(PathBuf::from(path), "my-sb", "default"), @@ -1022,77 +902,23 @@ mod tests { } } - #[test] - fn pin_store_writes_the_header_and_one_key_line() { - let config_dir = tempfile::tempdir().unwrap(); - let pin = pin_in(config_dir.path()); - - pin.store(&ed25519_blob(KEY_A)).unwrap(); - - assert_eq!( - fs::read_to_string(pin.path()).unwrap(), - format!( - "# Managed by OpenShell. Rewritten from the gateway-published fingerprint; do not edit.\n\ - openshell-my-sb.default ssh-ed25519 {KNOWN_HOSTS_KEY_A}\n" - ) - ); - } - - #[cfg(unix)] - #[test] - fn pin_file_is_owner_only_in_the_owner_only_directory_it_creates() { - use std::os::unix::fs::PermissionsExt as _; - let config_dir = tempfile::tempdir().unwrap(); - let pin = pin_in(config_dir.path()); - let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777; - - pin.store(&ed25519_blob(KEY_A)).unwrap(); - assert_eq!(mode(pin.path()), 0o600); - assert_eq!(mode(pin.path().parent().unwrap()), 0o700); - - fs::set_permissions(pin.path(), fs::Permissions::from_mode(0o644)).unwrap(); - pin.store(&ed25519_blob(KEY_B)).unwrap(); - assert_eq!(mode(pin.path()), 0o600); - } - + /// The pin directory is created owner-only; a directory that already + /// exists keeps its mode. #[cfg(unix)] #[test] - fn pin_store_leaves_the_mode_of_an_existing_directory_alone() { + fn pin_store_creates_owner_only_files_and_leaves_existing_directories_alone() { use std::os::unix::fs::PermissionsExt as _; let shared = tempfile::tempdir().unwrap(); fs::set_permissions(shared.path(), fs::Permissions::from_mode(0o755)).unwrap(); - let pin = KnownHostsPin::at(shared.path().join("pin"), "my-sb", "default").unwrap(); - - pin.store(&ed25519_blob(KEY_A)).unwrap(); - - let mode = fs::metadata(shared.path()).unwrap().permissions().mode(); - assert_eq!(mode & 0o777, 0o755); - } - - #[cfg(unix)] - #[test] - fn pin_store_replaces_the_file_only_when_the_key_differs() { - use std::os::unix::fs::MetadataExt as _; - let config_dir = tempfile::tempdir().unwrap(); - let pin = pin_in(config_dir.path()); - let inode = || fs::metadata(pin.path()).unwrap().ino(); - - pin.store(&ed25519_blob(KEY_A)).unwrap(); - let first = inode(); - pin.store(&ed25519_blob(KEY_A)).unwrap(); - assert_eq!(inode(), first); + let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777; - pin.store(&ed25519_blob(KEY_B)).unwrap(); - assert_ne!(inode(), first); - assert_eq!( - fs::read_to_string(pin.path()).unwrap(), - pin_file(KNOWN_HOSTS_KEY_B) - ); - let entries: Vec<_> = fs::read_dir(pin.path().parent().unwrap()) - .unwrap() - .map(|entry| entry.unwrap().file_name()) - .collect(); - assert_eq!(entries, ["openshell-my-sb.default"]); + for pin in [shared.path().join("pin"), shared.path().join("new/pin")] { + let pin = KnownHostsPin::at(pin, "my-sb", "default").unwrap(); + pin.store(&ed25519_blob(KEY_A)).unwrap(); + assert_eq!(mode(pin.path()), 0o600); + } + assert_eq!(mode(&shared.path().join("new")), 0o700); + assert_eq!(mode(shared.path()), 0o755); } #[test] @@ -1129,43 +955,26 @@ mod tests { } } + /// Before `ssh` can read the pin, it holds the published key or no key. #[test] - fn check_for_session_drops_a_stale_key_before_ssh_can_read_it() { + fn check_for_session_keeps_only_the_published_key() { with_config_dir(|config_dir| { let pin = pin_in(config_dir); - pin.store(&ed25519_blob(KEY_B)).unwrap(); - - let check = HostKeyCheck::for_session( - &published(FINGERPRINT_A), - &TARGET, - UnverifiedPolicy::Refuse, - ) - .unwrap(); - - assert_eq!(fs::read_to_string(pin.path()).unwrap(), PIN_HEADER); - assert_eq!(check.unverified_warning("my-sb"), None); let path = pin.path().display().to_string(); - assert_eq!( - check.proxy_flags(), - ["--host-key", FINGERPRINT_A, "--known-hosts", path.as_str()] - ); - assert_eq!(check.ssh_options(), pin.ssh_options()); - }); - } + for (stored, kept) in [ + (KEY_B, PIN_HEADER.to_string()), + (KEY_A, pin_file(KNOWN_HOSTS_KEY_A)), + ] { + pin.store(&ed25519_blob(stored)).unwrap(); - #[test] - fn check_for_session_keeps_the_published_key() { - with_config_dir(|config_dir| { - let pin = pin_in(config_dir); - pin.store(&ed25519_blob(KEY_A)).unwrap(); - - for policy in [UnverifiedPolicy::Refuse, UnverifiedPolicy::Allow] { let check = - HostKeyCheck::for_session(&published(FINGERPRINT_A), &TARGET, policy).unwrap(); + HostKeyCheck::for_session(&published(FINGERPRINT_A), &TARGET, false).unwrap(); + + assert_eq!(fs::read_to_string(pin.path()).unwrap(), kept); assert_eq!(check.unverified_warning("my-sb"), None); assert_eq!( - fs::read_to_string(pin.path()).unwrap(), - pin_file(KNOWN_HOSTS_KEY_A) + check.proxy_flags(), + ["--host-key", FINGERPRINT_A, "--known-hosts", path.as_str()] ); } }); @@ -1174,11 +983,14 @@ mod tests { #[test] fn check_for_session_refuses_an_unpublished_key_without_touching_the_pin() { with_config_dir(|config_dir| { - let error = - HostKeyCheck::for_session(&published(""), &TARGET, UnverifiedPolicy::Refuse) - .unwrap_err(); + let error = HostKeyCheck::for_session(&published(""), &TARGET, false).unwrap_err(); - assert!(matches!(error, HostKeyError::Unpublished { .. })); + assert_eq!( + error.to_string(), + "sandbox 'my-sb' has no published SSH host key, so its SSH server cannot be verified.\n \ + If the gateway predates sandbox SSH identities, upgrade it. Otherwise stop and start the sandbox so the gateway assigns a key.\n \ + To connect without verification, set OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY=1." + ); assert!(!config_dir.exists()); }); } @@ -1189,8 +1001,7 @@ mod tests { let pin = pin_in(config_dir); pin.store(&ed25519_blob(KEY_A)).unwrap(); - let check = HostKeyCheck::for_session(&published(""), &TARGET, UnverifiedPolicy::Allow) - .unwrap(); + let check = HostKeyCheck::for_session(&published(""), &TARGET, true).unwrap(); assert_eq!(fs::read_to_string(pin.path()).unwrap(), PIN_HEADER); assert_eq!( @@ -1201,23 +1012,6 @@ mod tests { }); } - #[test] - fn check_for_relay_drops_a_stale_key_before_the_first_byte() { - let config_dir = tempfile::tempdir().unwrap(); - let path = config_dir.path().join("known_hosts/prod/pin"); - let stale = KnownHostsPin::at(path.clone(), "my-sb", "default").unwrap(); - stale.store(&ed25519_blob(KEY_B)).unwrap(); - - let check = HostKeyCheck::for_relay( - HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)), - stale, - ) - .unwrap(); - - assert_eq!(fs::read_to_string(&path).unwrap(), PIN_HEADER); - assert_eq!(check.unverified_warning("my-sb"), None); - } - /// Other connections to the sandbox start while one stores the key and /// hands the reply to `ssh`, which must then find the key in the pin. #[test] @@ -1260,26 +1054,6 @@ mod tests { }); } - #[test] - fn check_reports_a_pin_directory_that_cannot_be_created() { - let config_dir = tempfile::tempdir().unwrap(); - let blocker = config_dir.path().join("known_hosts"); - fs::write(&blocker, "a file where the directory should be").unwrap(); - let pin = KnownHostsPin::at(blocker.join("prod/pin"), "my-sb", "default").unwrap(); - - let error = HostKeyCheck::for_relay( - HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)), - pin, - ) - .unwrap_err(); - - assert!(matches!(error, HostKeyError::Io(_))); - assert!( - error.to_string().contains("known_hosts/prod/pin"), - "{error}" - ); - } - /// Asserts that `stream` is refused as `is_expected` says, that nothing /// past `before` is released and that the pin file is left as `pin`, /// however `stream` is split. @@ -1293,57 +1067,36 @@ mod tests { ) { for chunks in every_split(stream) { let at = chunks[0].len(); - let relayed = relay(expectation, chunks); + let (released, result, kept) = relay(expectation, chunks); assert!( - relayed.result.as_ref().is_err_and(&is_expected), - "{name} split at {at}: {:?}", - relayed.result - ); - assert!( - before.starts_with(&relayed.released), - "{name} split at {at}" + result.as_ref().is_err_and(&is_expected), + "{name} split at {at}: {result:?}" ); - assert_eq!(relayed.pin.as_deref(), Some(pin), "{name} split at {at}"); + assert!(before.starts_with(&released), "{name} split at {at}"); + assert_eq!(kept.as_deref(), Some(pin), "{name} split at {at}"); } } #[test] fn gate_relays_an_exchange_unchanged_and_pins_its_key() { - let greeting = b"Welcome\r\nnotice: maintenance at noon\n"; + let curve = curve25519_exchange(KEY_A).concat(); + let gex = group_exchange(KEY_A).concat(); + let other = curve25519_exchange(KEY_B).concat(); + let greeted = [&b"Welcome\r\nnotice: maintenance at noon\n"[..], &curve].concat(); for (name, expectation, stream, key) in [ - ( - "curve25519", - FINGERPRINT_A, - curve25519_exchange(KEY_A).concat(), - KNOWN_HOSTS_KEY_A, - ), - ( - "group exchange", - FINGERPRINT_A, - group_exchange(KEY_A).concat(), - KNOWN_HOSTS_KEY_A, - ), - ( - "unverified", - "unverified", - curve25519_exchange(KEY_B).concat(), - KNOWN_HOSTS_KEY_B, - ), - ( - "lines before the identification line", - FINGERPRINT_A, - [greeting, curve25519_exchange(KEY_A).concat().as_slice()].concat(), - KNOWN_HOSTS_KEY_A, - ), + ("curve25519", FINGERPRINT_A, &curve, KNOWN_HOSTS_KEY_A), + ("group exchange", FINGERPRINT_A, &gex, KNOWN_HOSTS_KEY_A), + ("unverified", "unverified", &other, KNOWN_HOSTS_KEY_B), + ("greeting", FINGERPRINT_A, &greeted, KNOWN_HOSTS_KEY_A), ] { - for chunks in every_split(&stream) { + for chunks in every_split(stream) { let at = chunks[0].len(); - let relayed = relay(expectation, chunks); + let (released, result, pin) = relay(expectation, chunks); - assert!(relayed.result.is_ok(), "{name} split at {at}"); - assert_eq!(relayed.released, stream, "{name} split at {at}"); - assert_eq!(relayed.pin, Some(pin_file(key)), "{name} split at {at}"); + assert!(result.is_ok(), "{name} split at {at}"); + assert_eq!(&released, stream, "{name} split at {at}"); + assert_eq!(pin, Some(pin_file(key)), "{name} split at {at}"); } } } @@ -1368,15 +1121,6 @@ mod tests { assert_eq!(gate.push(&after).unwrap(), after); } - #[test] - fn gate_releases_whole_lines_that_precede_the_identification_line() { - let config_dir = tempfile::tempdir().unwrap(); - let mut gate = gate_in(config_dir.path(), FINGERPRINT_A); - assert_eq!(gate.push(b"Welcome\r\nnot").unwrap(), b"Welcome\r\n"); - assert_eq!(gate.push(b"ice\nSSH-2.0-Open").unwrap(), b"notice\n"); - assert_eq!(gate.push(b"Shell\r\n").unwrap(), b"SSH-2.0-OpenShell\r\n"); - } - #[test] fn gate_withholds_a_reply_carrying_another_key() { for exchange in [curve25519_exchange(KEY_B), group_exchange(KEY_B)] { @@ -1430,76 +1174,35 @@ mod tests { const NO_HOST_KEY: &str = "key exchange finished without an Ed25519 host key"; const OUT_OF_ORDER: &str = "packet outside the key exchange order"; - /// `ssh` discards the decoy, a packet it does not expect or was told to + /// `ssh` discards a decoy, a packet it does not expect or was told to /// skip, and would act on the reply behind it. - #[test] - fn gate_withholds_a_reply_carrying_another_key_behind_a_decoy() { - let decoy = kex_reply(31, &ed25519_blob(KEY_A)); - let other = ed25519_blob(KEY_B); - let decoy_pinned = pin_file(KNOWN_HOSTS_KEY_A); - for (name, before, withheld, pin) in [ - ( - "decoy before KEXINIT", - BANNER.to_vec(), - [decoy.as_slice(), &kexinit(), &kex_reply(31, &other)].concat(), - PIN_HEADER, - ), - ( - "decoy as the announced wrong guess", - BANNER.to_vec(), - [ - kexinit_ending(&[1, 0, 0, 0, 0]), - decoy.clone(), - kex_reply(31, &other), - ] - .concat(), - PIN_HEADER, - ), - ( - "decoy before the reply", - [BANNER, &kexinit(), &decoy].concat(), - kex_reply(31, &other), - decoy_pinned.as_str(), - ), - ( - "decoy after the group", - [BANNER, &kexinit(), &group(), &decoy].concat(), - kex_reply(33, &other), - decoy_pinned.as_str(), - ), - ] { - assert_refused( - name, - FINGERPRINT_A, - &before, - &[before.as_slice(), &withheld, &newkeys(), &ENCRYPTED].concat(), - |error| matches!(error, HostKeyError::Malformed(OUT_OF_ORDER)), - pin, - ); - } - } - #[test] fn gate_rejects_a_packet_outside_the_key_exchange_order() { let ignore = packet(&[2, 0, 0, 0, 0]); let reply = kex_reply(31, &ed25519_blob(KEY_A)); + let other = kex_reply(31, &ed25519_blob(KEY_B)); let rsa = [string(b"ssh-rsa"), string(&[1, 0, 1]), string(&[0x7f; 257])].concat(); + let decoy_first = [reply.as_slice(), &kexinit(), &other].concat(); + let other_gex = kex_reply(33, &ed25519_blob(KEY_B)); + let rsa_gex = kex_reply(33, &rsa); + let wrong_guess = [&kexinit_ending(&[1, 0, 0, 0, 0])[..], &reply, &other].concat(); let after_kexinit = [BANNER, &kexinit()].concat(); let after_group = [BANNER, &kexinit(), &group()].concat(); let after_reply = [BANNER, &kexinit(), &reply].concat(); + let after_group_reply = [BANNER, &kexinit(), &group(), &reply].concat(); let pinned = pin_file(KNOWN_HOSTS_KEY_A); for (name, before, offending) in [ + ("decoy before KEXINIT", BANNER, &decoy_first), + ("decoy as the announced wrong guess", BANNER, &wrong_guess), + ("decoy before the reply", &after_reply, &other), + ("decoy after the group", &after_group_reply, &other_gex), ("IGNORE first", BANNER, &ignore), ("NEWKEYS first", BANNER, &newkeys()), ("KEXINIT cut short", BANNER, &kexinit_ending(&[])), ("IGNORE before the reply", &after_kexinit, &ignore), ("second KEXINIT", &after_kexinit, &kexinit()), ("second group", &after_group, &group()), - ( - "group exchange reply without an Ed25519 key", - &after_group, - &kex_reply(33, &rsa), - ), + ("non-Ed25519 group reply", &after_group, &rsa_gex), ("IGNORE before NEWKEYS", &after_reply, &ignore), ("second reply", &after_reply, &reply), ] { @@ -1509,7 +1212,7 @@ mod tests { before, &[before, offending, &newkeys(), &ENCRYPTED].concat(), |error| matches!(error, HostKeyError::Malformed(OUT_OF_ORDER)), - if before == after_reply { + if before.ends_with(&reply) { &pinned } else { PIN_HEADER @@ -1539,80 +1242,29 @@ mod tests { } } - #[test] - fn gate_withholds_the_reply_when_the_key_cannot_be_pinned() { - let [before, reply, _] = curve25519_exchange(KEY_A); - let config_dir = tempfile::tempdir().unwrap(); - let pin = pin_in(config_dir.path()); - let mut gate = gate_in(config_dir.path(), FINGERPRINT_A); - gate.push(&before).unwrap(); - let pin_dir = pin.path().parent().unwrap(); - fs::remove_dir_all(pin_dir).unwrap(); - fs::write(pin_dir, "a file where the directory should be").unwrap(); - - assert!(matches!(gate.push(&reply), Err(HostKeyError::Io(_)))); - } - #[test] fn gate_rejects_a_malformed_packet() { let length = |length: u32| [&length.to_be_bytes()[..], &[0; 16]].concat(); - for (packet, expected) in [ - (length(0), "packet length is out of range"), - (length(1), "packet length is out of range"), - (length(262_145), "packet length is out of range"), - (length(u32::MAX), "packet length is out of range"), - (vec![0, 0, 0, 2, 1, 31], "packet padding leaves no payload"), - (vec![0, 0, 0, 2, 9, 31], "packet padding leaves no payload"), + let padded = |padding: u8| vec![0, 0, 0, 2, padding, 31]; + let mut long_line = b"SSH-2.0-".to_vec(); + long_line.resize(8192, b'a'); + for (before, rest, expected) in [ + (BANNER, length(0), "packet length is out of range"), + (BANNER, length(1), "packet length is out of range"), + (BANNER, length(262_145), "packet length is out of range"), + (BANNER, length(u32::MAX), "packet length is out of range"), + (BANNER, padded(1), "packet padding leaves no payload"), + (BANNER, padded(9), "packet padding leaves no payload"), + (b"".as_slice(), long_line, "identification line is too long"), ] { assert_refused( expected, FINGERPRINT_A, - BANNER, - &[BANNER, &packet].concat(), + before, + &[before, &rest].concat(), |error| matches!(error, HostKeyError::Malformed(message) if *message == expected), PIN_HEADER, ); } } - - #[test] - fn gate_relays_the_largest_packet_ssh_accepts() { - let mut largest_group = 262_144_u32.to_be_bytes().to_vec(); - largest_group.extend_from_slice(&[4, 31]); - largest_group.resize(4 + 262_144, 2); - let [_, reply, after] = group_exchange(KEY_A); - let stream = [BANNER, &kexinit(), &largest_group, &reply, &after].concat(); - - let relayed = relay(FINGERPRINT_A, stream.chunks(4096)); - - assert!(relayed.result.is_ok()); - assert!(relayed.released == stream); - assert_eq!(relayed.pin, Some(pin_file(KNOWN_HOSTS_KEY_A))); - } - - #[test] - fn gate_rejects_an_identification_line_that_is_too_long() { - let mut too_long = b"SSH-2.0-".to_vec(); - too_long.resize(8192, b'a'); - assert_refused( - "line of 8192 bytes", - FINGERPRINT_A, - b"", - &too_long, - |error| { - matches!( - error, - HostKeyError::Malformed("identification line is too long") - ) - }, - PIN_HEADER, - ); - - let mut longest = vec![b'a'; 8191]; - longest.push(b'\n'); - let stream = [longest, curve25519_exchange(KEY_A).concat()].concat(); - let relayed = relay(FINGERPRINT_A, [stream.as_slice()]); - assert!(relayed.result.is_ok()); - assert!(relayed.released == stream); - } } diff --git a/crates/openshell-tui/src/lib.rs b/crates/openshell-tui/src/lib.rs index cb41900651..6e8b0ad9b4 100644 --- a/crates/openshell-tui/src/lib.rs +++ b/crates/openshell-tui/src/lib.rs @@ -1219,7 +1219,7 @@ use openshell_core::forward::{ sandbox_ssh_args, shell_escape, validate_ssh_session_response, }; use openshell_core::ssh_host_key::{ - HostKeyCheck, HostKeyError, SandboxSshTarget, UnverifiedPolicy, + HostKeyCheck, HostKeyError, SandboxSshTarget, allow_unverified_host_key, }; /// Decide how the sandbox's SSH host key is checked and build the @@ -1231,7 +1231,7 @@ fn sandbox_proxy_for_session( gateway_url: &str, target: &SandboxSshTarget<'_>, ) -> std::result::Result<(SandboxProxyCommand, Option), HostKeyError> { - let host_key = HostKeyCheck::for_session(session, target, UnverifiedPolicy::from_env())?; + let host_key = HostKeyCheck::for_session(session, target, allow_unverified_host_key())?; let unverified_warning = host_key.unverified_warning(target.sandbox); let proxy_command = sandbox_proxy_command( &exe.to_string_lossy(), @@ -3271,23 +3271,6 @@ mod exec_command_tests { } } -#[cfg(test)] -mod host_key_refusal_tests { - use super::*; - - #[test] - fn host_key_refusal_fits_the_status_line() { - let refusal = HostKeyError::Unpublished { - sandbox: "my-sb".to_string(), - }; - - assert_eq!( - host_key_refused_status(&refusal), - "sandbox 'my-sb' has no published SSH host key, so its SSH server cannot be verified." - ); - } -} - #[cfg(test)] mod draft_approve_all_message_tests { use super::*; From ee95035de59b918be1126c78a3fdc971ae3329fb Mon Sep 17 00:00:00 2001 From: Adrien Langou Date: Wed, 18 Nov 2026 16:37:41 +0100 Subject: [PATCH 3/6] fix(cli): keep existing SSH setups working with host key checks Address the compatibility review of the host key verification change. A sandbox with no published fingerprint connects with a warning instead of being refused, so gateways that predate sandbox SSH identities keep working. The OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY opt-out is no longer needed. SSH config entries and running clients from an earlier release keep working: ssh-proxy relays without a check and warns, since their ssh accepts any host key anyway. A '%' in the config directory is escaped for ssh instead of rejected. Harden the relay gate: an identification line is at most 255 bytes, so the gate and every ssh release hand over to packets at the same byte, and a group exchange reply is accepted only after its group. Signed-off-by: Adrien Langou --- crates/openshell-cli/src/main.rs | 35 ++-- crates/openshell-cli/src/ssh.rs | 88 +++++----- .../tests/ssh_proxy_host_key_integration.rs | 80 ++++----- .../tests/ssh_proxy_shutdown_integration.rs | 4 - crates/openshell-core/src/ssh_host_key.rs | 160 +++++++----------- crates/openshell-tui/src/lib.rs | 6 +- docs/how-it-works/sandboxes/overview.mdx | 18 +- skills/openshell-cli/SKILL.md | 8 +- 8 files changed, 179 insertions(+), 220 deletions(-) diff --git a/crates/openshell-cli/src/main.rs b/crates/openshell-cli/src/main.rs index ad36490cd1..8d8b4df287 100644 --- a/crates/openshell-cli/src/main.rs +++ b/crates/openshell-cli/src/main.rs @@ -699,13 +699,13 @@ enum Commands { name: Option, /// SSH host key the sandbox must present: the fingerprint the gateway - /// published, or `unverified`. Required in token mode; name mode asks - /// the gateway instead. + /// published, or `unverified`. Token mode only; name mode asks the + /// gateway instead. #[arg(long, requires = "known_hosts", conflicts_with = "name")] host_key: Option, /// Known-hosts file in which the verified host key is pinned for `ssh`. - /// Required in both modes. + /// Without it the host key is not verified. #[arg(long)] known_hosts: Option, }, @@ -4199,16 +4199,27 @@ async fn run_async() -> Result<()> { match (gateway, sandbox, token, server, gateway_name, name) { // Token mode (existing behavior): pre-created session credentials. (Some(gw), Some(sandbox), Some(tok), _, gateway_name_opt, _) => { - let (Some(expectation), Some(known_hosts)) = (host_key, known_hosts) else { - eprintln!( - "openshell: this SSH connection was started by an OpenShell client that predates host-key verification and is refused.\n\ - Restart that client (the openshell command or openshell term) and connect again." - ); - std::process::exit(1); + let host_key = match (host_key, known_hosts) { + (Some(expectation), Some(known_hosts)) => Some( + KnownHostsPin::at(known_hosts, &sandbox, &cli.workspace) + .and_then(|pin| HostKeyCheck::for_relay(expectation, pin)) + .map_err(|err| miette::miette!("{err}"))?, + ), + // The `ssh` of such a client accepts any host key. + (None, None) => { + eprintln!( + "openshell: warning: this SSH connection was started by an OpenShell client that predates host-key verification, so the SSH host key is NOT verified.\n\ + Restart that client (the openshell command or openshell term)." + ); + None + } + // Nothing would pin a key for the `ssh` that reads the file. + _ => { + return Err(miette::miette!( + "--known-hosts requires --host-key when --token is given" + )); + } }; - let host_key = KnownHostsPin::at(known_hosts, &sandbox, &cli.workspace) - .and_then(|pin| HostKeyCheck::for_relay(expectation, pin)) - .map_err(|err| miette::miette!("{err}"))?; let mut effective_tls = match gateway_name_opt { Some(ref g) => tls.with_gateway_name(g), None => tls, diff --git a/crates/openshell-cli/src/ssh.rs b/crates/openshell-cli/src/ssh.rs index 1b88f030ea..d0f368ae85 100644 --- a/crates/openshell-cli/src/ssh.rs +++ b/crates/openshell-cli/src/ssh.rs @@ -24,7 +24,7 @@ use openshell_core::proto::{ }; use openshell_core::ssh_host_key::{ HostKeyCheck, HostKeyExpectation, HostKeyGate, KnownHostsPin, SandboxSshTarget, - allow_unverified_host_key, host_key_alias as host_alias, + host_key_alias as host_alias, }; use openshell_core::upload::{ DestinationKind as UploadDestinationKind, ExpectedDestination, UploadPlan, @@ -228,9 +228,7 @@ async fn ssh_session_config( workspace, sandbox: name, }; - let host_key = - HostKeyCheck::for_session(&session.response, &target, allow_unverified_host_key()) - .into_diagnostic()?; + let host_key = HostKeyCheck::for_session(&session.response, &target).into_diagnostic()?; if let Some(warning) = host_key.unverified_warning(name) { eprintln!("{warning}"); } @@ -1996,14 +1994,15 @@ async fn sandbox_sync_down_directory( stream_sandbox_tar(session, tar_cmd, dest_path).await } -/// Run the SSH proxy, connecting stdin/stdout to the gateway. +/// Run the SSH proxy, connecting stdin/stdout to the gateway. Without +/// `host_key`, the sandbox's host key is not checked. pub async fn sandbox_ssh_proxy( gateway_url: &str, sandbox_name: &str, workspace: &str, token: &str, tls: &TlsOptions, - host_key: HostKeyCheck, + host_key: Option, ) -> Result<()> { let server = grpc_server_from_ssh_gateway_url(gateway_url)?; let mut client = grpc_client(&server, tls).await?; @@ -2024,7 +2023,7 @@ pub async fn sandbox_ssh_proxy( workspace, token, replica.as_ref(), - host_key.into_gate(), + host_key.map(HostKeyCheck::into_gate), ) .await } @@ -2035,7 +2034,7 @@ async fn relay_ssh_proxy( workspace: &str, token: &str, replica: Option<&AsciiMetadataValue>, - mut gate: HostKeyGate, + mut gate: Option, ) -> Result<()> { let init = TcpForwardFrame { payload: Some(openshell_core::proto::tcp_forward_frame::Payload::Init( @@ -2103,12 +2102,17 @@ async fn relay_ssh_proxy( else { continue; }; - let data = gate.push(&data).unwrap_or_else(|refusal| { - // Printed rather than returned: ssh passes this text to the user - // as is, and a returned error would be reformatted as a report. - eprintln!("openshell: {refusal}"); - std::process::exit(1) - }); + let data = match gate.as_mut().map(|gate| gate.push(&data)) { + None => data, + Some(Ok(released)) => released, + Some(Err(refusal)) => { + // Printed rather than returned: ssh passes this text to the + // user as is, and a returned error would be reformatted as + // a report. + eprintln!("openshell: {refusal}"); + std::process::exit(1) + } + }; if data.is_empty() { continue; } @@ -2171,9 +2175,9 @@ fn grpc_server_from_ssh_gateway_url(gateway_url: &str) -> Result { /// creates a fresh session on every invocation. /// /// `known_hosts` is the pin file the config entry also names as its -/// `UserKnownHostsFile`. An entry without it tells `ssh` to accept any host -/// key, so it is refused. `gateway_name` is the gateway the entry names, when -/// it names one. +/// `UserKnownHostsFile`. An entry without it predates host-key verification +/// and tells `ssh` to accept any host key: it keeps working, with a warning. +/// `gateway_name` is the gateway the entry names, when it names one. pub async fn sandbox_ssh_proxy_by_name( server: &str, gateway_name: Option<&str>, @@ -2182,25 +2186,24 @@ pub async fn sandbox_ssh_proxy_by_name( workspace: &str, known_hosts: Option, ) -> Result<()> { - let Some(known_hosts) = known_hosts else { + let session = create_ssh_session(server, name, tls, workspace, None).await?; + let gate = if let Some(known_hosts) = known_hosts { + let expectation = + HostKeyExpectation::from_published(&session.response.host_key_fingerprint) + .into_diagnostic()?; + let pin = KnownHostsPin::at(known_hosts, name, workspace).into_diagnostic()?; + let host_key = HostKeyCheck::for_relay(expectation, pin).into_diagnostic()?; + if let Some(warning) = host_key.unverified_warning(name) { + eprintln!("{warning}"); + } + Some(host_key.into_gate()) + } else { eprintln!( "{}", - stale_ssh_config_refusal(gateway_name, workspace, name) + stale_ssh_config_warning(gateway_name, workspace, name) ); - std::process::exit(1); + None }; - let session = create_ssh_session(server, name, tls, workspace, None).await?; - let expectation = HostKeyExpectation::from_published( - &session.response.host_key_fingerprint, - name, - allow_unverified_host_key(), - ) - .into_diagnostic()?; - let pin = KnownHostsPin::at(known_hosts, name, workspace).into_diagnostic()?; - let host_key = HostKeyCheck::for_relay(expectation, pin).into_diagnostic()?; - if let Some(warning) = host_key.unverified_warning(name) { - eprintln!("{warning}"); - } let server = grpc_server_from_ssh_gateway_url(&session.gateway_url)?; let client = grpc_client(&server, tls).await?; relay_ssh_proxy( @@ -2209,19 +2212,19 @@ pub async fn sandbox_ssh_proxy_by_name( workspace, &session.response.token, session.replica.as_ref(), - host_key.into_gate(), + gate, ) .await } -fn stale_ssh_config_refusal(gateway: Option<&str>, workspace: &str, sandbox: &str) -> String { +fn stale_ssh_config_warning(gateway: Option<&str>, workspace: &str, sandbox: &str) -> String { let gateway = gateway.map_or_else(String::new, |gateway| { format!(" -g {}", shell_escape(gateway)) }); let openshell = format!("openshell{gateway} --workspace {}", shell_escape(workspace)); let name = shell_escape(sandbox); format!( - "openshell: this SSH config entry for sandbox '{sandbox}' (workspace '{workspace}') predates host-key verification and is refused.\n\ + "openshell: warning: this SSH config entry for sandbox '{sandbox}' (workspace '{workspace}') predates host-key verification, so the SSH host key is NOT verified.\n\ Regenerate it: {openshell} sandbox connect {name} --editor vscode\n\ Or replace the entry with the output of: {openshell} sandbox ssh-config {name}" ) @@ -2242,7 +2245,7 @@ fn render_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result String { format!("ssh-proxy --server http://{gateway} --name {SANDBOX} --workspace {WORKSPACE}") } -/// Runs `command` to completion. `allow_unverified` is the value of the -/// variable that permits a sandbox with no published fingerprint. -async fn finished( - mut command: tokio::process::Command, - config: &Path, - allow_unverified: Option<&str>, -) -> Output { - match allow_unverified { - Some(value) => command.env(ALLOW_UNVERIFIED, value), - None => command.env_remove(ALLOW_UNVERIFIED), - }; +/// Runs `command` to completion. +async fn finished(mut command: tokio::process::Command, config: &Path) -> Output { let run = command .env("XDG_CONFIG_HOME", config) .env("OPENSHELL_TELEMETRY_ENABLED", "false") @@ -218,10 +208,10 @@ async fn finished( } /// Runs `openshell` with `args`, which are separated by single spaces. -async fn openshell(args: &str, config: &Path, allow_unverified: Option<&str>) -> Output { +async fn openshell(args: &str, config: &Path) -> Output { let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell")); command.args(args.split(' ')); - finished(command, config, allow_unverified).await + finished(command, config).await } fn mismatch_message(published: &PrivateKey, presented: &PrivateKey) -> String { @@ -282,7 +272,7 @@ async fn name_mode_proxy_withholds_a_key_the_gateway_did_not_publish() { let scratch = tempfile::tempdir().unwrap(); let pin = scratch.path().join("known_hosts/pin"); - let proxy = openshell(&name_mode(gateway, &pin), scratch.path(), None).await; + let proxy = openshell(&name_mode(gateway, &pin), scratch.path()).await; let stderr = String::from_utf8_lossy(&proxy.stderr); assert_eq!(proxy.status.code(), Some(1), "{stderr}"); @@ -297,8 +287,9 @@ async fn name_mode_proxy_withholds_a_key_the_gateway_did_not_publish() { assert_eq!(std::fs::read_to_string(&pin).unwrap(), pin_content(None)); } +/// A gateway that predates sandbox SSH identities publishes nothing. #[tokio::test] -async fn name_mode_proxy_reaches_a_sandbox_with_no_published_key_only_when_allowed() { +async fn name_mode_proxy_warns_and_relays_when_the_gateway_published_no_key() { let key = host_key(1); let transcript = handshake(&key).concat(); let gateway = Gateway { @@ -309,24 +300,12 @@ async fn name_mode_proxy_reaches_a_sandbox_with_no_published_key_only_when_allow .await; let scratch = tempfile::tempdir().unwrap(); let pin = scratch.path().join("known_hosts/pin"); - let args = name_mode(gateway, &pin); - - let refused = openshell(&args, scratch.path(), None).await; - - let stderr = String::from_utf8_lossy(&refused.stderr); - assert!(!refused.status.success()); - assert_eq!(refused.stdout, b""); - assert!( - stderr.contains("sandbox 'my-sb' has no published SSH host key"), - "{stderr}" - ); - assert!(!pin.exists()); - let allowed = openshell(&args, scratch.path(), Some("1")).await; + let proxy = openshell(&name_mode(gateway, &pin), scratch.path()).await; - let stderr = String::from_utf8_lossy(&allowed.stderr); - assert!(allowed.status.success(), "{stderr}"); - assert_eq!(allowed.stdout, transcript); + let stderr = String::from_utf8_lossy(&proxy.stderr); + assert!(proxy.status.success(), "{stderr}"); + assert_eq!(proxy.stdout, transcript); assert!( stderr.contains("warning: SSH host key of sandbox 'my-sb' is NOT verified"), "{stderr}" @@ -338,12 +317,13 @@ async fn name_mode_proxy_reaches_a_sandbox_with_no_published_key_only_when_allow } /// An `ssh` started by a client or a config entry from before host-key -/// verification accepts any key, so its proxy must not relay. +/// verification accepts any key. It keeps working, and is told so. #[tokio::test] -async fn proxy_refuses_a_caller_that_predates_host_key_verification() { +async fn proxy_warns_and_relays_for_a_caller_that_predates_host_key_verification() { + let transcript = handshake(&host_key(2)).concat(); let gateway = Gateway { published: fingerprint(&host_key(1)), - sandbox: sending(handshake(&host_key(1)).concat()), + sandbox: sending(transcript.clone()), } .serve() .await; @@ -352,20 +332,23 @@ async fn proxy_refuses_a_caller_that_predates_host_key_verification() { "ssh-proxy --gateway http://{gateway}/proxy/connect --sandbox {SANDBOX} --workspace {WORKSPACE} --token test-token" ); - for (args, refused) in [ + for (args, caller) in [ (old_client, "started by an OpenShell client that predates"), ( old_name_mode(gateway), "SSH config entry for sandbox 'my-sb'", ), ] { - let proxy = openshell(&args, scratch.path(), None).await; + let proxy = openshell(&args, scratch.path()).await; let stderr = String::from_utf8_lossy(&proxy.stderr); - assert_eq!(proxy.status.code(), Some(1), "{stderr}"); - assert_eq!(proxy.stdout, b""); - assert!(stderr.contains(refused), "{stderr}"); - assert!(stderr.contains("and is refused"), "{stderr}"); + assert!(proxy.status.success(), "{stderr}"); + assert_eq!(proxy.stdout, transcript); + assert!(stderr.contains(caller), "{stderr}"); + assert!( + stderr.contains("the SSH host key is NOT verified"), + "{stderr}" + ); } } @@ -474,7 +457,7 @@ mod openssh { .args(overrides.iter().flat_map(|option| ["-o", option])) .args(sandbox_ssh_args(&proxy_command, "ERROR")) .args(["sandbox", COMMAND]); - finished(ssh, config, None).await + finished(ssh, config).await } /// Runs [`COMMAND`] through the config entry `sandbox ssh-config` @@ -493,7 +476,7 @@ mod openssh { let print_entry = format!("--gateway {GATEWAY} --workspace {WORKSPACE} sandbox ssh-config {SANDBOX}"); - let printed = openshell(&print_entry, config, None).await; + let printed = openshell(&print_entry, config).await; assert!( printed.status.success(), "{}", @@ -507,7 +490,7 @@ mod openssh { .arg(entry) .args(["-o", "BatchMode=yes"]) .args([format!("openshell-{SANDBOX}.{WORKSPACE}").as_str(), COMMAND]); - finished(ssh, config, None).await + finished(ssh, config).await } /// The proxy accepts and forwards any key here, so the refusal is `ssh`'s @@ -546,12 +529,13 @@ mod openssh { /// `ssh` is already running when the proxy pins the key, so this holds /// only because `ssh` reads its known-hosts file when it processes the key /// exchange reply, not when it starts. Host-key verification rests on it. + /// The `%` is one `ssh` would expand in the path it is given. #[tokio::test] async fn ssh_with_the_client_arguments_reaches_the_published_sandbox() { let key = host_key(1); let gateway = sandbox_presenting(&key).await; let scratch = tempfile::tempdir().unwrap(); - let pin = scratch.path().join("known_hosts/pin"); + let pin = scratch.path().join("100% known_hosts/pin"); let ssh = ssh_as_client(gateway, &fingerprint(&key), &pin, scratch.path(), &[]).await; @@ -591,14 +575,14 @@ mod openssh { let key = host_key(1); let gateway = sandbox_presenting(&key).await; let scratch = tempfile::tempdir().unwrap(); - let pin = scratch - .path() + let config = scratch.path().join("100% config"); + let pin = config .join("openshell/known_hosts") .join(GATEWAY) .join(format!("openshell-{SANDBOX}.{WORKSPACE}")); write_pin(&pin, &pin_content(Some(&host_key(2)))); - let ssh = ssh_through_config_entry(gateway, scratch.path()).await; + let ssh = ssh_through_config_entry(gateway, &config).await; let stderr = String::from_utf8_lossy(&ssh.stderr); assert!(ssh.status.success(), "{stderr}"); diff --git a/crates/openshell-cli/tests/ssh_proxy_shutdown_integration.rs b/crates/openshell-cli/tests/ssh_proxy_shutdown_integration.rs index 8afb20bdbd..76f9b32f5e 100644 --- a/crates/openshell-cli/tests/ssh_proxy_shutdown_integration.rs +++ b/crates/openshell-cli/tests/ssh_proxy_shutdown_integration.rs @@ -72,10 +72,6 @@ async fn proxy_exits_with_stdin_open(relay_status: tonic::Status) { "repro", "--token", "test-token", - "--host-key", - "unverified", - "--known-hosts", - &config.path().join("known_hosts").display().to_string(), ]) .env("XDG_CONFIG_HOME", config.path()) .env("OPENSHELL_TELEMETRY_ENABLED", "false") diff --git a/crates/openshell-core/src/ssh_host_key.rs b/crates/openshell-core/src/ssh_host_key.rs index 239776d68f..be9efc9624 100644 --- a/crates/openshell-core/src/ssh_host_key.rs +++ b/crates/openshell-core/src/ssh_host_key.rs @@ -24,14 +24,15 @@ use crate::proto::CreateSshSessionResponse; const FINGERPRINT_PREFIX: &str = "SHA256:"; const UNVERIFIED: &str = "unverified"; -const ALLOW_UNVERIFIED_ENV: &str = "OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY"; const HOST_KEY_ALGORITHM: &str = "ssh-ed25519"; const PIN_HEADER: &str = "# Managed by OpenShell. Rewritten from the gateway-published fingerprint; do not edit.\n"; /// `string "ssh-ed25519"`, then the length prefix of the 32-byte key. const ED25519_BLOB_PREFIX: &[u8] = b"\0\0\0\x0bssh-ed25519\0\0\0\x20"; const ED25519_KEY_LEN: usize = 32; -const MAX_BANNER_LINE_LEN: usize = 8192; +/// The longest identification line of RFC 4253, with its line ending. Every +/// `ssh` release reads a line this short whole; older ones split a longer one. +const MAX_BANNER_LINE_LEN: usize = 255; /// The padding length byte plus one payload byte, up to the largest packet /// `ssh` accepts. const PACKET_LENGTHS: std::ops::RangeInclusive = 2..=262_144; @@ -84,7 +85,7 @@ impl HostKeyFingerprint { #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum HostKeyExpectation { Fingerprint(HostKeyFingerprint), - /// Only when the gateway published nothing and the user opted in. + /// Only when the gateway published nothing. AcceptAny, } @@ -110,39 +111,19 @@ impl fmt::Display for HostKeyExpectation { } impl HostKeyExpectation { - /// Interprets the fingerprint field of a gateway response. - /// `allow_unverified` decides only what an empty field means. - pub fn from_published( - published: &str, - sandbox: &str, - allow_unverified: bool, - ) -> Result { - if !published.is_empty() { - published.parse().map(Self::Fingerprint) - } else if allow_unverified { + /// Interprets the fingerprint field of a gateway response. A gateway + /// that predates sandbox SSH identities leaves it empty. + pub fn from_published(published: &str) -> Result { + if published.is_empty() { Ok(Self::AcceptAny) } else { - Err(HostKeyError::Unpublished { - sandbox: sandbox.to_string(), - }) + published.parse().map(Self::Fingerprint) } } } -/// Whether a sandbox with no published fingerprint may be reached unverified: -/// only when `OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY` is `1`. -pub fn allow_unverified_host_key() -> bool { - std::env::var(ALLOW_UNVERIFIED_ENV).is_ok_and(|value| value == "1") -} - #[derive(Debug, thiserror::Error)] pub enum HostKeyError { - #[error( - "sandbox '{sandbox}' has no published SSH host key, so its SSH server cannot be verified.\n \ - If the gateway predates sandbox SSH identities, upgrade it. Otherwise stop and start the sandbox so the gateway assigns a key.\n \ - To connect without verification, set {ALLOW_UNVERIFIED_ENV}=1." - )] - Unpublished { sandbox: String }, #[error("SSH host key fingerprint is not 'SHA256:' followed by 43 unpadded base64 characters")] BadFingerprint, #[error( @@ -165,7 +146,7 @@ pub enum HostKeyError { Malformed(&'static str), #[error( "SSH known-hosts path '{}' cannot be used: it must be absolute, be built from plain gateway, \ - sandbox and workspace names, and contain no '%', '\"', '\\', \"${{\" or newline", + sandbox and workspace names, and contain no '\"', '\\', \"${{\" or newline", .0.display() )] UnusablePath(PathBuf), @@ -210,12 +191,12 @@ impl KnownHostsPin { /// The `ssh-proxy` side: the path the `ssh` invocation or config block /// handed over. pub fn at(path: PathBuf, sandbox: &str, workspace: &str) -> Result { - // `ssh` expands `%` tokens and `${VAR}` in both UserKnownHostsFile and - // ProxyCommand, a quote or newline would end the value early, and a - // backslash escapes the character after it. Where the backslash - // separates path components, rejecting it would reject every path. + // `ssh` expands `${VAR}` in UserKnownHostsFile and has no escape for + // it, a quote or newline would end the value early, and a backslash + // escapes the character after it. Where the backslash separates path + // components, rejecting it would reject every path. let expands_or_breaks = |text: &str| { - text.contains(['%', '"', '\n']) + text.contains(['"', '\n']) || text.contains("${") || (MAIN_SEPARATOR != '\\' && text.contains('\\')) }; @@ -258,6 +239,12 @@ impl KnownHostsPin { &self.path } + /// The path as `ssh` must be given it in `UserKnownHostsFile` and inside + /// a `ProxyCommand`: it expands `%` tokens in both, and `%%` is a `%`. + pub fn ssh_path(&self) -> String { + self.path.display().to_string().replace('%', "%%") + } + /// The options of every sandbox SSH connection that decide which host key /// it trusts, in the order they are passed. Rendered as `-o key=value` on /// a command line and as `key value` lines in a config block. @@ -266,7 +253,7 @@ impl KnownHostsPin { ("StrictHostKeyChecking", "yes".to_string()), // Quoted, so that no `ssh` release splits the path at whitespace, // `=` or an apostrophe. - ("UserKnownHostsFile", format!("\"{}\"", self.path.display())), + ("UserKnownHostsFile", format!("\"{}\"", self.ssh_path())), ("GlobalKnownHostsFile", "/dev/null".to_string()), ("HostKeyAlias", self.alias()), ("HostKeyAlgorithms", HOST_KEY_ALGORITHM.to_string()), @@ -381,13 +368,8 @@ impl HostKeyCheck { pub fn for_session( session: &CreateSshSessionResponse, target: &SandboxSshTarget<'_>, - allow_unverified: bool, ) -> Result { - let expectation = HostKeyExpectation::from_published( - &session.host_key_fingerprint, - target.sandbox, - allow_unverified, - )?; + let expectation = HostKeyExpectation::from_published(&session.host_key_fingerprint)?; Self::for_relay(expectation, KnownHostsPin::for_target(target)?) } @@ -407,7 +389,8 @@ impl HostKeyCheck { pub fn unverified_warning(&self, sandbox: &str) -> Option { matches!(self.expectation, HostKeyExpectation::AcceptAny).then(|| { format!( - "warning: SSH host key of sandbox '{sandbox}' is NOT verified (the gateway published no fingerprint)" + "warning: SSH host key of sandbox '{sandbox}' is NOT verified: the gateway published no fingerprint. \ + Upgrade a gateway that predates sandbox SSH identities; otherwise stop and start the sandbox." ) }) } @@ -416,13 +399,14 @@ impl HostKeyCheck { self.pin.ssh_options() } - /// `["--host-key", , "--known-hosts", ]` + /// `["--host-key", , "--known-hosts", ]`, for a + /// `ProxyCommand`. pub(crate) fn proxy_flags(&self) -> [String; 4] { [ "--host-key".to_string(), self.expectation.to_string(), "--known-hosts".to_string(), - self.pin.path.display().to_string(), + self.pin.ssh_path(), ] } @@ -467,7 +451,8 @@ impl HostKeyCheck { (GatePhase::KexInit, MSG_KEXINIT, _) if first_kex_packet_follows(fields) == Some(0) => { Ok(GatePhase::GroupOrReply) } - (GatePhase::GroupOrReply | GatePhase::Reply, _, Some(blob)) => { + (GatePhase::GroupOrReply, MSG_KEX_REPLY, Some(blob)) + | (GatePhase::Reply, MSG_KEX_GEX_REPLY, Some(blob)) => { self.admit(blob)?; Ok(GatePhase::NewKeys) } @@ -782,39 +767,27 @@ mod tests { } } + /// Only an empty field means that nothing was published. A malformed one + /// is refused, also when it spells `unverified`. #[test] - fn expectation_policy_never_changes_a_published_outcome() { - for allow in [false, true] { - assert_eq!( - HostKeyExpectation::from_published(FINGERPRINT_A, "my-sb", allow).unwrap(), - HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)), - "{allow}" + fn expectation_verifies_a_published_fingerprint_and_refuses_a_malformed_one() { + assert_eq!( + HostKeyExpectation::from_published(FINGERPRINT_A).unwrap(), + HostKeyExpectation::Fingerprint(fingerprint(FINGERPRINT_A)), + ); + assert_eq!( + HostKeyExpectation::from_published("").unwrap(), + HostKeyExpectation::AcceptAny, + ); + for malformed in ["unverified", "SHA256:inconsistent", "MD5:00:11"] { + assert!( + matches!( + HostKeyExpectation::from_published(malformed), + Err(HostKeyError::BadFingerprint) + ), + "{malformed:?}" ); - for malformed in ["unverified", "SHA256:inconsistent", "MD5:00:11"] { - assert!( - matches!( - HostKeyExpectation::from_published(malformed, "my-sb", allow), - Err(HostKeyError::BadFingerprint) - ), - "{malformed:?} {allow}" - ); - } - } - } - - #[test] - fn unverified_policy_allows_only_when_the_variable_is_one() { - Environment::new() - .set(ALLOW_UNVERIFIED_ENV, "1") - .run(|| assert!(allow_unverified_host_key())); - for value in ["", "0", "true", "yes", "11"] { - Environment::new() - .set(ALLOW_UNVERIFIED_ENV, value) - .run(|| assert!(!allow_unverified_host_key())); } - Environment::new() - .remove(ALLOW_UNVERIFIED_ENV) - .run(|| assert!(!allow_unverified_host_key())); } #[test] @@ -881,7 +854,6 @@ mod tests { #[test] fn pin_rejects_paths_ssh_would_expand_or_split() { for path in [ - "/home/100%/pin", "/home/a\"b/pin", "/home/${HOME}/pin", "/home/a\nb/pin", @@ -967,8 +939,7 @@ mod tests { ] { pin.store(&ed25519_blob(stored)).unwrap(); - let check = - HostKeyCheck::for_session(&published(FINGERPRINT_A), &TARGET, false).unwrap(); + let check = HostKeyCheck::for_session(&published(FINGERPRINT_A), &TARGET).unwrap(); assert_eq!(fs::read_to_string(pin.path()).unwrap(), kept); assert_eq!(check.unverified_warning("my-sb"), None); @@ -980,33 +951,19 @@ mod tests { }); } - #[test] - fn check_for_session_refuses_an_unpublished_key_without_touching_the_pin() { - with_config_dir(|config_dir| { - let error = HostKeyCheck::for_session(&published(""), &TARGET, false).unwrap_err(); - - assert_eq!( - error.to_string(), - "sandbox 'my-sb' has no published SSH host key, so its SSH server cannot be verified.\n \ - If the gateway predates sandbox SSH identities, upgrade it. Otherwise stop and start the sandbox so the gateway assigns a key.\n \ - To connect without verification, set OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY=1." - ); - assert!(!config_dir.exists()); - }); - } - #[test] fn check_for_session_never_trusts_a_cached_pin_when_unverified() { with_config_dir(|config_dir| { let pin = pin_in(config_dir); pin.store(&ed25519_blob(KEY_A)).unwrap(); - let check = HostKeyCheck::for_session(&published(""), &TARGET, true).unwrap(); + let check = HostKeyCheck::for_session(&published(""), &TARGET).unwrap(); assert_eq!(fs::read_to_string(pin.path()).unwrap(), PIN_HEADER); assert_eq!( check.unverified_warning("my-sb").unwrap(), - "warning: SSH host key of sandbox 'my-sb' is NOT verified (the gateway published no fingerprint)" + "warning: SSH host key of sandbox 'my-sb' is NOT verified: the gateway published no fingerprint. \ + Upgrade a gateway that predates sandbox SSH identities; otherwise stop and start the sandbox." ); assert_eq!(check.proxy_flags()[..2], ["--host-key", "unverified"]); }); @@ -1183,13 +1140,14 @@ mod tests { let other = kex_reply(31, &ed25519_blob(KEY_B)); let rsa = [string(b"ssh-rsa"), string(&[1, 0, 1]), string(&[0x7f; 257])].concat(); let decoy_first = [reply.as_slice(), &kexinit(), &other].concat(); + let reply_gex = kex_reply(33, &ed25519_blob(KEY_A)); let other_gex = kex_reply(33, &ed25519_blob(KEY_B)); let rsa_gex = kex_reply(33, &rsa); let wrong_guess = [&kexinit_ending(&[1, 0, 0, 0, 0])[..], &reply, &other].concat(); let after_kexinit = [BANNER, &kexinit()].concat(); let after_group = [BANNER, &kexinit(), &group()].concat(); let after_reply = [BANNER, &kexinit(), &reply].concat(); - let after_group_reply = [BANNER, &kexinit(), &group(), &reply].concat(); + let after_group_reply = [BANNER, &kexinit(), &group(), &reply_gex].concat(); let pinned = pin_file(KNOWN_HOSTS_KEY_A); for (name, before, offending) in [ ("decoy before KEXINIT", BANNER, &decoy_first), @@ -1202,6 +1160,12 @@ mod tests { ("IGNORE before the reply", &after_kexinit, &ignore), ("second KEXINIT", &after_kexinit, &kexinit()), ("second group", &after_group, &group()), + ( + "group exchange reply without a group", + &after_kexinit, + &reply_gex, + ), + ("plain reply after a group", &after_group, &reply), ("non-Ed25519 group reply", &after_group, &rsa_gex), ("IGNORE before NEWKEYS", &after_reply, &ignore), ("second reply", &after_reply, &reply), @@ -1212,7 +1176,7 @@ mod tests { before, &[before, offending, &newkeys(), &ENCRYPTED].concat(), |error| matches!(error, HostKeyError::Malformed(OUT_OF_ORDER)), - if before.ends_with(&reply) { + if before.ends_with(&reply) || before.ends_with(&reply_gex) { &pinned } else { PIN_HEADER @@ -1247,7 +1211,7 @@ mod tests { let length = |length: u32| [&length.to_be_bytes()[..], &[0; 16]].concat(); let padded = |padding: u8| vec![0, 0, 0, 2, padding, 31]; let mut long_line = b"SSH-2.0-".to_vec(); - long_line.resize(8192, b'a'); + long_line.resize(255, b'a'); for (before, rest, expected) in [ (BANNER, length(0), "packet length is out of range"), (BANNER, length(1), "packet length is out of range"), diff --git a/crates/openshell-tui/src/lib.rs b/crates/openshell-tui/src/lib.rs index 6e8b0ad9b4..fceb429a86 100644 --- a/crates/openshell-tui/src/lib.rs +++ b/crates/openshell-tui/src/lib.rs @@ -1218,9 +1218,7 @@ use openshell_core::forward::{ SandboxProxyCommand, format_gateway_url, resolve_ssh_gateway, sandbox_proxy_command, sandbox_ssh_args, shell_escape, validate_ssh_session_response, }; -use openshell_core::ssh_host_key::{ - HostKeyCheck, HostKeyError, SandboxSshTarget, allow_unverified_host_key, -}; +use openshell_core::ssh_host_key::{HostKeyCheck, HostKeyError, SandboxSshTarget}; /// Decide how the sandbox's SSH host key is checked and build the /// `ProxyCommand` that enforces it. The warning is present when the key cannot @@ -1231,7 +1229,7 @@ fn sandbox_proxy_for_session( gateway_url: &str, target: &SandboxSshTarget<'_>, ) -> std::result::Result<(SandboxProxyCommand, Option), HostKeyError> { - let host_key = HostKeyCheck::for_session(session, target, allow_unverified_host_key())?; + let host_key = HostKeyCheck::for_session(session, target)?; let unverified_warning = host_key.unverified_warning(target.sandbox); let proxy_command = sandbox_proxy_command( &exe.to_string_lossy(), diff --git a/docs/how-it-works/sandboxes/overview.mdx b/docs/how-it-works/sandboxes/overview.mdx index 776c006589..7ff36d95d7 100644 --- a/docs/how-it-works/sandboxes/overview.mdx +++ b/docs/how-it-works/sandboxes/overview.mdx @@ -902,12 +902,10 @@ key against this fingerprint on every SSH connection they open. When the SSH server presents a different key, they stop before authentication and report `SSH host key mismatch`. -They refuse a sandbox that has no published fingerprint. If the gateway -predates sandbox SSH identities, upgrade it. Otherwise stop and start the -sandbox so the gateway assigns it a key. To connect without verification, set -`OPENSHELL_SSH_ALLOW_UNVERIFIED_HOST_KEY=1`. OpenShell then prints a warning -on every such connection. For an editor, set the variable in the editor's -environment. +They cannot verify a sandbox that has no published fingerprint. They connect +and print a warning on every such connection. If the gateway predates sandbox +SSH identities, upgrade it. Otherwise stop and start the sandbox so the +gateway assigns it a key. Generate an SSH config entry for a sandbox so tools like VS Code Remote-SSH can connect directly: @@ -917,10 +915,10 @@ openshell sandbox ssh-config my-sandbox Append the output to `~/.ssh/config` or use `--editor` on `sandbox create`/`sandbox connect` for automatic setup. -OpenShell refuses connections through SSH config entries that an earlier -release generated, because those entries accept any host key. Replace each -pasted entry with new `openshell sandbox ssh-config` output, or use `--editor` -on `sandbox connect` again for that sandbox. +SSH config entries that an earlier release generated accept any host key. +They keep working and print a warning on every connection. Replace each pasted +entry with new `openshell sandbox ssh-config` output, or use `--editor` on +`sandbox connect` again for that sandbox. ## Transfer Files diff --git a/skills/openshell-cli/SKILL.md b/skills/openshell-cli/SKILL.md index b6c889cda7..40d5cfb23c 100644 --- a/skills/openshell-cli/SKILL.md +++ b/skills/openshell-cli/SKILL.md @@ -554,10 +554,10 @@ sandbox, even under the same name, creates a different identity. See the for verification behavior and release compatibility. The CLI, the TUI, and generated SSH config entries verify the host key against -that fingerprint on every SSH connection they open and fail closed. Each -refusal (`SSH host key mismatch`, `has no published SSH host key`, `predates -host-key verification`) prints its fix. The TUI status line shows only the -first line; run `openshell sandbox connect ` to read the rest. +that fingerprint on every SSH connection they open, and refuse a different key +with `SSH host key mismatch`. A `NOT verified` warning means the gateway +published no fingerprint, or the SSH config entry predates host-key +verification; the warning prints its fix. For an infrastructure `Error` on a backend that supports restarting stopped sandboxes, fix the reported cause, run `sandbox stop`, and wait for it to succeed before running `sandbox start`. This retains the sandbox and persistent workspace but cannot recover deleted resources or storage. For a provisioning timeout, wait for cleanup to finish, then run `sandbox start` directly. From ffc6aee5d540258aa20ad5aa17e7413384dc33cc Mon Sep 17 00:00:00 2001 From: Adrien Langou Date: Thu, 19 Nov 2026 12:38:11 +0100 Subject: [PATCH 4/6] feat(cli): let users refuse unverified SSH host keys OPENSHELL_SSH_REQUIRE_HOST_KEY turns the two unverified cases, a sandbox with no published fingerprint and a caller that predates host-key verification, from a warning into a refusal. It is off by default. Signed-off-by: Adrien Langou --- crates/openshell-cli/src/main.rs | 5 +- crates/openshell-cli/src/ssh.rs | 3 +- .../tests/ssh_proxy_host_key_integration.rs | 52 +++++++++++---- crates/openshell-core/src/ssh_host_key.rs | 63 +++++++++++++++++++ docs/how-it-works/sandboxes/overview.mdx | 4 ++ skills/openshell-cli/SKILL.md | 3 +- 6 files changed, 116 insertions(+), 14 deletions(-) diff --git a/crates/openshell-cli/src/main.rs b/crates/openshell-cli/src/main.rs index 8d8b4df287..4fc676cb16 100644 --- a/crates/openshell-cli/src/main.rs +++ b/crates/openshell-cli/src/main.rs @@ -21,7 +21,9 @@ use openshell_cli::completers; use openshell_cli::run; use openshell_cli::tls::TlsOptions; use openshell_core::proto::{GpuResourceRequirements, ServiceAuthorizationMode}; -use openshell_core::ssh_host_key::{HostKeyCheck, HostKeyExpectation, KnownHostsPin}; +use openshell_core::ssh_host_key::{ + HostKeyCheck, HostKeyExpectation, KnownHostsPin, permit_unverified, +}; /// Resolved gateway context: name + gateway endpoint. struct GatewayContext { @@ -4211,6 +4213,7 @@ async fn run_async() -> Result<()> { "openshell: warning: this SSH connection was started by an OpenShell client that predates host-key verification, so the SSH host key is NOT verified.\n\ Restart that client (the openshell command or openshell term)." ); + permit_unverified().map_err(|err| miette::miette!("{err}"))?; None } // Nothing would pin a key for the `ssh` that reads the file. diff --git a/crates/openshell-cli/src/ssh.rs b/crates/openshell-cli/src/ssh.rs index d0f368ae85..bde90b9c4a 100644 --- a/crates/openshell-cli/src/ssh.rs +++ b/crates/openshell-cli/src/ssh.rs @@ -24,7 +24,7 @@ use openshell_core::proto::{ }; use openshell_core::ssh_host_key::{ HostKeyCheck, HostKeyExpectation, HostKeyGate, KnownHostsPin, SandboxSshTarget, - host_key_alias as host_alias, + host_key_alias as host_alias, permit_unverified, }; use openshell_core::upload::{ DestinationKind as UploadDestinationKind, ExpectedDestination, UploadPlan, @@ -2202,6 +2202,7 @@ pub async fn sandbox_ssh_proxy_by_name( "{}", stale_ssh_config_warning(gateway_name, workspace, name) ); + permit_unverified().into_diagnostic()?; None }; let server = grpc_server_from_ssh_gateway_url(&session.gateway_url)?; diff --git a/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs b/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs index 68c88a38db..84dada9e9b 100644 --- a/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs +++ b/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs @@ -9,6 +9,7 @@ use hyper_util::rt::{TokioExecutor, TokioIo}; use openshell_core::proto::{ CreateSshSessionResponse, Sandbox, SandboxResponse, TcpForwardFrame, tcp_forward_frame, }; +use openshell_core::ssh_host_key::REQUIRE_HOST_KEY_ENV; use prost::Message; use russh::keys::ssh_key::private::Ed25519Keypair; use russh::keys::{HashAlg, PrivateKey}; @@ -190,8 +191,18 @@ fn old_name_mode(gateway: SocketAddr) -> String { format!("ssh-proxy --server http://{gateway} --name {SANDBOX} --workspace {WORKSPACE}") } -/// Runs `command` to completion. -async fn finished(mut command: tokio::process::Command, config: &Path) -> Output { +/// Runs `command` to completion. `require_host_key` is whether the user set +/// `OPENSHELL_SSH_REQUIRE_HOST_KEY`. +async fn finished( + mut command: tokio::process::Command, + config: &Path, + require_host_key: bool, +) -> Output { + if require_host_key { + command.env(REQUIRE_HOST_KEY_ENV, "1"); + } else { + command.env_remove(REQUIRE_HOST_KEY_ENV); + } let run = command .env("XDG_CONFIG_HOME", config) .env("OPENSHELL_TELEMETRY_ENABLED", "false") @@ -208,10 +219,10 @@ async fn finished(mut command: tokio::process::Command, config: &Path) -> Output } /// Runs `openshell` with `args`, which are separated by single spaces. -async fn openshell(args: &str, config: &Path) -> Output { +async fn openshell(args: &str, config: &Path, require_host_key: bool) -> Output { let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell")); command.args(args.split(' ')); - finished(command, config).await + finished(command, config, require_host_key).await } fn mismatch_message(published: &PrivateKey, presented: &PrivateKey) -> String { @@ -272,7 +283,7 @@ async fn name_mode_proxy_withholds_a_key_the_gateway_did_not_publish() { let scratch = tempfile::tempdir().unwrap(); let pin = scratch.path().join("known_hosts/pin"); - let proxy = openshell(&name_mode(gateway, &pin), scratch.path()).await; + let proxy = openshell(&name_mode(gateway, &pin), scratch.path(), false).await; let stderr = String::from_utf8_lossy(&proxy.stderr); assert_eq!(proxy.status.code(), Some(1), "{stderr}"); @@ -301,7 +312,7 @@ async fn name_mode_proxy_warns_and_relays_when_the_gateway_published_no_key() { let scratch = tempfile::tempdir().unwrap(); let pin = scratch.path().join("known_hosts/pin"); - let proxy = openshell(&name_mode(gateway, &pin), scratch.path()).await; + let proxy = openshell(&name_mode(gateway, &pin), scratch.path(), false).await; let stderr = String::from_utf8_lossy(&proxy.stderr); assert!(proxy.status.success(), "{stderr}"); @@ -314,6 +325,16 @@ async fn name_mode_proxy_warns_and_relays_when_the_gateway_published_no_key() { std::fs::read_to_string(&pin).unwrap(), pin_content(Some(&key)) ); + + let refused = openshell(&name_mode(gateway, &pin), scratch.path(), true).await; + + let stderr = String::from_utf8_lossy(&refused.stderr); + assert!(!refused.status.success(), "{stderr}"); + assert_eq!(refused.stdout, b""); + // The error is wrapped to the terminal width. + assert!(stderr.contains("fingerprint"), "{stderr}"); + assert!(stderr.contains(REQUIRE_HOST_KEY_ENV), "{stderr}"); + assert_eq!(std::fs::read_to_string(&pin).unwrap(), pin_content(None)); } /// An `ssh` started by a client or a config entry from before host-key @@ -339,7 +360,7 @@ async fn proxy_warns_and_relays_for_a_caller_that_predates_host_key_verification "SSH config entry for sandbox 'my-sb'", ), ] { - let proxy = openshell(&args, scratch.path()).await; + let proxy = openshell(&args, scratch.path(), false).await; let stderr = String::from_utf8_lossy(&proxy.stderr); assert!(proxy.status.success(), "{stderr}"); @@ -349,6 +370,14 @@ async fn proxy_warns_and_relays_for_a_caller_that_predates_host_key_verification stderr.contains("the SSH host key is NOT verified"), "{stderr}" ); + + let refused = openshell(&args, scratch.path(), true).await; + + let stderr = String::from_utf8_lossy(&refused.stderr); + assert!(!refused.status.success(), "{stderr}"); + assert_eq!(refused.stdout, b""); + assert!(stderr.contains(caller), "{stderr}"); + assert!(stderr.contains(REQUIRE_HOST_KEY_ENV), "{stderr}"); } } @@ -457,11 +486,12 @@ mod openssh { .args(overrides.iter().flat_map(|option| ["-o", option])) .args(sandbox_ssh_args(&proxy_command, "ERROR")) .args(["sandbox", COMMAND]); - finished(ssh, config).await + finished(ssh, config, false).await } /// Runs [`COMMAND`] through the config entry `sandbox ssh-config` - /// prints, with no other configuration and no options of its own. + /// prints, with no other configuration and no options of its own, for + /// a user who requires a verified host key. async fn ssh_through_config_entry(gateway: SocketAddr, config: &Path) -> Output { let gateway_dir = config.join("openshell/gateways").join(GATEWAY); std::fs::create_dir_all(&gateway_dir).unwrap(); @@ -476,7 +506,7 @@ mod openssh { let print_entry = format!("--gateway {GATEWAY} --workspace {WORKSPACE} sandbox ssh-config {SANDBOX}"); - let printed = openshell(&print_entry, config).await; + let printed = openshell(&print_entry, config, false).await; assert!( printed.status.success(), "{}", @@ -490,7 +520,7 @@ mod openssh { .arg(entry) .args(["-o", "BatchMode=yes"]) .args([format!("openshell-{SANDBOX}.{WORKSPACE}").as_str(), COMMAND]); - finished(ssh, config).await + finished(ssh, config, true).await } /// The proxy accepts and forwards any key here, so the refusal is `ssh`'s diff --git a/crates/openshell-core/src/ssh_host_key.rs b/crates/openshell-core/src/ssh_host_key.rs index be9efc9624..ed6df95afa 100644 --- a/crates/openshell-core/src/ssh_host_key.rs +++ b/crates/openshell-core/src/ssh_host_key.rs @@ -9,6 +9,7 @@ //! fingerprint format, the pin location and file format, the `ssh` options, //! the `ssh-proxy` flags, and the scan of the server's key exchange reply. +use std::ffi::OsStr; use std::fmt; use std::fs; use std::io::{self, Write as _}; @@ -21,7 +22,11 @@ use base64::engine::general_purpose::{STANDARD, STANDARD_NO_PAD}; use crate::forward::shell_escape; use crate::proto::CreateSshSessionResponse; +use crate::settings::parse_bool_like; +/// Set to refuse every SSH connection whose host key is not verified. Unset, +/// such a connection goes ahead with a warning. +pub const REQUIRE_HOST_KEY_ENV: &str = "OPENSHELL_SSH_REQUIRE_HOST_KEY"; const FINGERPRINT_PREFIX: &str = "SHA256:"; const UNVERIFIED: &str = "unverified"; const HOST_KEY_ALGORITHM: &str = "ssh-ed25519"; @@ -122,6 +127,28 @@ impl HostKeyExpectation { } } +/// Whether the user set [`REQUIRE_HOST_KEY_ENV`]. Only a value that reads as +/// "no" turns it back off. +fn host_key_required() -> bool { + requires_host_key(std::env::var_os(REQUIRE_HOST_KEY_ENV).as_deref()) +} + +fn requires_host_key(value: Option<&OsStr>) -> bool { + value.is_some_and(|value| { + !value.is_empty() && value.to_str().and_then(parse_bool_like) != Some(false) + }) +} + +/// For a caller that just warned that nothing verifies the host key of its +/// connection. +pub fn permit_unverified() -> Result<(), HostKeyError> { + if host_key_required() { + Err(HostKeyError::Required) + } else { + Ok(()) + } +} + #[derive(Debug, thiserror::Error)] pub enum HostKeyError { #[error("SSH host key fingerprint is not 'SHA256:' followed by 43 unpadded base64 characters")] @@ -142,6 +169,17 @@ pub enum HostKeyError { expected: HostKeyFingerprint, presented: HostKeyFingerprint, }, + #[error( + "SSH connection to sandbox '{sandbox}' (workspace '{workspace}') refused: {} is set and the gateway published no host key fingerprint.\n\ + Upgrade a gateway that predates sandbox SSH identities; otherwise stop and start the sandbox.", + REQUIRE_HOST_KEY_ENV + )] + Unpublished { sandbox: String, workspace: String }, + #[error( + "the SSH connection is refused because {} is set and its host key is not verified", + REQUIRE_HOST_KEY_ENV + )] + Required, #[error("malformed SSH stream from the sandbox: {0}")] Malformed(&'static str), #[error( @@ -382,6 +420,12 @@ impl HostKeyCheck { HostKeyExpectation::Fingerprint(fingerprint) => Some(fingerprint), HostKeyExpectation::AcceptAny => None, })?; + if matches!(expectation, HostKeyExpectation::AcceptAny) && host_key_required() { + return Err(HostKeyError::Unpublished { + sandbox: pin.sandbox, + workspace: pin.workspace, + }); + } Ok(Self { expectation, pin }) } @@ -927,6 +971,25 @@ mod tests { } } + #[test] + fn host_key_is_required_by_any_value_but_a_no() { + for (value, required) in [ + (None, false), + (Some(""), false), + (Some("0"), false), + (Some("False"), false), + (Some("1"), true), + (Some("true"), true), + (Some("strict"), true), + ] { + assert_eq!( + requires_host_key(value.map(OsStr::new)), + required, + "{value:?}" + ); + } + } + /// Before `ssh` can read the pin, it holds the published key or no key. #[test] fn check_for_session_keeps_only_the_published_key() { diff --git a/docs/how-it-works/sandboxes/overview.mdx b/docs/how-it-works/sandboxes/overview.mdx index 2a46a47683..48e2b26b0b 100644 --- a/docs/how-it-works/sandboxes/overview.mdx +++ b/docs/how-it-works/sandboxes/overview.mdx @@ -920,6 +920,10 @@ They keep working and print a warning on every connection. Replace each pasted entry with new `openshell sandbox ssh-config` output, or use `--editor` on `sandbox connect` again for that sandbox. +Set `OPENSHELL_SSH_REQUIRE_HOST_KEY=1` to refuse every unverified connection, +to a sandbox with no published fingerprint or through such an entry, instead +of connecting with a warning. + ## Transfer Files Upload files from your host into the sandbox: diff --git a/skills/openshell-cli/SKILL.md b/skills/openshell-cli/SKILL.md index 718b7c10ce..2df51324b1 100644 --- a/skills/openshell-cli/SKILL.md +++ b/skills/openshell-cli/SKILL.md @@ -557,7 +557,8 @@ The CLI, the TUI, and generated SSH config entries verify the host key against that fingerprint on every SSH connection they open, and refuse a different key with `SSH host key mismatch`. A `NOT verified` warning means the gateway published no fingerprint, or the SSH config entry predates host-key -verification; the warning prints its fix. +verification; the warning prints its fix. Set +`OPENSHELL_SSH_REQUIRE_HOST_KEY=1` to refuse such connections instead. For an infrastructure `Error` on a backend that supports restarting stopped sandboxes, fix the reported cause, run `sandbox stop`, and wait for it to succeed before running `sandbox start`. This retains the sandbox and persistent workspace but cannot recover deleted resources or storage. For a provisioning timeout, wait for cleanup to finish, then run `sandbox start` directly. From 6290ecba44cc509107b952890ed9284e51f14954 Mon Sep 17 00:00:00 2001 From: Adrien Langou Date: Fri, 20 Nov 2026 13:23:02 +0100 Subject: [PATCH 5/6] fix(cli): keep SSH connection sharing in generated config entries A generated entry has one host alias per sandbox, so a shared connection never reaches another sandbox. Stop disabling ControlMaster there. Also name the missing flags in the ssh-proxy warning, note that the gateway issues only Ed25519 host keys, and document that the fingerprint is read per connection and that unverified connections are allowed by default. Signed-off-by: Adrien Langou --- crates/openshell-cli/src/main.rs | 7 ++++--- crates/openshell-cli/src/ssh.rs | 15 +++------------ .../tests/ssh_proxy_host_key_integration.rs | 2 +- crates/openshell-core/src/ssh_host_key.rs | 2 ++ docs/how-it-works/sandboxes/overview.mdx | 14 +++++++++----- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/crates/openshell-cli/src/main.rs b/crates/openshell-cli/src/main.rs index 4fc676cb16..2e4ba4fe29 100644 --- a/crates/openshell-cli/src/main.rs +++ b/crates/openshell-cli/src/main.rs @@ -4207,11 +4207,12 @@ async fn run_async() -> Result<()> { .and_then(|pin| HostKeyCheck::for_relay(expectation, pin)) .map_err(|err| miette::miette!("{err}"))?, ), - // The `ssh` of such a client accepts any host key. + // An older client starts the proxy this way, and its `ssh` + // accepts any host key. (None, None) => { eprintln!( - "openshell: warning: this SSH connection was started by an OpenShell client that predates host-key verification, so the SSH host key is NOT verified.\n\ - Restart that client (the openshell command or openshell term)." + "openshell: warning: ssh-proxy was started without --host-key and --known-hosts, so the SSH host key is NOT verified.\n\ + If an OpenShell client that predates host-key verification started it, restart that client (the openshell command or openshell term)." ); permit_unverified().map_err(|err| miette::miette!("{err}"))?; None diff --git a/crates/openshell-cli/src/ssh.rs b/crates/openshell-cli/src/ssh.rs index bde90b9c4a..a5ea27d8d6 100644 --- a/crates/openshell-cli/src/ssh.rs +++ b/crates/openshell-cli/src/ssh.rs @@ -2256,7 +2256,7 @@ fn render_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result>() .join("\n"); Ok(format!( - "Host {host_alias}\n User sandbox\n{options}\n ControlMaster no\n ControlPath none\n ControlPersist no\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {proxy_cmd}\n" + "Host {host_alias}\n User sandbox\n{options}\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {proxy_cmd}\n" )) } @@ -2930,7 +2930,7 @@ mod tests { } /// `ssh` resolves the rendered block to the intended options, also when - /// the pin path needs quoting and a later block enables connection sharing. + /// the pin path needs quoting. #[cfg(unix)] #[test] fn rendered_ssh_config_makes_ssh_trust_only_the_pin() { @@ -2942,15 +2942,7 @@ mod tests { .to_string(); let config = config_home.join("rendered_ssh_config"); fs::create_dir_all(config_home).unwrap(); - let user_sharing = format!( - "Host *\n ControlMaster auto\n ControlPath \"{}/mux-%C\"\n ControlPersist yes\n", - config_home.display() - ); - fs::write( - &config, - render_ssh_config("my-gw", "demo", "beta").unwrap() + &user_sharing, - ) - .unwrap(); + fs::write(&config, render_ssh_config("my-gw", "demo", "beta").unwrap()).unwrap(); let output = Command::new("ssh") .arg("-F") @@ -2965,7 +2957,6 @@ mod tests { Path::new(&known_hosts), "openshell-demo.beta", ); - assert_connection_sharing_disabled(&effective); let proxy_command = effective .lines() .find_map(|line| line.strip_prefix("proxycommand ")) diff --git a/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs b/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs index 84dada9e9b..7ac796fbf1 100644 --- a/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs +++ b/crates/openshell-cli/tests/ssh_proxy_host_key_integration.rs @@ -354,7 +354,7 @@ async fn proxy_warns_and_relays_for_a_caller_that_predates_host_key_verification ); for (args, caller) in [ - (old_client, "started by an OpenShell client that predates"), + (old_client, "started without --host-key and --known-hosts"), ( old_name_mode(gateway), "SSH config entry for sandbox 'my-sb'", diff --git a/crates/openshell-core/src/ssh_host_key.rs b/crates/openshell-core/src/ssh_host_key.rs index ed6df95afa..4aa923a368 100644 --- a/crates/openshell-core/src/ssh_host_key.rs +++ b/crates/openshell-core/src/ssh_host_key.rs @@ -29,6 +29,8 @@ use crate::settings::parse_bool_like; pub const REQUIRE_HOST_KEY_ENV: &str = "OPENSHELL_SSH_REQUIRE_HOST_KEY"; const FINGERPRINT_PREFIX: &str = "SHA256:"; const UNVERIFIED: &str = "unverified"; +/// The only host key type a gateway issues. A second type needs its name +/// here, in `HostKeyAlgorithms`, and its own blob check in the gate. const HOST_KEY_ALGORITHM: &str = "ssh-ed25519"; const PIN_HEADER: &str = "# Managed by OpenShell. Rewritten from the gateway-published fingerprint; do not edit.\n"; diff --git a/docs/how-it-works/sandboxes/overview.mdx b/docs/how-it-works/sandboxes/overview.mdx index 48e2b26b0b..3bfb607ab3 100644 --- a/docs/how-it-works/sandboxes/overview.mdx +++ b/docs/how-it-works/sandboxes/overview.mdx @@ -898,8 +898,10 @@ runtimes can return an empty fingerprint; an empty value cannot verify an SSH server's identity. The CLI, the TUI, and generated SSH config entries check the sandbox's host -key against this fingerprint on every SSH connection they open. When the SSH -server presents a different key, they stop before authentication and report +key against this fingerprint on every SSH connection they open. They read the +fingerprint from the gateway for each connection, so a sandbox recreated under +the same name is checked against its new key. When the SSH server presents a +different key, they stop before authentication and report `SSH host key mismatch`. They cannot verify a sandbox that has no published fingerprint. They connect @@ -920,9 +922,11 @@ They keep working and print a warning on every connection. Replace each pasted entry with new `openshell sandbox ssh-config` output, or use `--editor` on `sandbox connect` again for that sandbox. -Set `OPENSHELL_SSH_REQUIRE_HOST_KEY=1` to refuse every unverified connection, -to a sandbox with no published fingerprint or through such an entry, instead -of connecting with a warning. +Unverified connections go ahead by default. Set +`OPENSHELL_SSH_REQUIRE_HOST_KEY=1` to refuse every unverified connection, to a +sandbox with no published fingerprint or through such an entry, instead of +connecting with a warning. Any value other than `0`, `false`, `no`, `n`, or +`off` refuses them. ## Transfer Files From a7398f684461e20881ac338161600feaf3131cf3 Mon Sep 17 00:00:00 2001 From: Adrien Langou Date: Fri, 20 Nov 2026 13:32:57 +0100 Subject: [PATCH 6/6] fix(cli): disable SSH connection sharing in generated config entries The host alias of an entry names the sandbox and the workspace, not the gateway. With a user's persistent ControlMaster, ssh reused a connection opened to one gateway's sandbox for the entry of the same sandbox name on another gateway, and ran no host key check. Restore the three options that turn sharing off for the entry. Signed-off-by: Adrien Langou --- crates/openshell-cli/src/ssh.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/crates/openshell-cli/src/ssh.rs b/crates/openshell-cli/src/ssh.rs index a5ea27d8d6..bff16f848f 100644 --- a/crates/openshell-cli/src/ssh.rs +++ b/crates/openshell-cli/src/ssh.rs @@ -2255,8 +2255,10 @@ fn render_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result>() .join("\n"); + // The alias does not name the gateway. A connection the user shares would + // also serve the sandbox of the same name on another gateway, unchecked. Ok(format!( - "Host {host_alias}\n User sandbox\n{options}\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {proxy_cmd}\n" + "Host {host_alias}\n User sandbox\n{options}\n ControlMaster no\n ControlPath none\n ControlPersist no\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {proxy_cmd}\n" )) } @@ -2930,7 +2932,7 @@ mod tests { } /// `ssh` resolves the rendered block to the intended options, also when - /// the pin path needs quoting. + /// the pin path needs quoting and a later block enables connection sharing. #[cfg(unix)] #[test] fn rendered_ssh_config_makes_ssh_trust_only_the_pin() { @@ -2942,7 +2944,15 @@ mod tests { .to_string(); let config = config_home.join("rendered_ssh_config"); fs::create_dir_all(config_home).unwrap(); - fs::write(&config, render_ssh_config("my-gw", "demo", "beta").unwrap()).unwrap(); + let user_sharing = format!( + "Host *\n ControlMaster auto\n ControlPath \"{}/mux-%C\"\n ControlPersist yes\n", + config_home.display() + ); + fs::write( + &config, + render_ssh_config("my-gw", "demo", "beta").unwrap() + &user_sharing, + ) + .unwrap(); let output = Command::new("ssh") .arg("-F") @@ -2957,6 +2967,7 @@ mod tests { Path::new(&known_hosts), "openshell-demo.beta", ); + assert_connection_sharing_disabled(&effective); let proxy_command = effective .lines() .find_map(|line| line.strip_prefix("proxycommand "))