-
Notifications
You must be signed in to change notification settings - Fork 70
fix/security: shell-escape attacker-controlled filenames in batch change templates #1332
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
cbrnrd
wants to merge
1
commit into
main
Choose a base branch
from
carterbrainerd-vuln-91-arbitrary-command-execution-in-src-cli-via-unescaped
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−14
Open
Changes from all commits
Commits
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
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,152 @@ | ||
| package template | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/lib/batches/execution" | ||
| "github.com/sourcegraph/sourcegraph/lib/batches/git" | ||
| ) | ||
|
|
||
| // TestVULN91_NoShellInjectionFromFilenames is a regression test for VULN-91 | ||
| // (HackerOne report 3767160). It verifies that attacker-controlled filenames | ||
| // surfaced through the various filename-bearing template variables cannot | ||
| // inject shell metacharacters into the rendered step script. | ||
| // | ||
| // The PoC in the report relies on filenames like | ||
| // | ||
| // `id > /tmp/PWNED`.go | ||
| // foo.go; echo INJECTED; #.go | ||
| // | ||
| // being rendered verbatim through `${{ join repository.search_result_paths " " }}` | ||
| // and friends, then executed under /bin/sh. After the fix, each filename must | ||
| // be shell-quoted so the metacharacters lose their shell meaning. | ||
| func TestVULN91_NoShellInjectionFromFilenames(t *testing.T) { | ||
| maliciousFilenames := []string{ | ||
| "main.go", | ||
| "`id > /tmp/PWNED && cat /tmp/PWNED`.go", | ||
| "foo.go; echo INJECTED_$(whoami) > /tmp/PWNED2; #.go", | ||
| "with space.go", | ||
| "with'quote.go", | ||
| "with\nnewline.go", | ||
| } | ||
|
|
||
| // These template snippets all unconditionally splat filename-bearing | ||
| // variables into a shell command. None of them may contain raw shell | ||
| // metacharacters after rendering. | ||
| templates := map[string]string{ | ||
| "step run via repository.search_result_paths": `gofmt -w ${{ join repository.search_result_paths " " }}`, | ||
| "step run via previous_step.modified_files": `gofmt -w ${{ join previous_step.modified_files " " }}`, | ||
| "step run via previous_step.added_files": `gofmt -w ${{ join previous_step.added_files " " }}`, | ||
| "step run via previous_step.deleted_files": `rm -f ${{ join previous_step.deleted_files " " }}`, | ||
| "step run via previous_step.renamed_files": `touch ${{ join previous_step.renamed_files " " }}`, | ||
| "step run via steps.modified_files": `gofmt -w ${{ join steps.modified_files " " }}`, | ||
| "step run via direct repository fmt": `gofmt -w ${{ repository.search_result_paths }}`, | ||
| } | ||
|
|
||
| stepCtx := &StepContext{ | ||
| Repository: Repository{ | ||
| Name: "github.com/example/repo", | ||
| Branch: "main", | ||
| FileMatches: maliciousFilenames, | ||
| }, | ||
| PreviousStep: execution.AfterStepResult{ | ||
| ChangedFiles: git.Changes{ | ||
| Modified: maliciousFilenames, | ||
| Added: maliciousFilenames, | ||
| Deleted: maliciousFilenames, | ||
| Renamed: maliciousFilenames, | ||
| }, | ||
| }, | ||
| Steps: StepsContext{ | ||
| Changes: git.Changes{ | ||
| Modified: maliciousFilenames, | ||
| Added: maliciousFilenames, | ||
| Deleted: maliciousFilenames, | ||
| Renamed: maliciousFilenames, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Tokens that, if they appear unescaped in the rendered output, would | ||
| // trigger shell command substitution, command chaining, or redirection. | ||
| dangerousSubstrings := []string{ | ||
| "`id", | ||
| "$(", | ||
| "; echo", | ||
| "> /tmp/PWNED", | ||
| "PWNED2", | ||
| } | ||
|
|
||
| for name, tmpl := range templates { | ||
| t.Run(name, func(t *testing.T) { | ||
| var out bytes.Buffer | ||
| if err := RenderStepTemplate("vuln-91", tmpl, &out, stepCtx); err != nil { | ||
| t.Fatalf("RenderStepTemplate returned error: %v", err) | ||
| } | ||
| rendered := out.String() | ||
| for _, bad := range dangerousSubstrings { | ||
| if containsUnquoted(rendered, bad) { | ||
| t.Errorf("rendered output contains unescaped shell metasequence %q\nrendered: %s", bad, rendered) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Also exercise the ChangesetTemplateContext path. | ||
| tmplCtx := &ChangesetTemplateContext{ | ||
| Repository: Repository{ | ||
| Name: "github.com/example/repo", | ||
| Branch: "main", | ||
| FileMatches: maliciousFilenames, | ||
| }, | ||
| Steps: StepsContext{ | ||
| Changes: git.Changes{ | ||
| Modified: maliciousFilenames, | ||
| Added: maliciousFilenames, | ||
| Deleted: maliciousFilenames, | ||
| Renamed: maliciousFilenames, | ||
| }, | ||
| }, | ||
| } | ||
| ctTemplates := map[string]string{ | ||
| "changeset via repository.search_result_paths": `body: ${{ join repository.search_result_paths " " }}`, | ||
| "changeset via steps.modified_files": `body: ${{ join steps.modified_files " " }}`, | ||
| } | ||
| for name, tmpl := range ctTemplates { | ||
| t.Run(name, func(t *testing.T) { | ||
| rendered, err := RenderChangesetTemplateField("vuln-91", tmpl, tmplCtx) | ||
| if err != nil { | ||
| t.Fatalf("RenderChangesetTemplateField returned error: %v", err) | ||
| } | ||
| for _, bad := range dangerousSubstrings { | ||
| if containsUnquoted(rendered, bad) { | ||
| t.Errorf("rendered output contains unescaped shell metasequence %q\nrendered: %s", bad, rendered) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // containsUnquoted reports whether needle occurs in haystack outside of a | ||
| // single-quoted shell context. After shellquote.Join, dangerous bytes are | ||
| // either backslash-escaped (e.g. \`) or wrapped in single quotes that | ||
| // terminate the surrounding string literal, so any naive substring match | ||
| // reliably finds an injection. We additionally allow the substring to appear | ||
| // inside a single-quoted region (which shell does NOT interpret) to avoid | ||
| // false positives on the literal text of the (now safely quoted) filename. | ||
| func containsUnquoted(haystack, needle string) bool { | ||
| for i := 0; i+len(needle) <= len(haystack); i++ { | ||
| if haystack[i:i+len(needle)] != needle { | ||
| continue | ||
| } | ||
| // Count single quotes before this position; if odd, we're inside a | ||
| // single-quoted region and the bytes are harmless. | ||
| quotes := strings.Count(haystack[:i], "'") | ||
| if quotes%2 == 0 { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } |
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.
no need to sort
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.
Why not? the previous version of the function sorted the paths.