Skip to content

Commit 9996a59

Browse files
authored
feat(cli)!: verify sandbox SSH host keys (#5703)
* 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 <alangou@nvidia.com> * 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 <alangou@nvidia.com> * 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 <alangou@nvidia.com> * 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 <alangou@nvidia.com> * fix(cli): name the missing flags in the unverified SSH warning The ssh-proxy warning blamed an older client, which is wrong when someone starts the proxy by hand. It now names the missing flags. Also say why a generated config entry turns connection sharing off: its host alias does not name the gateway, so a shared connection would serve the sandbox of the same name on another gateway without a host key check. 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 <alangou@nvidia.com> * fix(cli): replace SSH config entries that accept any host key Using --editor rewrote only the entry of the sandbox being connected, so the entries an earlier release wrote for other sandboxes kept accepting any host key. It now replaces each of them that is still, line for line, what that release wrote. An entry the user edited is left alone and keeps its warning. The TUI also puts "SSH host key NOT verified" first in its status line after an unverified session, because ssh takes the terminal right after the printed warning. Signed-off-by: Adrien Langou <alangou@nvidia.com> * refactor(cli): return the SSH host key refusal from the relay The relay exited the process from inside its task. It now returns the refusal, and the CLI prints it like any other error. The exit code stays 1 and no byte of the refused key exchange reply reaches ssh. Signed-off-by: Adrien Langou <alangou@nvidia.com> * feat(cli)!: refuse unverified SSH host keys by default An SSH connection whose host key cannot be verified is now refused: a sandbox with no published fingerprint, an SSH config entry an earlier release wrote, or an older client that starts ssh-proxy without --host-key. Set OPENSHELL_SSH_REQUIRE_HOST_KEY=0 to connect with a warning instead. A stale config entry is refused before the gateway is called, and uploads and downloads no longer retry a host key refusal. Signed-off-by: Adrien Langou <alangou@nvidia.com> --------- Signed-off-by: Adrien Langou <alangou@nvidia.com>
1 parent 845614a commit 9996a59

17 files changed

Lines changed: 2739 additions & 107 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-cli/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ openshell-supervisor-middleware-builtins = { path = "../openshell-supervisor-mid
2727
serde = { workspace = true }
2828
serde_json = { workspace = true }
2929
serde_yml = { workspace = true }
30+
shell-words = { workspace = true }
3031
prost = { workspace = true }
3132
prost-types = { workspace = true }
3233

@@ -95,6 +96,7 @@ futures = { workspace = true }
9596
openshell-server = { path = "../openshell-server", features = ["test-support"] }
9697
rcgen = { workspace = true }
9798
reqwest = { workspace = true }
99+
russh = "0.65.0"
98100
serde_json = { workspace = true }
99101
temp-env = "0.3"
100102
tokio-stream = { workspace = true }

‎crates/openshell-cli/src/main.rs‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,12 @@ enum Commands {
716716
/// Sandbox to connect to.
717717
#[arg(long, alias = "sandbox")]
718718
name: String,
719+
720+
/// Known-hosts file of a saved SSH profile, in which the verified host
721+
/// key is pinned for `ssh`. Without it the host key is not verified,
722+
/// and the connection is refused by default.
723+
#[arg(long, conflicts_with = "handoff")]
724+
known_hosts: Option<PathBuf>,
719725
},
720726
}
721727

@@ -4409,6 +4415,7 @@ async fn run_async() -> Result<()> {
44094415
gateway_name,
44104416
name,
44114417
handoff,
4418+
known_hosts,
44124419
}) => {
44134420
if let Some(handle) = handoff {
44144421
let (grant, peer) = openshell_core::ssh_handoff::receive(&handle).await?;
@@ -4428,7 +4435,15 @@ async fn run_async() -> Result<()> {
44284435
let ctx = resolve_gateway(&profile, &endpoint, allow_endpoint_override)?;
44294436
let mut tls = tls.with_gateway_name(&ctx.name);
44304437
apply_auth(&mut tls, &ctx.name)?;
4431-
run::sandbox_ssh_proxy_by_name(&ctx.endpoint, &name, &tls, &cli.workspace).await?;
4438+
run::sandbox_ssh_proxy_by_name(
4439+
&ctx.endpoint,
4440+
&ctx.name,
4441+
&name,
4442+
&tls,
4443+
&cli.workspace,
4444+
known_hosts,
4445+
)
4446+
.await?;
44324447
}
44334448
}
44344449

@@ -5467,17 +5482,23 @@ mod tests {
54675482
"my-box",
54685483
"--workspace",
54695484
"team-a",
5485+
"--known-hosts",
5486+
"/home/u/.config/openshell/known_hosts/my-gateway/openshell-my-box.team-a",
54705487
])
54715488
.unwrap();
54725489
assert_eq!(cli.workspace, "team-a");
54735490
let Some(Commands::SshProxy {
5474-
gateway_name, name, ..
5491+
gateway_name,
5492+
name,
5493+
known_hosts,
5494+
..
54755495
}) = cli.command
54765496
else {
54775497
panic!("expected ssh-proxy")
54785498
};
54795499
assert_eq!(gateway_name.as_deref(), Some("my-gateway"));
54805500
assert_eq!(name, "my-box");
5501+
assert!(known_hosts.is_some());
54815502
}
54825503
}
54835504

0 commit comments

Comments
 (0)