Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
remote: Accept shell-quoted ssh executable in SSH command lines (#66360)
Yossi Eliaz6569db7470f8
diff --git a/crates/remote/src/transport/ssh.rs b/crates/remote/src/transport/ssh.rsindex 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<SshPortForwardOption> {impl SshConnectionOptions {pub fn parse_command_line(input: &str) -> Result<Self> {- let input = input.trim_start_matches("ssh ");let mut hostname: Option<String> = None;let mut username: Option<String> = None;let mut port: Option<u16> = 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 [email protected]")?;+ 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' '[email protected]'",+ )?;+ 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\" [email protected]")?;+ 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")?;