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
16 changes: 16 additions & 0 deletions src/rules/pattern_matcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,22 @@ mod tests {
#[case::flag_negation_empty_tokens_single("sort !-o *", "sort", true)]
#[case::flag_negation_empty_tokens_alt("sort !-o|--output|--compress-program *", "sort", true)]
#[case::flag_negation_empty_tokens_find("find !-delete *", "find", true)]
// Long flag negation with preceding literals and no trailing arguments
#[case::long_flag_negation_empty_after_literals(
"git interpret-trailers --parse !--in-place *",
"git interpret-trailers --parse",
true
)]
#[case::long_flag_negation_with_safe_arg(
"git interpret-trailers --parse !--in-place *",
"git interpret-trailers --parse file.txt",
true
)]
#[case::long_flag_negation_rejects_banned(
"git interpret-trailers --parse !--in-place *",
"git interpret-trailers --parse --in-place",
false
)]
// Positional negation with empty tokens should still be false
#[case::positional_negation_empty_tokens("kubectl !describe *", "kubectl", false)]
fn negation_matching(
Expand Down
9 changes: 9 additions & 0 deletions src/rules/pattern_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ fn should_consume_as_value(next: &LexToken, has_more_after: bool, inside_group:
LexToken::Alternation(alts) if alts.iter().any(|a| is_flag(a)) => false,
LexToken::Placeholder(_) => false,
LexToken::Wildcard => inside_group || has_more_after,
// A negation whose inner value is a flag (e.g., `!--in-place`) should
// not be consumed as a flag value — it is an independent negation token.
LexToken::Negation(s) if is_flag(s) => false,
LexToken::NegationAlternation(alts) if alts.iter().any(|a| is_flag(a)) => false,
_ => true,
}
}
Expand Down Expand Up @@ -538,6 +542,11 @@ mod tests {
value: Box::new(PatternToken::Literal("/".into())),
},
])]
#[case::flag_not_consuming_flag_negation("git --parse !--in-place *", "git", vec![
PatternToken::Alternation(vec!["--parse".into()]),
PatternToken::Negation(Box::new(PatternToken::Literal("--in-place".into()))),
PatternToken::Wildcard,
])]
fn parse_bare_flag(
#[case] input: &str,
#[case] expected_command: &str,
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/config_to_rule_evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,3 +1135,39 @@ fn flag_negation_empty_tokens(
let result = evaluate_command(&config, command, &empty_context).unwrap();
expected(&result.action);
}

// ========================================
// Long flag negation with preceding literal tokens (no trailing arguments)
// ========================================

#[rstest]
#[case::no_trailing_args_allowed(
"git interpret-trailers --parse",
assert_allow as ActionAssertion,
)]
#[case::safe_trailing_arg_allowed(
"git interpret-trailers --parse file.txt",
assert_allow as ActionAssertion,
)]
#[case::banned_flag_rejected(
"git interpret-trailers --parse --in-place",
assert_ask as ActionAssertion,
)]
#[case::banned_flag_with_arg_rejected(
"git interpret-trailers --parse --in-place file.txt",
assert_ask as ActionAssertion,
)]
fn long_flag_negation_with_preceding_literals(
#[case] command: &str,
#[case] expected: ActionAssertion,
empty_context: EvalContext,
) {
let config = parse_config(indoc! {"
rules:
- allow: 'git interpret-trailers --parse !--in-place *'
"})
.unwrap();

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