diff options
author | Ryooooooga <ryoga_314@yahoo.co.jp> | 2021-03-01 23:16:48 +0900 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-03-13 10:49:40 +1100 |
commit | 50e4e9d58da73abbd0a80239d6917e6753ee78ae (patch) | |
tree | 5f5936f2362edcc5240dbe3dee3343b9756dd4b8 /pkg/commands/oscommands | |
parent | 03b9db5e0ad81fb38526dc7120358186c9e177f9 (diff) |
fix command escaping
Diffstat (limited to 'pkg/commands/oscommands')
-rw-r--r-- | pkg/commands/oscommands/os.go | 31 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_default_platform.go | 15 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_test.go | 19 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_windows.go | 11 |
4 files changed, 46 insertions, 30 deletions
diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 2638f3c49..cc66f2cab 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -8,7 +8,6 @@ import ( "os/exec" "path/filepath" "regexp" - "strconv" "strings" "sync" @@ -24,14 +23,13 @@ import ( // Platform stores the os state type Platform struct { - OS string - CatCmd string - Shell string - ShellArg string - EscapedQuote string - OpenCommand string - OpenLinkCommand string - FallbackEscapedQuote string + OS string + CatCmd string + Shell string + ShellArg string + EscapedQuote string + OpenCommand string + OpenLinkCommand string } // OSCommand holds all the os commands @@ -129,7 +127,7 @@ func (c *OSCommand) ShellCommandFromString(commandStr string) *exec.Cmd { if c.Platform.OS == "windows" { quotedCommand = commandStr } else { - quotedCommand = strconv.Quote(commandStr) + quotedCommand = c.Quote(commandStr) } shellCommand := fmt.Sprintf("%s %s %s", c.Platform.Shell, c.Platform.ShellArg, quotedCommand) @@ -245,11 +243,16 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *ex // Quote wraps a message in platform-specific quotation marks func (c *OSCommand) Quote(message string) string { - message = strings.Replace(message, "`", "\\`", -1) - escapedQuote := c.Platform.EscapedQuote - if strings.Contains(message, c.Platform.EscapedQuote) { - escapedQuote = c.Platform.FallbackEscapedQuote + if c.Platform.OS == "windows" { + message = strings.Replace(message, `"`, `"'"'"`, -1) + message = strings.Replace(message, `\"`, `\\"`, -1) + } else { + message = strings.Replace(message, `\`, `\\`, -1) + message = strings.Replace(message, `"`, `\"`, -1) + message = strings.Replace(message, "`", "\\`", -1) + message = strings.Replace(message, "$", "\\$", -1) } + escapedQuote := c.Platform.EscapedQuote return escapedQuote + message + escapedQuote } diff --git a/pkg/commands/oscommands/os_default_platform.go b/pkg/commands/oscommands/os_default_platform.go index 7ba166428..80ef1aebd 100644 --- a/pkg/commands/oscommands/os_default_platform.go +++ b/pkg/commands/oscommands/os_default_platform.go @@ -8,13 +8,12 @@ import ( func getPlatform() *Platform { return &Platform{ - OS: runtime.GOOS, - CatCmd: "cat", - Shell: "bash", - ShellArg: "-c", - EscapedQuote: "'", - OpenCommand: "open {{filename}}", - OpenLinkCommand: "open {{link}}", - FallbackEscapedQuote: "\"", + OS: runtime.GOOS, + CatCmd: "cat", + Shell: "bash", + ShellArg: "-c", + EscapedQuote: `"`, + OpenCommand: "open {{filename}}", + OpenLinkCommand: "open {{link}}", } } diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go index 1504e4322..e6f3bd33d 100644 --- a/pkg/commands/oscommands/os_test.go +++ b/pkg/commands/oscommands/os_test.go @@ -114,6 +114,8 @@ func TestOSCommandOpenFile(t *testing.T) { func TestOSCommandQuote(t *testing.T) { osCommand := NewDummyOSCommand() + osCommand.Platform.OS = "linux" + actual := osCommand.Quote("hello `test`") expected := osCommand.Platform.EscapedQuote + "hello \\`test\\`" + osCommand.Platform.EscapedQuote @@ -129,7 +131,7 @@ func TestOSCommandQuoteSingleQuote(t *testing.T) { actual := osCommand.Quote("hello 'test'") - expected := osCommand.Platform.FallbackEscapedQuote + "hello 'test'" + osCommand.Platform.FallbackEscapedQuote + expected := osCommand.Platform.EscapedQuote + "hello 'test'" + osCommand.Platform.EscapedQuote assert.EqualValues(t, expected, actual) } @@ -142,7 +144,20 @@ func TestOSCommandQuoteDoubleQuote(t *testing.T) { actual := osCommand.Quote(`hello "test"`) - expected := osCommand.Platform.EscapedQuote + "hello \"test\"" + osCommand.Platform.EscapedQuote + expected := osCommand.Platform.EscapedQuote + `hello \"test\"` + osCommand.Platform.EscapedQuote + + assert.EqualValues(t, expected, actual) +} + +// TestOSCommandQuoteWindows tests the quote function for Windows +func TestOSCommandQuoteWindows(t *testing.T) { + osCommand := NewDummyOSCommand() + + osCommand.Platform.OS = "windows" + + actual := osCommand.Quote(`hello "test"`) + + expected := osCommand.Platform.EscapedQuote + `hello "'"'"test"'"'"` + osCommand.Platform.EscapedQuote assert.EqualValues(t, expected, actual) } diff --git a/pkg/commands/oscommands/os_windows.go b/pkg/commands/oscommands/os_windows.go index 3f4f7e5a1..3e86c803f 100644 --- a/pkg/commands/oscommands/os_windows.go +++ b/pkg/commands/oscommands/os_windows.go @@ -2,11 +2,10 @@ package oscommands func getPlatform() *Platform { return &Platform{ - OS: "windows", - CatCmd: "cmd /c type", - Shell: "cmd", - ShellArg: "/c", - EscapedQuote: `\"`, - FallbackEscapedQuote: "\\'", + OS: "windows", + CatCmd: "cmd /c type", + Shell: "cmd", + ShellArg: "/c", + EscapedQuote: `\"`, } } |