Skip to content

Commit 81550b3

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.
1 parent 3ac6bab commit 81550b3

2 files changed

Lines changed: 15 additions & 19 deletions

File tree

rust/src/detect/uint.rs

Lines changed: 12 additions & 15 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 {
@@ -1045,17 +1049,10 @@ mod tests {
10451049
}
10461050

10471051
#[test]
1048-
fn test_null_range() {
1049-
// This is OK and expected.
1050-
let (_, val) = detect_parse_uint_notending::<u8>("1<>3").unwrap();
1051-
assert_eq!(val.arg1, 1);
1052-
assert_eq!(val.arg2, 3);
1053-
assert_eq!(val.mode, DetectUintMode::DetectUintModeRange);
1054-
1055-
// Is this expected?
1056-
let (_, val) = detect_parse_uint_notending::<u8>("1<>2").unwrap();
1057-
assert_eq!(val.arg1, 1);
1058-
assert_eq!(val.arg2, 0);
1059-
assert_eq!(val.mode, DetectUintMode::DetectUintModeEqual);
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());
10601057
}
10611058
}

src/detect-urilen.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,16 +475,15 @@ static int DetectUrilenSetpTest01(void)
475475
Signature *sig = NULL;
476476
DetectEngineCtx *de_ctx = NULL;
477477

478-
res = DetectUrilenInitTest(&de_ctx, &sig, &urilend, "1 <> 2 ");
479-
if (res == 0) {
478+
if (!DetectUrilenInitTest(&de_ctx, &sig, &urilend, "1 <> 3 ")) {
480479
goto end;
481-
}
480+
}
482481

483482
if(urilend == NULL)
484483
goto cleanup;
485484

486485
if (urilend != NULL) {
487-
if (urilend->du16.arg1 == 1 && urilend->du16.arg2 == 2 &&
486+
if (urilend->du16.arg1 == 1 && urilend->du16.arg2 == 3 &&
488487
urilend->du16.mode == DETECT_UINT_RA)
489488
res = 1;
490489
}

0 commit comments

Comments
 (0)