-
Notifications
You must be signed in to change notification settings - Fork 3
fix: validate shell_wrapper template against injection #81
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: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -938,16 +938,43 @@ pub fn resolve_shell_wrapper( | |||||||||||||||||||||||||||||
| resolve_hook_with_parent(project_hooks, parent_hooks, global_hooks, |h| &h.terminal.shell_wrapper) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Dangerous shell metacharacters that indicate injection attempts in wrapper templates. | ||||||||||||||||||||||||||||||
| const DANGEROUS_PATTERNS: &[&str] = &[";", "&&", "||", "|", "`", "$(", ">", "<"]; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Validate a `shell_wrapper` template against shell injection. | ||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||
| /// Splits the wrapper on `{shell}` and checks that the surrounding parts do not contain | ||||||||||||||||||||||||||||||
| /// dangerous shell metacharacters (`;`, `&&`, `||`, `|`, `` ` ``, `$(`, `>`, `<`). | ||||||||||||||||||||||||||||||
| /// Returns `Ok(())` if the template is safe, or `Err(message)` describing the problem. | ||||||||||||||||||||||||||||||
| pub fn validate_shell_wrapper(wrapper: &str) -> Result<(), String> { | ||||||||||||||||||||||||||||||
| let parts: Vec<&str> = wrapper.split("{shell}").collect(); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| let parts: Vec<&str> = wrapper.split("{shell}").collect(); | |
| let parts: Vec<&str> = wrapper.split("{shell}").collect(); | |
| let occurrences = parts.len().saturating_sub(1); | |
| if occurrences == 0 { | |
| return Err(format!( | |
| "shell_wrapper must contain the placeholder {{shell}} exactly once: {:?}", | |
| wrapper, | |
| )); | |
| } else if occurrences > 1 { | |
| return Err(format!( | |
| "shell_wrapper must contain the placeholder {{shell}} exactly once, but it appears {} times: {:?}", | |
| occurrences, wrapper, | |
| )); | |
| } |
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.
validate_shell_wrapper()still allows command separators not inDANGEROUS_PATTERNS, so injection is still possible (e.g."malicious & {shell}"or"malicious\n{shell}"will pass validation but executemalicious). Consider expanding validation to reject single&and any newline/CR characters (and add unit tests for these cases).