Skip to content

Commit 8370bc1

Browse files
committed
fix windows cmd quoting
1 parent efa0705 commit 8370bc1

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

integration-tests/goss/windows/tests/command.goss.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,17 @@ command:
1414
- 0
1515
stderr: []
1616
timeout: 10000
17+
wrap a powershell with quotes - expect 0 because travis does not restrict anonymous logins:
18+
exec: powershell -noprofile -noninteractive -command "(get-itemproperty -path 'HKLM:/SYSTEM/CurrentControlSet/Control/Lsa/').restrictanonymous"
19+
exit-status: 0
20+
stdout:
21+
- 0
22+
stderr: []
23+
timeout: 10000
24+
powershell with quotes:
25+
exec: powershell /c "(echo '{"b":2, "a":1}' | ConvertFrom-json).a"
26+
exit-status: 0
27+
stdout:
28+
- "1"
29+
stderr: []
30+
timeout: 10000

system/command_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import "github.com/aelsabbahy/goss/util"
77
const windowsShell string = "cmd"
88

99
func commandWrapper(cmd string) *util.Command {
10-
return util.NewCommand(windowsShell, "/c", cmd)
10+
return util.NewCommandForWindowsCmd(windowsShell, "/c", cmd)
1111
}

system/command_windows_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ func TestCommandWrapper(t *testing.T) {
1515
if c.Cmd.Path != cmdPath {
1616
t.Errorf("Command not wrapped properly for Windows os. got %s, want: %s", c.Cmd.Path, cmdPath)
1717
}
18+
19+
if c.Cmd.SysProcAttr.CmdLine != "/c echo hello world" {
20+
t.Errorf("Command not wrapped properly for Windows cmd.exe. got %s, want: %s", c.Cmd.SysProcAttr.CmdLine, "/c echo hello world")
21+
}
22+
23+
if len(c.Cmd.Args) != 1 {
24+
t.Errorf("Args length should be blank. got: %d, want: %d", len(c.Cmd.Args), 1)
25+
}
1826
}

util/command.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package util
22

33
import (
44
"bytes"
5+
"strings"
6+
57
//"fmt"
68
"os/exec"
79
"syscall"
@@ -20,6 +22,25 @@ func NewCommand(name string, arg ...string) *Command {
2022
command := new(Command)
2123
command.name = name
2224
command.Cmd = exec.Command(name, arg...)
25+
26+
return command
27+
}
28+
29+
func NewCommandForWindowsCmd(name string, arg ...string) *Command {
30+
//fmt.Println(arg)
31+
command := new(Command)
32+
command.name = name
33+
34+
// cmd.exe has a unique unquoting algorithm
35+
// provide the full command line in SysProcAttr.CmdLine, leaving Args empty.
36+
// more information: https://golang.org/pkg/os/exec/#Command
37+
command.Cmd = exec.Command(name)
38+
command.Cmd.SysProcAttr = &syscall.SysProcAttr{
39+
HideWindow: false,
40+
CmdLine: strings.Join(arg, " "),
41+
CreationFlags: 0,
42+
}
43+
2344
return command
2445
}
2546

0 commit comments

Comments
 (0)