Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/rules/pattern_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,33 @@ fn optional_flags_absent(optional_tokens: &[PatternToken], cmd_tokens: &[&str])
fn literal_matches(pattern: &str, token: &str) -> bool {
if pattern.contains('*') {
glob_match(pattern, token)
} else if pattern.contains('\\') {
// Strip backslash escapes so that pattern `\;` matches command token `;`.
// The pattern lexer preserves backslash-escaped characters as-is (e.g. `\;`),
// while the command tokenizer resolves them (e.g. `\;` -> `;`).
let unescaped: String = unescape_backslashes(pattern);
unescaped == token
} else {
pattern == token
}
}

/// Remove backslash escapes: `\;` -> `;`, `\\` -> `\`, etc.
fn unescape_backslashes(s: &str) -> String {
let mut result = String::with_capacity(s.len());
let mut chars = s.chars();
while let Some(ch) = chars.next() {
if ch == '\\' {
if let Some(next) = chars.next() {
result.push(next);
}
} else {
result.push(ch);
}
}
result
}

/// Simple glob matching where `*` matches zero or more arbitrary characters.
///
/// Only supports `*` as a wildcard; no other glob syntax (e.g. `?`, `[...]`)
Expand Down
27 changes: 18 additions & 9 deletions src/rules/pattern_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ fn should_consume_as_value(next: &LexToken, has_more_after: bool, inside_group:
LexToken::Literal(s) if s == "]" => false,
LexToken::Literal(s) if is_flag(s) => false,
LexToken::Alternation(alts) if alts.iter().any(|a| is_flag(a)) => false,
LexToken::Placeholder(_) => false,
LexToken::Wildcard => inside_group || has_more_after,
_ => true,
}
Expand Down Expand Up @@ -496,17 +497,13 @@ mod tests {
PatternToken::Alternation(vec!["-f".into(), "--force".into()]),
PatternToken::Wildcard,
])]
#[case::placeholder_value("cmd -o|--option <cmd>", "cmd", vec![
PatternToken::FlagWithValue {
aliases: vec!["-o".into(), "--option".into()],
value: Box::new(PatternToken::Placeholder("cmd".into())),
},
#[case::placeholder_not_consumed_as_flag_value("cmd -o|--option <cmd>", "cmd", vec![
PatternToken::Alternation(vec!["-o".into(), "--option".into()]),
PatternToken::Placeholder("cmd".into()),
])]
#[case::path_ref_value("cmd -c|--config <path:config>", "cmd", vec![
PatternToken::FlagWithValue {
aliases: vec!["-c".into(), "--config".into()],
value: Box::new(PatternToken::PathRef("config".into())),
},
PatternToken::Alternation(vec!["-c".into(), "--config".into()]),
PatternToken::PathRef("config".into()),
])]
fn parse_flag_with_value(
#[case] input: &str,
Expand Down Expand Up @@ -641,6 +638,18 @@ mod tests {
#[case::path_ref("cat <path:sensitive>", "cat", vec![
PatternToken::PathRef("sensitive".into()),
])]
#[case::flag_alternation_then_placeholder(
r"find * -exec|-execdir|-ok|-okdir <cmd> \;|+",
"find",
vec![
PatternToken::Wildcard,
PatternToken::Alternation(vec![
"-exec".into(), "-execdir".into(), "-ok".into(), "-okdir".into(),
]),
PatternToken::Placeholder("cmd".into()),
PatternToken::Alternation(vec![r"\;".into(), "+".into()]),
],
)]
fn parse_placeholder(
#[case] input: &str,
#[case] expected_command: &str,
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/wrapper_recursive_evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,35 @@ fn wrapper_compound_with_sandbox(empty_context: EvalContext) {
assert_eq!(result.action, Action::Allow);
assert_eq!(result.sandbox_preset.as_deref(), Some("py_sandbox"));
}

// ========================================
// find -exec/-execdir wrapper: flag alternation followed by <cmd>
// placeholder is parsed correctly, enabling recursive evaluation
// ========================================

#[rstest]
#[case::find_exec_rm_denied_semicolon("find . -exec rm -rf / \\;", assert_deny as ActionAssertion)]
#[case::find_exec_rm_denied_plus("find . -exec rm -rf / +", assert_deny as ActionAssertion)]
#[case::find_execdir_echo_allowed("find . -execdir echo hello +", assert_allow as ActionAssertion)]
#[case::find_ok_rm_denied("find /tmp -ok rm -rf / \\;", assert_deny as ActionAssertion)]
#[case::find_okdir_ls_allowed("find . -okdir ls -la +", assert_allow as ActionAssertion)]
#[case::find_exec_unmatched_default("find . -exec hg status +", assert_default as ActionAssertion)]
fn find_exec_wrapper_evaluates_inner(
#[case] command: &str,
#[case] expected: ActionAssertion,
empty_context: EvalContext,
) {
let config = parse_config(indoc! {"
rules:
- deny: 'rm -rf *'
- allow: 'echo *'
- allow: 'ls *'
definitions:
wrappers:
- 'find * -exec|-execdir|-ok|-okdir <cmd> \\;|+'
"})
.unwrap();

let result = evaluate_command(&config, command, &empty_context).unwrap();
expected(&result.action);
}