Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

remote: Stop URL-encoding SSH usernames (#64744)

Ryan Brue42c4caa474da
diff --git a/Cargo.lock b/Cargo.lock
index 5b2eeb56d4e..260d78e8bc8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -15111,7 +15111,6 @@ dependencies = [
"telemetry",
"tempfile",
"thiserror 2.0.17",
- "urlencoding",
"util",
"which",
]
diff --git a/crates/remote/Cargo.toml b/crates/remote/Cargo.toml
index 3f78b802041..54b95140ee8 100644
--- a/crates/remote/Cargo.toml
+++ b/crates/remote/Cargo.toml
@@ -41,7 +41,6 @@ smol.workspace = true
telemetry.workspace = true
tempfile.workspace = true
thiserror.workspace = true
-urlencoding.workspace = true
util.workspace = true
which.workspace = true
diff --git a/crates/remote/src/transport/ssh.rs b/crates/remote/src/transport/ssh.rs
index ed0987a11e7..e2b9579b941 100644
--- a/crates/remote/src/transport/ssh.rs
+++ b/crates/remote/src/transport/ssh.rs
@@ -305,7 +305,7 @@ impl MasterProcess {
askpass_script_path: &std::ffi::OsStr,
additional_args: Vec<String>,
socket_path: &std::path::Path,
- destination: &str,
+ destination_args: Vec<String>,
) -> Result<Self> {
let args = [
"-N",
@@ -329,7 +329,7 @@ impl MasterProcess {
master_process.arg(format!("ControlPath={}", socket_path.display()));
- let process = master_process.arg(&destination).spawn()?;
+ let process = master_process.args(destination_args).spawn()?;
Ok(MasterProcess { process })
}
@@ -353,7 +353,7 @@ impl MasterProcess {
askpass_script_path: &std::ffi::OsStr,
askpass_socket_path: &std::ffi::OsStr,
additional_args: Vec<String>,
- destination: &str,
+ destination_args: Vec<String>,
) -> Result<Self> {
// On Windows, `ControlMaster` and `ControlPath` are not supported:
// https://github.com/PowerShell/Win32-OpenSSH/issues/405
@@ -361,10 +361,7 @@ impl MasterProcess {
//
// Using an ugly workaround to detect connection establishment
// -N doesn't work with JumpHosts as windows openssh never closes stdin in that case
- let args = [
- "-t",
- &format!("echo '{}'; exec $0", Self::CONNECTION_ESTABLISHED_MAGIC),
- ];
+ let remote_command = format!("echo '{}'; exec $0", Self::CONNECTION_ESTABLISHED_MAGIC);
let mut master_process = util::command::new_command("ssh");
master_process
@@ -376,8 +373,9 @@ impl MasterProcess {
.env("SSH_ASKPASS", askpass_script_path)
.env("ZED_ASKPASS_SOCKET", askpass_socket_path)
.args(additional_args)
- .arg(destination)
- .args(args);
+ .arg("-t")
+ .args(destination_args)
+ .arg(remote_command);
let process = master_process.spawn()?;
@@ -479,7 +477,7 @@ impl RemoteConnection for SshRemoteConnection {
ssh_shell,
*ssh_shell_kind,
socket.ssh_command_options(),
- &socket.connection_options.ssh_destination(),
+ socket.connection_options.ssh_destination_args(),
interactive,
)
} else {
@@ -494,7 +492,7 @@ impl RemoteConnection for SshRemoteConnection {
ssh_shell,
*ssh_shell_kind,
socket.ssh_command_options(),
- &socket.connection_options.ssh_destination(),
+ socket.connection_options.ssh_destination_args(),
interactive,
)
}
@@ -534,7 +532,7 @@ impl RemoteConnection for SshRemoteConnection {
self.socket.ssh_command_options(),
None,
Interactive::No,
- &self.socket.connection_options.ssh_destination(),
+ self.socket.connection_options.ssh_destination_args(),
exec,
self.socket.envs.clone(),
);
@@ -557,7 +555,7 @@ impl RemoteConnection for SshRemoteConnection {
remote_port
));
}
- args.push(socket.connection_options.ssh_destination());
+ args.extend(socket.connection_options.ssh_destination_args());
Ok(CommandTemplate {
program: "ssh".into(),
args,
@@ -721,15 +719,16 @@ impl RemoteConnection for SshRemoteConnection {
/// given destination. See: https://github.com/zed-industries/zed/issues/45271
#[cfg(not(windows))]
async fn find_existing_control_master(
- destination: &str,
- additional_args: &[String],
+ connection_options: &SshConnectionOptions,
) -> Option<PathBuf> {
+ let additional_args = connection_options.additional_args();
+ let destination_args = connection_options.ssh_destination_args();
// Use `ssh -G` to resolve the user's effective SSH config for this host.
// This expands ControlPath tokens (%h, %p, %r, %C, etc.) into actual paths.
let output = match util::command::new_command("ssh")
- .args(additional_args)
+ .args(&additional_args)
.arg("-G")
- .arg(destination)
+ .args(&destination_args)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null())
@@ -744,7 +743,10 @@ async fn find_existing_control_master(
};
if !output.status.success() {
- log::debug!("ssh -G failed for {destination}, skipping ControlMaster reuse");
+ log::debug!(
+ "ssh -G failed for {}, skipping ControlMaster reuse",
+ connection_options.connection_string()
+ );
return None;
}
@@ -764,7 +766,7 @@ async fn find_existing_control_master(
.args(["-O", "check"])
.arg("-o")
.arg(format!("ControlPath={}", control_path.display()))
- .arg(destination)
+ .args(&destination_args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
@@ -802,8 +804,6 @@ impl SshRemoteConnection {
) -> Result<Self> {
use askpass::AskPassResult;
- let destination = connection_options.ssh_destination();
-
let temp_dir = tempfile::Builder::new()
.prefix("zed-ssh-session")
.tempdir()?;
@@ -811,8 +811,7 @@ impl SshRemoteConnection {
// On non-Windows, check if the user already has an active ControlMaster
// session for this host. If so, reuse it instead of prompting for auth.
#[cfg(not(windows))]
- let reused_socket =
- find_existing_control_master(&destination, &connection_options.additional_args()).await;
+ let reused_socket = find_existing_control_master(&connection_options).await;
#[cfg(not(windows))]
let (socket, master_process_option) = if let Some(reused_path) = reused_socket {
@@ -842,7 +841,7 @@ impl SshRemoteConnection {
askpass.script_path().as_ref(),
connection_options.additional_args(),
&socket_path,
- &destination,
+ connection_options.ssh_destination_args(),
)?;
let result = select_biased! {
@@ -902,7 +901,7 @@ impl SshRemoteConnection {
askpass.script_path().as_ref(),
askpass.socket_path().as_ref(),
connection_options.additional_args(),
- &destination,
+ connection_options.ssh_destination_args(),
)?;
let result = select_biased! {
@@ -1395,7 +1394,9 @@ impl SshRemoteConnection {
if let Some(args) = additional_args {
command.args(args);
}
- command.arg(src_path).arg(format!(
+ // scp and sftp have no option for the username, so `--` keeps a destination beginning
+ // with `-` from being parsed as an option. They pass the username on to ssh via `-l`.
+ command.arg("--").arg(src_path).arg(format!(
"{}:{}",
self.socket.connection_options.scp_destination(),
dest_path_str
@@ -1418,7 +1419,9 @@ impl SshRemoteConnection {
.unwrap_or_default(),
);
command.arg("-b").arg("-");
- command.arg(self.socket.connection_options.scp_destination());
+ command
+ .arg("--")
+ .arg(self.socket.connection_options.scp_destination());
command.stdin(Stdio::piped());
command
}
@@ -1555,12 +1558,13 @@ impl SshSocket {
let separator = shell_kind.sequential_commands_separator();
format!("cd{separator} {to_run}")
};
- self.ssh_options(&mut command, true, None)
- .arg(self.connection_options.ssh_destination());
+ self.ssh_options(&mut command, true, None);
if !allow_pseudo_tty {
command.arg("-T");
}
- command.arg(to_run);
+ command
+ .args(self.connection_options.ssh_destination_args())
+ .arg(to_run);
log::debug!("ssh {:?}", command);
command
}
@@ -1949,17 +1953,19 @@ impl SshConnectionOptions {
})
}
- pub fn ssh_destination(&self) -> String {
- let mut result = String::default();
+ /// Arguments identifying the remote for `ssh`, which must come after all other options.
+ ///
+ /// The username is passed via `-l` and the host after `--` so that neither is parsed as
+ /// an option when it begins with `-`.
+ fn ssh_destination_args(&self) -> Vec<String> {
+ let mut args = Vec::new();
if let Some(username) = &self.username {
- // Username might be: username1@username2@ip2
- let username = urlencoding::encode(username);
- result.push_str(&username);
- result.push('@');
+ args.push("-l".to_string());
+ args.push(username.clone());
}
-
- result.push_str(&self.host.to_string());
- result
+ args.push("--".to_string());
+ args.push(self.host.to_string());
+ args
}
pub fn additional_args_for_scp(&self) -> Vec<String> {
@@ -2036,7 +2042,7 @@ fn build_command_posix(
ssh_shell: &str,
ssh_shell_kind: ShellKind,
ssh_options: Vec<String>,
- ssh_destination: &str,
+ ssh_destination_args: Vec<String>,
interactive: Interactive,
) -> Result<CommandTemplate> {
let mut exec = posix_working_dir_prefix(working_dir, ssh_path_style, ssh_shell_kind)?;
@@ -2070,7 +2076,7 @@ fn build_command_posix(
ssh_options,
port_forward,
interactive,
- ssh_destination,
+ ssh_destination_args,
exec,
ssh_env,
))
@@ -2121,7 +2127,7 @@ fn ssh_command_template(
ssh_options: Vec<String>,
port_forward: Option<(u16, String, u16)>,
interactive: Interactive,
- ssh_destination: &str,
+ ssh_destination_args: Vec<String>,
exec: String,
ssh_env: HashMap<String, String>,
) -> CommandTemplate {
@@ -2148,7 +2154,7 @@ fn ssh_command_template(
Interactive::No => args.push("-T".into()),
}
// The destination must come after all options but before the command
- args.push(ssh_destination.into());
+ args.extend(ssh_destination_args);
args.push(exec);
CommandTemplate {
@@ -2169,7 +2175,7 @@ fn build_command_windows(
ssh_shell: &str,
_ssh_shell_kind: ShellKind,
ssh_options: Vec<String>,
- ssh_destination: &str,
+ ssh_destination_args: Vec<String>,
interactive: Interactive,
) -> Result<CommandTemplate> {
use base64::Engine as _;
@@ -2244,7 +2250,7 @@ fn build_command_windows(
}
// The destination must come after all options but before the command
- args.push(ssh_destination.into());
+ args.extend(ssh_destination_args);
// Windows OpenSSH server incorrectly escapes the command string when the PTY is used.
// The simplest way to work around this is to use a base64 encoded command, which doesn't require escaping.
@@ -2397,7 +2403,10 @@ mod tests {
"-o",
"LogLevel=ERROR",
"-T",
- "user@host",
+ "-l",
+ "user",
+ "--",
+ "host",
expected_exec,
]
);
@@ -2700,7 +2709,12 @@ mod tests {
"/bin/bash",
ShellKind::Posix,
vec!["-o".to_string(), "ControlMaster=auto".to_string()],
- "user@host",
+ vec![
+ "-l".to_string(),
+ "user".to_string(),
+ "--".to_string(),
+ "host".to_string(),
+ ],
Interactive::No,
)?;
assert_eq!(command.program, "ssh");
@@ -2720,7 +2734,12 @@ mod tests {
"/bin/fish",
ShellKind::Fish,
vec!["-p".to_string(), "2222".to_string()],
- "user@host",
+ vec![
+ "-l".to_string(),
+ "user".to_string(),
+ "--".to_string(),
+ "host".to_string(),
+ ],
Interactive::Yes,
)?;
@@ -2733,7 +2752,10 @@ mod tests {
"-o",
"LogLevel=ERROR",
"-t",
- "user@host",
+ "-l",
+ "user",
+ "--",
+ "host",
"cd \"$HOME\"/work && exec env 'INPUT_VA=val' remote_program arg1 arg2"
]
);
@@ -2755,7 +2777,12 @@ mod tests {
"/bin/fish",
ShellKind::Fish,
vec!["-p".to_string(), "2222".to_string()],
- "user@host",
+ vec![
+ "-l".to_string(),
+ "user".to_string(),
+ "--".to_string(),
+ "host".to_string(),
+ ],
Interactive::Yes,
)?;
@@ -2770,7 +2797,10 @@ mod tests {
"-o",
"LogLevel=ERROR",
"-t",
- "user@host",
+ "-l",
+ "user",
+ "--",
+ "host",
"cd && exec env 'INPUT_VA=val' /bin/fish -l"
]
);
@@ -2795,7 +2825,12 @@ mod tests {
"/bin/bash",
ShellKind::Posix,
vec![],
- "user@host",
+ vec![
+ "-l".to_string(),
+ "user".to_string(),
+ "--".to_string(),
+ "host".to_string(),
+ ],
Interactive::No,
)?;
@@ -3002,7 +3037,12 @@ mod tests {
"/bin/bash",
ShellKind::Posix,
vec![],
- "user@host",
+ vec![
+ "-l".to_string(),
+ "user".to_string(),
+ "--".to_string(),
+ "host".to_string(),
+ ],
Interactive::No,
)?;
@@ -3014,4 +3054,47 @@ mod tests {
Ok(())
}
+
+ #[test]
+ fn test_ssh_destination_args_pass_username_verbatim() {
+ let args_for = |username: &str| {
+ SshConnectionOptions {
+ host: "192.168.1.50".into(),
+ username: Some(username.to_string()),
+ ..Default::default()
+ }
+ .ssh_destination_args()
+ };
+
+ assert_eq!(
+ args_for(r"DOMAIN\user"),
+ ["-l", r"DOMAIN\user", "--", "192.168.1.50"]
+ );
+ assert_eq!(
+ args_for("DOMAIN/user"),
+ ["-l", "DOMAIN/user", "--", "192.168.1.50"]
+ );
+ assert_eq!(
+ args_for("user@jumphost"),
+ ["-l", "user@jumphost", "--", "192.168.1.50"]
+ );
+ assert_eq!(
+ args_for("-oProxyCommand=touch /tmp/pwned"),
+ [
+ "-l",
+ "-oProxyCommand=touch /tmp/pwned",
+ "--",
+ "192.168.1.50"
+ ]
+ );
+
+ let options = SshConnectionOptions {
+ host: "-oProxyCommand=touch /tmp/pwned".into(),
+ ..Default::default()
+ };
+ assert_eq!(
+ options.ssh_destination_args(),
+ ["--", "-oProxyCommand=touch /tmp/pwned"]
+ );
+ }
}