diff --git a/cmd/detectExecuteScan.go b/cmd/detectExecuteScan.go index 5ab7de92e8..e5a006a1e3 100644 --- a/cmd/detectExecuteScan.go +++ b/cmd/detectExecuteScan.go @@ -546,7 +546,7 @@ func addDetectArgs(args []string, config detectExecuteScanOptions, utils detectU args = append(args, fmt.Sprintf("\"--detect.code.location.name=%v\"", codelocation)) if len(mavenArgs) > 0 && !checkIfArgumentIsInScanProperties(config, "detect.maven.build.command") { - args = append(args, fmt.Sprintf("\"--detect.maven.build.command=%v\"", strings.Join(mavenArgs, " "))) + args = append(args, fmt.Sprintf("\"--detect.maven.build.command=%s\"", quoteMavenArgs(mavenArgs))) } args = append(args, fmt.Sprintf("\"--detect.force.success.on.skip=true\"")) @@ -1188,3 +1188,16 @@ func findItemInStringSlice(slice []string, item string) int { } return -1 } + +func quoteMavenArgs(args []string) string { + // Quote any argument containing spaces to handle paths properly + quotedArgs := make([]string, len(args)) + for i, arg := range args { + if strings.Contains(arg, " ") { + quotedArgs[i] = fmt.Sprintf("'%s'", arg) + } else { + quotedArgs[i] = arg + } + } + return strings.Join(quotedArgs, " ") +} diff --git a/cmd/detectExecuteScan_test.go b/cmd/detectExecuteScan_test.go index 4701577aac..e466ca5f51 100644 --- a/cmd/detectExecuteScan_test.go +++ b/cmd/detectExecuteScan_test.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "fmt" - "io" "net/http" "os" @@ -1011,3 +1010,42 @@ func TestRunDetectWithContainerScanAndDistro(t *testing.T) { "Docker inspector parameters should not be included when containerScan is true") }) } + +func TestQuoteMavenArgs(t *testing.T) { + t.Parallel() + tt := []struct { + name string + args []string + expected string + }{ + { + name: "no spaces in arguments", + args: []string{"--global-settings", "/path/without/spaces.xml"}, + expected: "--global-settings /path/without/spaces.xml", + }, + { + name: "arguments with spaces", + args: []string{"--global-settings", "/path with spaces/settings.xml"}, + expected: "--global-settings '/path with spaces/settings.xml'", + }, + { + name: "mixed arguments", + args: []string{"--global-settings", "/path with spaces/settings.xml", "--settings", "/normal/path.xml"}, + expected: "--global-settings '/path with spaces/settings.xml' --settings /normal/path.xml", + }, + { + name: "multiple arguments with spaces", + args: []string{"--global-settings", "/path with spaces/settings.xml", "--settings", "/another path/with spaces.xml"}, + expected: "--global-settings '/path with spaces/settings.xml' --settings '/another path/with spaces.xml'", + }, + } + + for _, test := range tt { + test := test + t.Run(test.name, func(t *testing.T) { + t.Parallel() + result := quoteMavenArgs(test.args) + assert.Equal(t, test.expected, result) + }) + } +}