Skip to content
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

[BugFix]: Fix panic occurring for regex with incomplete groupings #429

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion guard/src/rules/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,39 @@ fn parse_regex_inner(input: Span) -> IResult<Span, Value> {

regex.push_str(fragment);

for c in regex.chars() {
let mut stack = vec![];
let chars = regex.chars().collect::<Vec<_>>();

for (i, c) in chars.iter().enumerate() {
if c.is_control() {
return Err(nom::Err::Error(ParserError {
context: "Could not parse regular expression".to_string(),
kind: ErrorKind::RegexpMatch,
span: input,
}));
}
if *c == '(' {
if i == 0 || *chars.get(i - 1).unwrap() != '\\' {
stack.push(c);
}
} else if *c == ')'
&& (i == 0 || *chars.get(i - 1).unwrap() != '\\')
&& stack.pop().is_none()
{
return Err(nom::Err::Error(ParserError {
context: "Could not parse regular expression".to_string(),
kind: ErrorKind::RegexpMatch,
span: input,
}));
}
}

if !stack.is_empty() {
return Err(nom::Err::Error(ParserError {
context: "Could not parse regular expression".to_string(),
kind: ErrorKind::RegexpMatch,
span: input,
}));
}

return match Regex::try_from(regex.as_str()) {
Expand Down
18 changes: 18 additions & 0 deletions guard/src/rules/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4623,6 +4623,24 @@ fn test_parse_regex_inner_when_regex_is_valid() {
assert!(parse_regex_inner(valid_cmp).is_ok())
}

#[test]
fn test_parse_regex_when_regex_contains_incomplete_group_structure() {
std::env::set_var("RUST_BACKTRACE", "1");

let invalid = r#"/!w(?()"Kuz>/"#;

let invalid_cmp = unsafe { Span::new_from_raw_offset(invalid.len(), 1, invalid, "") };
assert!(parse_regex(invalid_cmp).is_err());
}

#[test]
fn test_parse_regex_when_regex_contains_complete_group_structure_and_escaped_opening_paren() {
let invalid = r#"/!w\(?()"Kuz>/"#;

let invalid_cmp = unsafe { Span::new_from_raw_offset(invalid.len(), 1, invalid, "") };
assert!(parse_regex(invalid_cmp).is_ok());
}

#[test]
fn test_parse_regex_when_regex_contains_control_characters() {
let invalid = r#"t(/(FF()!t (?(
Expand Down