Skip to content

fix: escape path in maven when user inputs a space #5346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
15 changes: 14 additions & 1 deletion cmd/detectExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")))

Choose a reason for hiding this comment

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

Cant users escape the space in path?

args = append(args, fmt.Sprintf("\"--detect.maven.build.command=%s\"", quoteMavenArgs(mavenArgs)))
}

args = append(args, fmt.Sprintf("\"--detect.force.success.on.skip=true\""))
Expand Down Expand Up @@ -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))
Copy link
Member

@manjunathSurendrakumar manjunathSurendrakumar May 8, 2025

Choose a reason for hiding this comment

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

Does this work in all platforms and cases?

Choose a reason for hiding this comment

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

We don't own this step :)

for i, arg := range args {
if strings.Contains(arg, " ") {
quotedArgs[i] = fmt.Sprintf("'%s'", arg)
} else {
quotedArgs[i] = arg
}
}
return strings.Join(quotedArgs, " ")
}
40 changes: 39 additions & 1 deletion cmd/detectExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"context"
"fmt"

"io"
"net/http"
"os"
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

What is the use of this directive?

t.Run(test.name, func(t *testing.T) {
t.Parallel()
result := quoteMavenArgs(test.args)
assert.Equal(t, test.expected, result)
})
}
}
Loading