Skip to content
Merged
Changes from all commits
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
33 changes: 26 additions & 7 deletions crates/bashkit/tests/integration/python_security_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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();
Expand All @@ -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
);
}
Expand All @@ -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
Expand All @@ -1058,21 +1076,22 @@ 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
.exec("python3 -c \"import re\nre.compile('(a{1000}){1000}')\nprint('ok')\"")
.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]
Expand Down
Loading