Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"os"
"os/exec"
"regexp"
"strings"
"time"

"cloud.google.com/go/auth/internal"
"github.com/mattn/go-shellwords"
)

const (
Expand Down Expand Up @@ -77,7 +77,13 @@ func (r runtimeEnvironment) now() time.Time {
}

func (r runtimeEnvironment) run(ctx context.Context, command string, env []string) ([]byte, error) {
splitCommand := strings.Fields(command)
splitCommand, err := shellwords.Parse(command)
if err != nil {
return nil, err
}
if len(splitCommand) == 0 {
return nil, errors.New("credentials: executable command is empty")
}
cmd := exec.CommandContext(ctx, splitCommand[0], splitCommand[1:]...)
cmd.Env = env

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,57 @@ import (
"cloud.google.com/go/auth/internal"
"cloud.google.com/go/auth/internal/credsfile"
"github.com/google/go-cmp/cmp"
"github.com/mattn/go-shellwords"
)

func TestShellwordsParse(t *testing.T) {
tests := []struct {
name string
command string
want []string
wantErr bool
}{
{
name: "simple command",
command: "a b c",
want: []string{"a", "b", "c"},
},
{
name: "quoted argument",
command: `a "b c"`,
want: []string{"a", "b c"},
},
{
name: "quoted executable",
command: `"a b" c`,
want: []string{"a b", "c"},
},
{
name: "quoted executable and argument",
command: `"a b" "c d"`,
want: []string{"a b", "c d"},
},
{
name: "single quotes",
command: `'a b' 'c d'`,
want: []string{"a b", "c d"},
},
}
Comment on lines +33 to +64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This test is a good addition for verifying the new parsing logic. To improve test coverage for the new error handling in the run function, please consider adding test cases for empty and whitespace-only command strings. These inputs cause shellwords.Parse to return an empty slice, which triggers the new if len(splitCommand) == 0 check.

 	tests := []struct {
 		name    string
 		command string
 		want    []string
 		wantErr bool
 	}{
 		{
 			name:    "simple command",
 			command: "a b c",
 			want:    []string{"a", "b", "c"},
 		},
 		{
 			name:    "quoted argument",
 			command: `a "b c"`,
 			want:    []string{"a", "b c"},
 		},
 		{
 			name:    "quoted executable",
 			command: `"a b" c`,
 			want:    []string{"a b", "c"},
 		},
 		{
 			name:    "quoted executable and argument",
 			command: `"a b" "c d"`,
 			want:    []string{"a b", "c d"},
 		},
 		{
 			name:    "single quotes",
 			command: `'a b' 'c d'`,
 			want:    []string{"a b", "c d"},
 		},
 		{
 			name:    "empty command",
 			command: "",
 			want:    []string{},
 		},
 		{
 			name:    "whitespace only",
 			command: "   ",
 			want:    []string{},
 		},
 	}


for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := shellwords.Parse(tt.command)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(got, tt.want) {
t.Errorf("Parse() = %v, want %v", got, tt.want)
}
})
}
}

var executablesAllowed = map[string]string{
allowExecutablesEnvVar: "1",
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ require (
github.com/google/s2a-go v0.1.9 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/spiffe/go-spiffe/v2 v2.5.0 // indirect
github.com/zeebo/errs v1.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.6 h1:GW/XbdyBFQ8Qe+YAmFU
github.com/googleapis/enterprise-certificate-proxy v0.3.6/go.mod h1:MkHOF77EYAE7qfSuSS9PU6g4Nt4e11cnsDUowfwewLA=
github.com/googleapis/gax-go/v2 v2.15.0 h1:SyjDc1mGgZU5LncH8gimWo9lW1DtIfPibOG81vgd/bo=
github.com/googleapis/gax-go/v2 v2.15.0/go.mod h1:zVVkkxAQHa1RQpg9z2AUCMnKhi0Qld9rcmyfL1OZhoc=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
Expand Down
Loading