-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(security): close shell denylist bypass vectors #820
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
Closed
+189
β56
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29be547
fix shell bypass commands by updating deny patterns (and tests)
GoCeylan 3045cad
address @sky5454 request to compile regex patterns in a txt file
GoCeylan e9a0480
Merge remote-tracking branch 'origin/main' into fix/shell-denylist-byβ¦
GoCeylan e98a491
test(shell): add CI test to validate default_deny_patterns.txt
GoCeylan 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,47 @@ | ||
| # Dangerous commands | ||
| \brm\s+-[rf]{1,2}\b | ||
| \bdel\s+/[fq]\b | ||
| \brmdir\s+/s\b | ||
| \b(format|mkfs|diskpart)\b\s | ||
| \bdd\s+if= | ||
| # Block writes to block devices (all common naming schemes) | ||
| >\s*/dev/(sd[a-z]|hd[a-z]|vd[a-z]|xvd[a-z]|nvme\d|mmcblk\d|loop\d|dm-\d|md\d|sr\d|nbd\d) | ||
| \b(shutdown|reboot|poweroff)\b | ||
| :\(\)\s*\{.*\};\s*: | ||
| \$\([^)]+\) | ||
| \$\{[^}]+\} | ||
| `[^`]+` | ||
| \|\s*sh\b | ||
| \|\s*bash\b | ||
| \|\s*/\S*(bash|sh|zsh|ksh|fish|csh|tcsh)\b | ||
| ;\s*rm\s+-[rf] | ||
| &&\s*rm\s+-[rf] | ||
| \|\|\s*rm\s+-[rf] | ||
| <<\s*EOF | ||
| <<< | ||
| \$\(\s*cat\s+ | ||
| \$\(\s*curl\s+ | ||
| \$\(\s*wget\s+ | ||
| \$\(\s*which\s+ | ||
| \bsudo\b | ||
| \bsu\b.*-c\b | ||
| \bchmod\s+[0-7]{3,4}\b | ||
| \bchown\b | ||
| \bpkill\b | ||
| \bkillall\b | ||
| \bkill\b | ||
| \bcurl\b.*\|\s*(sh|bash) | ||
| \bwget\b.*\|\s*(sh|bash) | ||
| \bnpm\s+install\s+-g\b | ||
| \bpip\s+install\s+--user\b | ||
| \bapt\s+(install|remove|purge)\b | ||
| \byum\s+(install|remove)\b | ||
| \bdnf\s+(install|remove)\b | ||
| \bdocker\s+run\b | ||
| \bdocker\s+exec\b | ||
| \bgit\s+push\b | ||
| \bgit\s+force\b | ||
| \bssh\b.*@ | ||
| \beval\b | ||
| \bsource\s+\S+ | ||
| (?:^|&&|\|\||;)\s*\.\s+\S |
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 |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package tools | |
| import ( | ||
| "bytes" | ||
| "context" | ||
| _ "embed" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
@@ -25,57 +26,45 @@ type ExecTool struct { | |
| restrictToWorkspace bool | ||
| } | ||
|
|
||
| var ( | ||
| defaultDenyPatterns = []*regexp.Regexp{ | ||
| regexp.MustCompile(`\brm\s+-[rf]{1,2}\b`), | ||
| regexp.MustCompile(`\bdel\s+/[fq]\b`), | ||
| regexp.MustCompile(`\brmdir\s+/s\b`), | ||
| // Match disk wiping commands (must be followed by space/args) | ||
| regexp.MustCompile( | ||
| `\b(format|mkfs|diskpart)\b\s`, | ||
| ), | ||
| regexp.MustCompile(`\bdd\s+if=`), | ||
| // Block writes to block devices (all common naming schemes). | ||
| regexp.MustCompile( | ||
| `>\s*/dev/(sd[a-z]|hd[a-z]|vd[a-z]|xvd[a-z]|nvme\d|mmcblk\d|loop\d|dm-\d|md\d|sr\d|nbd\d)`, | ||
| ), | ||
| regexp.MustCompile(`\b(shutdown|reboot|poweroff)\b`), | ||
| regexp.MustCompile(`:\(\)\s*\{.*\};\s*:`), | ||
| regexp.MustCompile(`\$\([^)]+\)`), | ||
| regexp.MustCompile(`\$\{[^}]+\}`), | ||
| regexp.MustCompile("`[^`]+`"), | ||
| regexp.MustCompile(`\|\s*sh\b`), | ||
| regexp.MustCompile(`\|\s*bash\b`), | ||
| regexp.MustCompile(`;\s*rm\s+-[rf]`), | ||
| regexp.MustCompile(`&&\s*rm\s+-[rf]`), | ||
| regexp.MustCompile(`\|\|\s*rm\s+-[rf]`), | ||
| regexp.MustCompile(`<<\s*EOF`), | ||
| regexp.MustCompile(`\$\(\s*cat\s+`), | ||
| regexp.MustCompile(`\$\(\s*curl\s+`), | ||
| regexp.MustCompile(`\$\(\s*wget\s+`), | ||
| regexp.MustCompile(`\$\(\s*which\s+`), | ||
| regexp.MustCompile(`\bsudo\b`), | ||
| regexp.MustCompile(`\bchmod\s+[0-7]{3,4}\b`), | ||
| regexp.MustCompile(`\bchown\b`), | ||
| regexp.MustCompile(`\bpkill\b`), | ||
| regexp.MustCompile(`\bkillall\b`), | ||
| regexp.MustCompile(`\bkill\b`), | ||
| regexp.MustCompile(`\bcurl\b.*\|\s*(sh|bash)`), | ||
| regexp.MustCompile(`\bwget\b.*\|\s*(sh|bash)`), | ||
| regexp.MustCompile(`\bnpm\s+install\s+-g\b`), | ||
| regexp.MustCompile(`\bpip\s+install\s+--user\b`), | ||
| regexp.MustCompile(`\bapt\s+(install|remove|purge)\b`), | ||
| regexp.MustCompile(`\byum\s+(install|remove)\b`), | ||
| regexp.MustCompile(`\bdnf\s+(install|remove)\b`), | ||
| regexp.MustCompile(`\bdocker\s+run\b`), | ||
| regexp.MustCompile(`\bdocker\s+exec\b`), | ||
| regexp.MustCompile(`\bgit\s+push\b`), | ||
| regexp.MustCompile(`\bgit\s+force\b`), | ||
| regexp.MustCompile(`\bssh\b.*@`), | ||
| regexp.MustCompile(`\beval\b`), | ||
| regexp.MustCompile(`\bsource\s+.*\.sh\b`), | ||
| //go:embed default_deny_patterns.txt | ||
| var defaultDenyPatternsText string | ||
|
|
||
| var defaultDenyPatterns = mustCompileRegexPatterns(parsePatternLines(defaultDenyPatternsText)) | ||
|
|
||
| func parsePatternLines(text string) []string { | ||
| lines := strings.Split(text, "\n") | ||
| patterns := make([]string, 0, len(lines)) | ||
| for _, line := range lines { | ||
| trimmed := strings.TrimSpace(line) | ||
| if trimmed == "" || strings.HasPrefix(trimmed, "#") { | ||
| continue | ||
| } | ||
| patterns = append(patterns, trimmed) | ||
| } | ||
| return patterns | ||
| } | ||
|
|
||
| func compileRegexPatterns(patterns []string) ([]*regexp.Regexp, error) { | ||
| compiled := make([]*regexp.Regexp, 0, len(patterns)) | ||
| for _, p := range patterns { | ||
| re, err := regexp.Compile(p) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid pattern %q: %w", p, err) | ||
| } | ||
| compiled = append(compiled, re) | ||
| } | ||
| return compiled, nil | ||
| } | ||
|
|
||
| func mustCompileRegexPatterns(patterns []string) []*regexp.Regexp { | ||
| compiled, err := compileRegexPatterns(patterns) | ||
| if err != nil { | ||
| panic("invalid default deny patterns: " + err.Error()) | ||
| } | ||
| return compiled | ||
| } | ||
|
|
||
| var ( | ||
| // absolutePathPattern matches absolute file paths in commands (Unix and Windows). | ||
| absolutePathPattern = regexp.MustCompile(`[A-Za-z]:\\[^\\\"']+|/[^\s\"']+`) | ||
|
|
||
|
|
@@ -371,13 +360,10 @@ func (t *ExecTool) SetRestrictToWorkspace(restrict bool) { | |
| } | ||
|
|
||
| func (t *ExecTool) SetAllowPatterns(patterns []string) error { | ||
| t.allowPatterns = make([]*regexp.Regexp, 0, len(patterns)) | ||
| for _, p := range patterns { | ||
| re, err := regexp.Compile(p) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid allow pattern %q: %w", p, err) | ||
| } | ||
| t.allowPatterns = append(t.allowPatterns, re) | ||
| compiled, err := compileRegexPatterns(patterns) | ||
|
Contributor
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. This encapsulates the regular expression, and the error printing is moved to the compiler egexpatterns function. Good |
||
| if err != nil { | ||
| return fmt.Errorf("invalid allow pattern: %w", err) | ||
| } | ||
| t.allowPatterns = compiled | ||
| return nil | ||
| } | ||
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.
I think these regular expression should be compiled in a list of text instead of changing the code all the time