From 3226be69e7b67392e83addac554f41f43a5faa58 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 7 Jan 2020 10:37:53 -0800 Subject: [PATCH] Fix issue with shell commands containing "<>" and unecessary (and poor) escaping --- config_test.go | 2 +- test/valid-verify-multi-line.yml | 2 +- util.go | 12 +----------- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/config_test.go b/config_test.go index 2618971..06d0e08 100644 --- a/config_test.go +++ b/config_test.go @@ -41,7 +41,7 @@ func TestMultiLineConfig(t *testing.T) { log.Println("-----") log.Println("TestMultiLineConfig(parse > string)") - expected := "echo 'Some string with stuff'; echo ''; exit 1\n" + expected := "echo 'Some string with stuff'; echo \"\"; exit 1\n" actual := config.Monitors[0].CommandShell if expected != actual { t.Errorf("TestMultiLineConfig(>) failed") diff --git a/test/valid-verify-multi-line.yml b/test/valid-verify-multi-line.yml index df76b58..3cbad19 100644 --- a/test/valid-verify-multi-line.yml +++ b/test/valid-verify-multi-line.yml @@ -5,7 +5,7 @@ monitors: - name: Shell command_shell: > echo 'Some string with stuff'; - echo ''; + echo ""; exit 1 alert_down: ['log_shell'] alert_after: 1 diff --git a/util.go b/util.go index 26c9c48..fbff395 100644 --- a/util.go +++ b/util.go @@ -5,19 +5,9 @@ import ( "strings" ) -// escapeCommandShell accepts a command to be executed by a shell and escapes it -func escapeCommandShell(command string) string { - // Remove extra spaces and newlines from ends - command = strings.TrimSpace(command) - // TODO: Not sure if this part is actually needed. Should verify - // Escape double quotes since this will be passed in as an argument - command = strings.Replace(command, `"`, `\"`, -1) - return command -} - // ShellCommand takes a string and executes it as a command using `sh` func ShellCommand(command string) *exec.Cmd { - shellCommand := []string{"sh", "-c", escapeCommandShell(command)} + shellCommand := []string{"sh", "-c", strings.TrimSpace(command)} //log.Printf("Shell command: %v", shellCommand) return exec.Command(shellCommand[0], shellCommand[1:]...) }