From 6837de8b32224449ff38707c29882ca4b60cfcc0 Mon Sep 17 00:00:00 2001 From: Mykhailo Chalyi Date: Sat, 7 Nov 2026 14:15:28 +0000 Subject: [PATCH] fix(test): make the python regex DoS wall-clock bounds hang detectors, not budgets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Coverage on main went red at 38d2c4c: regex_redos_patterns_are_bounded failed with "lookbehind: took 4.021935831s" against an `elapsed < 4s` assertion, while the same test configured `max_duration(5s)` and `exit_code` correctly showed no timeout. The two bounds contradicted each other — any run finishing between 4s and 5s failed a test whose own limit permitted it. The VM's `max_duration` is what these tests exercise; the wall clock is only there to catch a run that escaped it entirely, so it has to sit above that limit. `hang_bound(limit) = limit * 3` now derives it, which is the shape regex_loop_is_stopped_by_time_limit already used (6s over a 2s limit). regex_huge_pattern_is_rejected_or_bounded had the same inversion (4s against a 5s limit) and is fixed with it, before it could fail the same way. Not a performance regression: all seven ReDoS cases together take ~0.8s in a normal build. Tarpaulin instrumentation is what multiplies it — the whole integration suite takes ~240s there — and fancy-regex 0.20's reworked lookbehind path was enough to cross a bound that already had no margin. The security assertions are untouched: each case must still not hit the VM time limit (124), and the loop case must still be stopped by it. Claude-Session: https://claude.ai/code/session_01Wy7MByvAMwapenBYpmq9HN --- .../integration/python_security_tests.rs | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/crates/bashkit/tests/integration/python_security_tests.rs b/crates/bashkit/tests/integration/python_security_tests.rs index 6fa404740..27b4abf66 100644 --- a/crates/bashkit/tests/integration/python_security_tests.rs +++ b/crates/bashkit/tests/integration/python_security_tests.rs @@ -1002,6 +1002,21 @@ mod blackbox_dos { // step-capped, and Monty checks its time limit between matches. These // tests are the guard: each classic catastrophic pattern must finish (or // be stopped) well inside the Python time limit. + // A wall-clock assertion in these tests is a hang detector, not a + // performance budget. The VM's own `max_duration` is the thing under test, + // so the wall clock has to sit *above* that limit: a run the limit permits + // must not fail here. Two of them had it inverted -- `elapsed < 4s` against + // a 5s limit -- so any run finishing between 4s and 5s failed while + // `exit_code` correctly showed no timeout. Coverage runs under tarpaulin are + // several times slower than a normal build (the whole integration suite + // takes ~240s there against ~0.8s for all seven cases here), which put the + // `lookbehind` case at 4.02s and turned main red on a bound its own limit + // allowed. `regex_loop_is_stopped_by_time_limit` already used the right + // shape (6s over a 2s limit); this makes it the rule. + fn hang_bound(limit: Duration) -> Duration { + limit * 3 + } + const REDOS_CASES: &[(&str, &str)] = &[ ( "nested quantifier", @@ -1026,8 +1041,9 @@ mod blackbox_dos { #[tokio::test] async fn regex_redos_patterns_are_bounded() { + let limit = Duration::from_secs(5); for (name, call) in REDOS_CASES { - let limits = PythonLimits::default().max_duration(Duration::from_secs(5)); + let limits = PythonLimits::default().max_duration(limit); let mut bash = bash_python_limits(limits); let script = format!("python3 - <<'EOF'\nimport re\nr = {call}\nprint('done')\nEOF"); let started = std::time::Instant::now(); @@ -1037,8 +1053,9 @@ mod blackbox_dos { // a timeout (124) would mean one match ran past the VM's checks. assert_ne!(r.exit_code, 124, "{name}: hit the time limit: {}", r.stderr); assert!( - elapsed < Duration::from_secs(4), - "{name}: took {elapsed:?}: {}", + elapsed < hang_bound(limit), + "{name}: took {elapsed:?}, over the {:?} hang bound: {}", + hang_bound(limit), r.stderr ); } @@ -1048,7 +1065,8 @@ mod blackbox_dos { async fn regex_loop_is_stopped_by_time_limit() { // Many cheap-but-slow matches in a loop: the per-match cap alone does // not bound the script, Monty's between-match time check must. - let limits = PythonLimits::default().max_duration(Duration::from_secs(2)); + let limit = Duration::from_secs(2); + let limits = PythonLimits::default().max_duration(limit); let mut bash = bash_python_limits(limits); let started = std::time::Instant::now(); let r = bash @@ -1058,13 +1076,14 @@ mod blackbox_dos { .await .unwrap(); assert_eq!(r.exit_code, 124, "{}", r.stderr); - assert!(started.elapsed() < Duration::from_secs(6)); + assert!(started.elapsed() < hang_bound(limit), "{}", r.stderr); } #[tokio::test] async fn regex_huge_pattern_is_rejected_or_bounded() { // Compile-time blowup: a large counted repetition. - let limits = PythonLimits::default().max_duration(Duration::from_secs(5)); + let limit = Duration::from_secs(5); + let limits = PythonLimits::default().max_duration(limit); let mut bash = bash_python_limits(limits); let started = std::time::Instant::now(); let r = bash @@ -1072,7 +1091,7 @@ mod blackbox_dos { .await .unwrap(); assert_ne!(r.exit_code, 124, "{}", r.stderr); - assert!(started.elapsed() < Duration::from_secs(4)); + assert!(started.elapsed() < hang_bound(limit), "{}", r.stderr); } #[tokio::test]