-
Notifications
You must be signed in to change notification settings - Fork 604
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
53493bc
9d8dbfd
695b7c2
f2e25aa
1d15155
6fe7a89
67e5a1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work in all platforms and cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, " ") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
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?