Skip to content

Commit 804d1b9

Browse files
committed
detect/uint: make invalid range a failure
By converting the invalid range to a failure instead of just an error, the integer parser won't fall through to the equals parser which is lenient and will parse a range as an equals. Prevents "4<>5", "4-5" and "4-foo" from being parsed as "=4". Ticket: OISF#8028
1 parent a16e87b commit 804d1b9

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

rust/src/detect/uint.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use nom7::branch::alt;
1919
use nom7::bytes::complete::{is_a, tag, tag_no_case, take, take_while};
2020
use nom7::character::complete::{anychar, char, digit1, hex_digit1, i32 as nom_i32};
21-
use nom7::combinator::{all_consuming, map_opt, opt, value, verify};
21+
use nom7::combinator::{all_consuming, cut, map_opt, opt, value, verify};
2222
use nom7::error::{make_error, Error, ErrorKind};
2323
use nom7::Err;
2424
use nom7::IResult;
@@ -585,9 +585,13 @@ pub fn detect_parse_uint_start_interval<T: DetectIntType>(
585585
let (i, _) = opt(is_a(" "))(i)?;
586586
let (i, _) = alt((tag("-"), tag("<>")))(i)?;
587587
let (i, _) = opt(is_a(" "))(i)?;
588-
let (i, arg2) = verify(detect_parse_uint_value, |x| {
588+
589+
// As we've determined this is range, use cut to turn the error
590+
// into a failure so usage inside alt doesn't continue onto the
591+
// next item.
592+
let (i, arg2) = cut(verify(detect_parse_uint_value, |x| {
589593
x > &arg1 && *x - arg1 > T::one()
590-
})(i)?;
594+
}))(i)?;
591595
let mode = if neg.is_some() {
592596
DetectUintMode::DetectUintModeNegRg
593597
} else {
@@ -1043,4 +1047,13 @@ mod tests {
10431047
assert!(detect_parse_uint::<u8>("").is_err());
10441048
assert!(detect_parse_uint::<u8>("<444").is_err());
10451049
}
1050+
1051+
#[test]
1052+
fn test_invalid_range() {
1053+
// Invalid range - should fail (not enough values between bounds)
1054+
assert!(detect_parse_uint_notending::<u8>("1<>2").is_err());
1055+
assert!(detect_parse_uint_notending::<u8>("1-2").is_err());
1056+
assert!(detect_parse_uint_notending::<u8>("1-1").is_err());
1057+
assert!(detect_parse_uint_notending::<u8>("1-foo").is_err());
1058+
}
10461059
}

0 commit comments

Comments
 (0)