mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
remote: Accept shell-quoted ssh executable in SSH command lines (#66360)
## Why Remote Projects accepts either a bare destination or a full `ssh ...` command. Commands that tools print with every token shell-quoted (for example `'ssh' '-p' '64042' 'user@127.0.0.1'`) work in a shell but Zed rejects them with `unsupported argument`. The goal is that any valid SSH command pasted into **Connect New Server** parses the same way whether or not its tokens are quoted. Closes #61101 ## Summary `SshConnectionOptions::parse_command_line` stripped a literal, unquoted `ssh ` prefix from the raw string before tokenizing. With a quoted `'ssh'`, the prefix didn't match, so after tokenizing `ssh` became the hostname and the real destination was rejected as an extra argument. This change tokenizes first and then drops a leading `ssh` token when more tokens follow. A lone `ssh` is still treated as a hostname, as before. ## Testing - Added `test_parse_command_line_with_ssh_executable` covering unquoted, single-quoted (including an identity path with spaces), and double-quoted `ssh` executables, plus `ssh` used as a host. It fails on `main` with `unsupported argument: "user@127.0.0.1"` and passes with this change. - `cargo test -p remote`: 56 passed - `cargo fmt -p remote -- --check` - `./script/clippy -p remote` ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). (No UI changes.) - [x] Tests cover the new or changed behavior. AI disclosure: this change was AI-assisted. I reviewed the diff and the test. Release Notes: - Fixed Remote Projects rejecting SSH commands whose `ssh` executable is shell-quoted.
This commit is contained in:
@@ -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 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")?;
|
||||
|
||||
Reference in New Issue
Block a user