diff --git a/crates/remote/src/transport/ssh.rs b/crates/remote/src/transport/ssh.rs index 774fbb7137b..efed4c99032 100644 --- a/crates/remote/src/transport/ssh.rs +++ b/crates/remote/src/transport/ssh.rs @@ -1862,7 +1862,6 @@ fn parse_port_forward_spec(spec: &str) -> Result { impl SshConnectionOptions { pub fn parse_command_line(input: &str) -> Result { - let input = input.trim_start_matches("ssh "); let mut hostname: Option = None; let mut username: Option = None; let mut port: Option = None; @@ -1878,10 +1877,14 @@ impl SshConnectionOptions { "-w", ]; - let mut tokens = ShellKind::Posix - .split(input) - .context("invalid input")? - .into_iter(); + let mut tokens = ShellKind::Posix.split(input).context("invalid input")?; + // The input may be a full command such as `ssh -p 2222 host`, possibly with + // shell-quoted tokens. Strip the executable after tokenizing so that `'ssh'` + // is recognized too, while a lone `ssh` is still treated as a hostname. + if tokens.len() > 1 && tokens[0] == "ssh" { + tokens.remove(0); + } + let mut tokens = tokens.into_iter(); 'outer: while let Some(arg) = tokens.next() { if ALLOWED_OPTS.contains(&(&arg as &str)) { @@ -3053,6 +3056,45 @@ mod tests { Ok(()) } + #[test] + fn test_parse_command_line_with_ssh_executable() -> Result<()> { + let opts = SshConnectionOptions::parse_command_line("ssh -p 2222 user@example.com")?; + assert_eq!(opts.host, "example.com".into()); + assert_eq!(opts.username, Some("user".to_string())); + assert_eq!(opts.port, Some(2222)); + + let opts = SshConnectionOptions::parse_command_line( + "'ssh' '-i' '/Users/test/Library/Application Support/key' '-p' '64042' '-o' 'ControlMaster=auto' 'user@127.0.0.1'", + )?; + assert_eq!(opts.host, "127.0.0.1".into()); + assert_eq!(opts.username, Some("user".to_string())); + assert_eq!(opts.port, Some(64042)); + assert_eq!( + opts.args, + Some(vec![ + "-i".to_string(), + "/Users/test/Library/Application Support/key".to_string(), + "-o".to_string(), + "ControlMaster=auto".to_string(), + ]) + ); + + let opts = SshConnectionOptions::parse_command_line("\"ssh\" user@example.com")?; + assert_eq!(opts.host, "example.com".into()); + assert_eq!(opts.username, Some("user".to_string())); + + // A bare `ssh` is a hostname, not the executable. + let opts = SshConnectionOptions::parse_command_line("ssh")?; + assert_eq!(opts.host, "ssh".into()); + assert_eq!(opts.username, None); + + let opts = SshConnectionOptions::parse_command_line("ssh user@ssh")?; + assert_eq!(opts.host, "ssh".into()); + assert_eq!(opts.username, Some("user".to_string())); + + Ok(()) + } + #[test] fn test_parse_port_forward_spec_ipv6() -> Result<()> { let pf = parse_port_forward_spec("[::1]:8080:[::1]:80")?;