-
Notifications
You must be signed in to change notification settings - Fork 351
fix: reject package/image names starting with '-' to prevent argument injection (#23070) #23045
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| //go:build !integration | ||
|
|
||
| // Tests for argument injection prevention: package and image names starting with '-' | ||
| // must be rejected before being passed to exec.Command calls (npm, pip, uv, docker). | ||
|
|
||
| package workflow | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestRejectHyphenPrefixPackages tests the shared helper that guards against | ||
| // argument injection in all package validation paths. | ||
| func TestRejectHyphenPrefixPackages(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| packages []string | ||
| kind string | ||
| expectError bool | ||
| errContains string | ||
| }{ | ||
| { | ||
| name: "empty list is accepted", | ||
| packages: []string{}, | ||
| kind: "npx", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "valid package names are accepted", | ||
| packages: []string{"express", "@scope/pkg", "requests==2.28.0"}, | ||
| kind: "pip", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "single hyphen prefix is rejected", | ||
| packages: []string{"-exploit"}, | ||
| kind: "npx", | ||
| expectError: true, | ||
| errContains: "must not start with '-'", | ||
| }, | ||
| { | ||
| name: "double hyphen prefix is rejected", | ||
| packages: []string{"--privileged"}, | ||
| kind: "uv", | ||
| expectError: true, | ||
| errContains: "must not start with '-'", | ||
| }, | ||
| { | ||
| name: "mixed list with one invalid is rejected", | ||
| packages: []string{"valid-pkg", "-exploit"}, | ||
| kind: "pip", | ||
| expectError: true, | ||
| errContains: "-exploit", | ||
| }, | ||
| { | ||
| name: "package with version specifier starting with hyphen is rejected", | ||
| packages: []string{"-exploit==1.0"}, | ||
| kind: "uv", | ||
| expectError: true, | ||
| errContains: "-exploit==1.0", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := rejectHyphenPrefixPackages(tt.packages, tt.kind) | ||
| if tt.expectError { | ||
| if err == nil { | ||
| t.Errorf("expected error for packages %v but got none", tt.packages) | ||
| return | ||
| } | ||
| if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) { | ||
| t.Errorf("expected error to contain %q, got: %v", tt.errContains, err) | ||
| } | ||
| } else { | ||
| if err != nil { | ||
| t.Errorf("unexpected error for packages %v: %v", tt.packages, err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestValidateDockerImage_RejectsHyphenPrefix tests that Docker image names starting | ||
| // with '-' are rejected without invoking docker. This is the primary injection path | ||
| // because image names are taken directly from frontmatter (not filtered by extractors). | ||
| func TestValidateDockerImage_RejectsHyphenPrefix(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| image string | ||
| errContains string | ||
| }{ | ||
| { | ||
| name: "single hyphen prefix", | ||
| image: "-exploit", | ||
| errContains: "must not start with '-'", | ||
| }, | ||
| { | ||
| name: "double hyphen prefix", | ||
| image: "--privileged", | ||
| errContains: "must not start with '-'", | ||
| }, | ||
| { | ||
| name: "looks like a docker flag", | ||
| image: "-rm", | ||
| errContains: "must not start with '-'", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := validateDockerImage(tt.image, false) | ||
| if err == nil { | ||
| t.Errorf("expected error for image %q but got none", tt.image) | ||
| return | ||
| } | ||
| if !strings.Contains(err.Error(), tt.errContains) { | ||
| t.Errorf("expected error to contain %q, got: %v", tt.errContains, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestExtractNpxFromCommands_HyphenPrefixFiltered shows that the npx package | ||
| // extractor already filters out names starting with '-' (treating them as flags), | ||
| // so they never reach the validation guard. | ||
| func TestExtractNpxFromCommands_HyphenPrefixFiltered(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| commands string | ||
| wantLen int | ||
| }{ | ||
| { | ||
| name: "lone hyphen-prefix arg yields no packages", | ||
| commands: "npx -exploit", | ||
| wantLen: 0, | ||
| }, | ||
| { | ||
| name: "double-hyphen arg yields no packages", | ||
| commands: "npx --exploit", | ||
| wantLen: 0, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| packages := extractNpxFromCommands(tt.commands) | ||
| if len(packages) != tt.wantLen { | ||
| t.Errorf("expected %d packages, got %d: %v", tt.wantLen, len(packages), packages) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestExtractPipFromCommands_HyphenPrefixFiltered shows that the pip package | ||
| // extractor already filters out names starting with '-', so they never reach | ||
| // the validation guard. | ||
| func TestExtractPipFromCommands_HyphenPrefixFiltered(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| commands string | ||
| wantLen int | ||
| }{ | ||
| { | ||
| name: "hyphen-prefix pip package yields no packages", | ||
| commands: "pip install -exploit", | ||
| wantLen: 0, | ||
| }, | ||
| { | ||
| name: "double-hyphen pip package yields no packages", | ||
| commands: "pip install --exploit", | ||
| wantLen: 0, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| packages := extractPipFromCommands(tt.commands) | ||
| if len(packages) != tt.wantLen { | ||
| t.Errorf("expected %d packages, got %d: %v", tt.wantLen, len(packages), packages) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestCollectPackagesFromWorkflow_HyphenPrefixInArgs verifies that the MCP tool | ||
| // args extractor also filters out names starting with '-', providing an additional | ||
| // layer of defense for the structured args format. | ||
| func TestCollectPackagesFromWorkflow_HyphenPrefixInArgs(t *testing.T) { | ||
| workflowData := &WorkflowData{ | ||
| Tools: map[string]any{ | ||
| "test-tool": map[string]any{ | ||
| "command": "npx", | ||
| "args": []any{"-exploit", "safe-package"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| packages := collectPackagesFromWorkflow(workflowData, extractNpxFromCommands, "npx") | ||
|
|
||
| for _, pkg := range packages { | ||
| if strings.HasPrefix(pkg, "-") { | ||
| t.Errorf("package starting with '-' should not be collected: %s", pkg) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| //go:build !js && !wasm | ||
|
|
||
| // This file provides package and image name validation utilities for agentic workflows. | ||
| // | ||
| // # Name Validation | ||
| // | ||
| // Package and image names passed to external tools (npm, pip, uv, docker) must not | ||
| // start with '-'. A leading '-' would be interpreted as a command-line flag by the | ||
| // downstream tool, causing unintended argument injection. | ||
| // | ||
| // Note: exec.Command uses argv directly (not sh -c), so this is argument injection, | ||
| // not shell injection. The risk is low — compilation runs on the developer's local | ||
| // machine with the developer's own privileges. | ||
|
|
||
| package workflow | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // rejectHyphenPrefixPackages returns a ValidationError if any of the provided | ||
| // names starts with '-'. The kind parameter (e.g. "npx", "pip", "uv") is used | ||
| // in the error messages. | ||
| // | ||
| // Names starting with '-' would be interpreted as flags by the downstream CLI | ||
| // tool, constituting argument injection into the exec.Command call. | ||
| func rejectHyphenPrefixPackages(names []string, kind string) error { | ||
| var invalid []string | ||
| for _, name := range names { | ||
| if strings.HasPrefix(name, "-") { | ||
| invalid = append(invalid, fmt.Sprintf("%s package name '%s' is invalid: names must not start with '-'", kind, name)) | ||
| } | ||
| } | ||
| if len(invalid) == 0 { | ||
| return nil | ||
| } | ||
| return NewValidationError( | ||
| kind+".packages", | ||
| fmt.Sprintf("%d invalid package names", len(invalid)), | ||
| kind+" package names must not start with '-'", | ||
| "Fix invalid package names:\n\n"+strings.Join(invalid, "\n"), | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Grammar nit in the doc comment: "names starts with '-'" should be "names start with '-'".