generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 297
feat: Allow readonly find -exec
commands without permission prompt
#2829
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
h16rkim
wants to merge
6
commits into
aws:main
Choose a base branch
from
h16rkim:fix/execute-bash-readonly
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8ed2ab
feat: Update `find` command handling to validate readonly `-exec` act…
yakpoong 63d056c
feat: Enhance argument safety checks with mid-pattern validation
yakpoong dc228cd
refactor: Simplify dangerous pattern detection logic in argument checks
yakpoong 5d5b648
feat: Refine `find` command handling for multiple `-exec` validation
yakpoong 78f98b4
refactor: Consolidate dangerous pattern checks with `contains_in_midd…
yakpoong 6dc7a05
refactor: Rename and update `contains_in_middle` to `contains_but_not…
yakpoong 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 |
---|---|---|
|
@@ -72,9 +72,9 @@ impl ExecuteCommand { | |
}; | ||
const DANGEROUS_PATTERNS: &[&str] = &["<(", "$(", "`", ">", "&&", "||", "&", ";", "${", "\n", "\r", "IFS"]; | ||
|
||
if args | ||
if DANGEROUS_PATTERNS | ||
.iter() | ||
.any(|arg| DANGEROUS_PATTERNS.iter().any(|p| arg.contains(p))) | ||
.any(|p| contains_but_not_ends_with(&self.command, p)) | ||
{ | ||
return true; | ||
} | ||
|
@@ -109,14 +109,28 @@ impl ExecuteCommand { | |
Some(cmd) | ||
if cmd == "find" | ||
&& cmd_args.iter().any(|arg| { | ||
arg.contains("-exec") // includes -execdir | ||
|| arg.contains("-delete") | ||
|| arg.contains("-ok") // includes -okdir | ||
|| arg.contains("-fprint") // includes -fprint0 and -fprintf | ||
arg.contains("-delete") | ||
|| arg.contains("-ok") // includes -okdir | ||
|| arg.contains("-fprint") // includes -fprint0 and -fprintf | ||
}) => | ||
{ | ||
return true; | ||
}, | ||
// Check -exec commands separately to allow readonly commands | ||
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.
|
||
Some(cmd) if cmd == "find" && cmd_args.iter().any(|arg| arg.contains("-exec")) => { | ||
// Find all -exec arguments and check if ANY command is non-readonly | ||
for (i, arg) in cmd_args.iter().enumerate() { | ||
if arg == "-exec" || arg == "-execdir" { | ||
// Check if there's a next argument (the command to execute) | ||
if let Some(exec_cmd) = cmd_args.get(i + 1) { | ||
if !READONLY_COMMANDS.contains(&exec_cmd.as_str()) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
}, | ||
Some(cmd) => { | ||
// Special casing for `grep`. -P flag for perl regexp has RCE issues, apparently | ||
// should not be supported within grep but is flagged as a possibility since this is perl | ||
|
@@ -268,13 +282,68 @@ pub fn format_output(output: &str, max_size: usize) -> String { | |
) | ||
} | ||
|
||
fn contains_but_not_ends_with(target: &str, pattern: &str) -> bool { | ||
if let Some(pos) = target.find(pattern) { | ||
pos + pattern.len() < target.len() | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
|
||
use super::*; | ||
use crate::cli::agent::ToolSettingTarget; | ||
|
||
#[test] | ||
fn test_contains_but_not_ends_with() { | ||
let test_cases = &[ | ||
// Semicolon tests | ||
("find . -exec cat {} \\; -exec grep pattern {} \\;", ";", true), | ||
("echo hello; echo world", ";", true), | ||
("echo hello", ";", false), | ||
("echo hello;", ";", false), | ||
// Pipe tests | ||
("echo hello | grep world", "|", true), | ||
("echo hello|grep world", "|", true), | ||
("< input.txt wc -l", "<", true), | ||
("echo hello|", "|", false), | ||
// Ampersand tests | ||
("echo hello & echo world", "&", true), | ||
("echo hello&echo world", "&", true), | ||
("&echo hello", "&", true), | ||
("echo hello&", "&", false), | ||
// Greater than tests | ||
("echo hello > file.txt", ">", true), | ||
("echo hello>file.txt", ">", true), | ||
("> out.txt echo hello", ">", true), | ||
("echo hello>", ">", false), | ||
// Less than tests | ||
("cat < input.txt", "<", true), | ||
("cat<input.txt", "<", true), | ||
("cat input.txt<", "<", false), | ||
// Dollar sign tests | ||
("echo $HOME test", "$", true), | ||
("echo test$HOME", "$", true), | ||
("echo test$", "$", false), | ||
("$(date)", "$", true), | ||
("${HOME}", "$", true), | ||
]; | ||
|
||
for (target, pattern, expected) in test_cases { | ||
assert_eq!( | ||
contains_but_not_ends_with(target, pattern), | ||
*expected, | ||
"expected contains_in_middle('{}', '{}') to be {}", | ||
target, | ||
pattern, | ||
expected | ||
); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_requires_acceptance_for_readonly_commands() { | ||
let cmds = &[ | ||
|
@@ -326,6 +395,16 @@ mod tests { | |
), | ||
("find important-dir/ -name '*.txt'", false), | ||
(r#"find / -fprintf "/path/to/file" <data-to-write> -quit"#, true), | ||
// `find` with readonly -exec commands (should be allowed) | ||
("find . -name '*.rs' -exec grep -l pattern {} \\;", false), | ||
("find . -type f -exec cat {} \\;", false), | ||
("find . -type f -exec rm {} \\;", true), | ||
("find . -name '*.txt' -exec head {} \\;", false), | ||
("find . -type f -exec ls -l {} \\;", false), | ||
// Multiple -exec commands - mixed readonly and non-readonly | ||
("find . -exec cat {} \\; -exec rm {} \\;", true), | ||
("find . -exec ls {} \\; -exec touch newfile \\;", true), | ||
("find . -exec grep pattern {} \\; -exec chmod 755 {} \\;", true), | ||
(r"find . -${t}exec touch asdf \{\} +", true), | ||
(r"find . -${t:=exec} touch asdf2 \{\} +", true), | ||
// `grep` command arguments | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
If DANGEROUS_PATTERNS only appears at the end, it is no longer treated as a dangerous command.
Because these commands cannot appear at the end, or if they do, they are not dangerous commands (e.g.,
;
).