Skip to content

Commit d5a156b

Browse files
remote: Exclude port-forward flags in scp commands (#40402)
Closes #36454 Release Notes: - Exclude port-forward flags in `scp` commands for file and directory uploads
1 parent 6c3a7f6 commit d5a156b

File tree

1 file changed

+73
-8
lines changed
  • crates/remote/src/transport

1 file changed

+73
-8
lines changed

crates/remote/src/transport/ssh.rs

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl RemoteConnection for SshRemoteConnection {
173173
let mut command = util::command::new_smol_command("scp");
174174
let output = self
175175
.socket
176-
.ssh_options(&mut command)
176+
.ssh_options(&mut command, false)
177177
.args(
178178
self.socket
179179
.connection_options
@@ -648,7 +648,7 @@ impl SshRemoteConnection {
648648
let mut command = util::command::new_smol_command("scp");
649649
let output = self
650650
.socket
651-
.ssh_options(&mut command)
651+
.ssh_options(&mut command, false)
652652
.args(
653653
self.socket
654654
.connection_options
@@ -729,7 +729,7 @@ impl SshSocket {
729729
to_run.push_str(&shlex::try_quote(arg.as_ref()).unwrap());
730730
}
731731
let to_run = format!("cd; {to_run}");
732-
self.ssh_options(&mut command)
732+
self.ssh_options(&mut command, true)
733733
.arg(self.connection_options.ssh_url())
734734
.arg("-T")
735735
.arg(to_run);
@@ -748,23 +748,43 @@ impl SshSocket {
748748
}
749749

750750
#[cfg(not(target_os = "windows"))]
751-
fn ssh_options<'a>(&self, command: &'a mut process::Command) -> &'a mut process::Command {
751+
fn ssh_options<'a>(
752+
&self,
753+
command: &'a mut process::Command,
754+
include_port_forwards: bool,
755+
) -> &'a mut process::Command {
756+
let args = if include_port_forwards {
757+
self.connection_options.additional_args()
758+
} else {
759+
self.connection_options.additional_args_for_scp()
760+
};
761+
752762
command
753763
.stdin(Stdio::piped())
754764
.stdout(Stdio::piped())
755765
.stderr(Stdio::piped())
756-
.args(self.connection_options.additional_args())
766+
.args(args)
757767
.args(["-o", "ControlMaster=no", "-o"])
758768
.arg(format!("ControlPath={}", self.socket_path.display()))
759769
}
760770

761771
#[cfg(target_os = "windows")]
762-
fn ssh_options<'a>(&self, command: &'a mut process::Command) -> &'a mut process::Command {
772+
fn ssh_options<'a>(
773+
&self,
774+
command: &'a mut process::Command,
775+
include_port_forwards: bool,
776+
) -> &'a mut process::Command {
777+
let args = if include_port_forwards {
778+
self.connection_options.additional_args()
779+
} else {
780+
self.connection_options.additional_args_for_scp()
781+
};
782+
763783
command
764784
.stdin(Stdio::piped())
765785
.stdout(Stdio::piped())
766786
.stderr(Stdio::piped())
767-
.args(self.connection_options.additional_args())
787+
.args(args)
768788
.envs(self.envs.clone())
769789
}
770790

@@ -991,8 +1011,12 @@ impl SshConnectionOptions {
9911011
result
9921012
}
9931013

1014+
pub fn additional_args_for_scp(&self) -> Vec<String> {
1015+
self.args.iter().flatten().cloned().collect::<Vec<String>>()
1016+
}
1017+
9941018
pub fn additional_args(&self) -> Vec<String> {
995-
let mut args = self.args.iter().flatten().cloned().collect::<Vec<String>>();
1019+
let mut args = self.additional_args_for_scp();
9961020

9971021
if let Some(forwards) = &self.port_forwards {
9981022
args.extend(forwards.iter().map(|pf| {
@@ -1169,4 +1193,45 @@ mod tests {
11691193

11701194
Ok(())
11711195
}
1196+
1197+
#[test]
1198+
fn scp_args_exclude_port_forward_flags() {
1199+
let options = SshConnectionOptions {
1200+
host: "example.com".into(),
1201+
args: Some(vec![
1202+
"-p".to_string(),
1203+
"2222".to_string(),
1204+
"-o".to_string(),
1205+
"StrictHostKeyChecking=no".to_string(),
1206+
]),
1207+
port_forwards: Some(vec![SshPortForwardOption {
1208+
local_host: Some("127.0.0.1".to_string()),
1209+
local_port: 8080,
1210+
remote_host: Some("127.0.0.1".to_string()),
1211+
remote_port: 80,
1212+
}]),
1213+
..Default::default()
1214+
};
1215+
1216+
let ssh_args = options.additional_args();
1217+
assert!(
1218+
ssh_args.iter().any(|arg| arg.starts_with("-L")),
1219+
"expected ssh args to include port-forward: {ssh_args:?}"
1220+
);
1221+
1222+
let scp_args = options.additional_args_for_scp();
1223+
assert_eq!(
1224+
scp_args,
1225+
vec![
1226+
"-p".to_string(),
1227+
"2222".to_string(),
1228+
"-o".to_string(),
1229+
"StrictHostKeyChecking=no".to_string()
1230+
]
1231+
);
1232+
assert!(
1233+
scp_args.iter().all(|arg| !arg.starts_with("-L")),
1234+
"scp args should not contain port forward flags: {scp_args:?}"
1235+
);
1236+
}
11721237
}

0 commit comments

Comments
 (0)