Skip to content

Commit

Permalink
fix windows cmd quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
jsturtevant committed Dec 16, 2020
1 parent 2ce56e7 commit 07d2a21
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
14 changes: 14 additions & 0 deletions integration-tests/goss/windows/tests/command.goss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ command:
- 0
stderr: []
timeout: 10000
wrap a powershell with quotes - expect 0 because travis does not restrict anonymous logins:
exec: powershell -noprofile -noninteractive -command "(get-itemproperty -path 'HKLM:/SYSTEM/CurrentControlSet/Control/Lsa/').restrictanonymous"
exit-status: 0
stdout:
- 0
stderr: []
timeout: 10000
powershell with quotes:
exec: powershell /c "(echo '{"b":2, "a":1}' | ConvertFrom-json).a"
exit-status: 0
stdout:
- "1"
stderr: []
timeout: 10000
2 changes: 1 addition & 1 deletion system/command_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import "github.com/aelsabbahy/goss/util"
const windowsShell string = "cmd"

func commandWrapper(cmd string) *util.Command {
return util.NewCommand(windowsShell, "/c", cmd)
return util.NewCommandForWindowsCmd(windowsShell, "/c", cmd)
}
8 changes: 8 additions & 0 deletions system/command_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ func TestCommandWrapper(t *testing.T) {
if c.Cmd.Path != cmdPath {
t.Errorf("Command not wrapped properly for Windows os. got %s, want: %s", c.Cmd.Path, cmdPath)
}

if c.Cmd.SysProcAttr.CmdLine != "/c echo hello world" {
t.Errorf("Command not wrapped properly for Windows cmd.exe. got %s, want: %s", c.Cmd.SysProcAttr.CmdLine, "/c echo hello world")
}

if len(c.Cmd.Args) != 1 {
t.Errorf("Args length should be blank. got: %d, want: %d", len(c.Cmd.Args), 1)
}
}
2 changes: 2 additions & 0 deletions util/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"bytes"

//"fmt"
"os/exec"
"syscall"
Expand All @@ -20,6 +21,7 @@ func NewCommand(name string, arg ...string) *Command {
command := new(Command)
command.name = name
command.Cmd = exec.Command(name, arg...)

return command
}

Expand Down
29 changes: 29 additions & 0 deletions util/command_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build windows

package util

import (
"strings"

//"fmt"
"os/exec"
"syscall"
)

func NewCommandForWindowsCmd(name string, arg ...string) *Command {
//fmt.Println(arg)
command := new(Command)
command.name = name

// cmd.exe has a unique unquoting algorithm
// provide the full command line in SysProcAttr.CmdLine, leaving Args empty.
// more information: https://golang.org/pkg/os/exec/#Command
command.Cmd = exec.Command(name)
command.Cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: false,
CmdLine: strings.Join(arg, " "),
CreationFlags: 0,
}

return command
}

0 comments on commit 07d2a21

Please sign in to comment.