From 77aa2accb041c4a112486d93a28755aab509fa5a Mon Sep 17 00:00:00 2001 From: steve Date: Mon, 6 Jan 2025 15:46:04 -0500 Subject: [PATCH 1/2] Create function for escaping shell arguments --- src/command.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/command.rs b/src/command.rs index 03f26ec..6ae3b97 100644 --- a/src/command.rs +++ b/src/command.rs @@ -56,6 +56,14 @@ impl<'a> PreparedCommand<'a> { self } + pub fn escape_shell_arg(arg: &str) -> String { + arg.replace("\\", "\\\\") + .replace(" ", "\\ ") + .replace("\"", "\\\"") + .replace("'", "\\'") + .replace("|", "\\|") + } + fn prepare_command(&self) -> (OsString, Vec) { match self.target { CommandTarget::Local { dry, .. } | CommandTarget::Remote { dry, .. } @@ -103,9 +111,9 @@ impl<'a> PreparedCommand<'a> { // XXX: turns out when passing backslashes to ssh, they need // to be escaped twice - args.push(self.command.to_str().unwrap().replace("\\", "\\\\").into()); + args.push(Self::escape_shell_arg(self.command.to_str().unwrap()).into()); for arg in &self.args { - args.push(arg.to_str().unwrap().replace("\\", "\\\\").into()); + args.push(Self::escape_shell_arg(arg.to_str().unwrap()).into()); } (ssh, args) From b113c41cca96ce18b5418f4bc915c3eb8ff0e498 Mon Sep 17 00:00:00 2001 From: steve Date: Mon, 6 Jan 2025 15:49:11 -0500 Subject: [PATCH 2/2] fmt --- src/command.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/command.rs b/src/command.rs index 6ae3b97..8b222fe 100644 --- a/src/command.rs +++ b/src/command.rs @@ -58,10 +58,10 @@ impl<'a> PreparedCommand<'a> { pub fn escape_shell_arg(arg: &str) -> String { arg.replace("\\", "\\\\") - .replace(" ", "\\ ") - .replace("\"", "\\\"") - .replace("'", "\\'") - .replace("|", "\\|") + .replace(" ", "\\ ") + .replace("\"", "\\\"") + .replace("'", "\\'") + .replace("|", "\\|") } fn prepare_command(&self) -> (OsString, Vec) {