pub(crate) const MAX_PARSE_DEPTH: usize = 64;
pub(crate) fn json_depth_within(payload: &[u8], max: usize) -> bool {
let mut depth: usize = 0;
let mut in_string = false;
let mut escaped = false;
for &b in payload {
if in_string {
if escaped {
escaped = false;
} else if b == b'\\' {
escaped = true;
} else if b == b'"' {
in_string = false;
}
continue;
}
match b {
b'"' => in_string = true,
b'{' | b'[' => {
depth += 1;
if depth > max {
return false;
}
}
b'}' | b']' => depth = depth.saturating_sub(1),
_ => {}
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flat_and_shallow_pass() {
assert!(json_depth_within(br"{}", 64));
assert!(json_depth_within(br#"{"a":1,"b":[1,2,3]}"#, 64));
assert!(json_depth_within(br#"{"a":{"b":{"c":1}}}"#, 64));
}
#[test]
fn exactly_at_bound_passes_one_over_fails() {
assert!(json_depth_within(br"[[[1]]]", 3));
assert!(!json_depth_within(br"[[[[1]]]]", 3));
}
#[test]
fn braces_inside_strings_do_not_count() {
assert!(json_depth_within(br#"{"k":"{{{{{{{{[[[[["}"#, 2));
}
#[test]
fn escaped_quote_keeps_string_open() {
assert!(json_depth_within(br#"{"k":"a\"{{{{{"}"#, 2));
}
#[test]
fn pathological_depth_is_rejected_cheaply() {
let mut deep = vec![b'['; 5000];
deep.extend_from_slice(b"1");
deep.extend(std::iter::repeat_n(b']', 5000));
assert!(!json_depth_within(&deep, MAX_PARSE_DEPTH));
}
}