fix(domain): OD-3 value count runs past the depth cap; the rescue-copy guard covers the archive text
Security gate finding (R4.2): prescan stopped counting once the depth exceeded 64, so a 16 MiB rescue copy nested 65 deep passed the value budget with 66 values and built its 792 MiB tree. The count now runs to the end (the depth verdict unchanged, the PDF probe unchanged); the class is in the budget suite (48 MiB, refused) and pinned in cargo.
This commit is contained in:
1 parent
1695fbb408
commit
593893a7c2
4 files changed
+31
-12
No files matched your search
@@ -58,8 +58,11 @@ pub struct JsonPrescan {
|
||||
|
||||
/// One linear pass over the raw bytes before any parser sees them: `withinJsonDepth` exactly
|
||||
/// (string literals skipped, a backslash skipping the next byte; `{` and `[` deepen, `}` and
|
||||
/// `]` rise, below zero too; the scan stops as soon as the depth exceeds the cap) and a
|
||||
/// count of the values the parser would allocate, for the OD-3 budget.
|
||||
/// `]` rise, below zero too; the verdict is settled as soon as the depth exceeds the cap)
|
||||
/// and a count of the values the parser would allocate, for the OD-3 budget. The count runs
|
||||
/// to the end whatever the depth verdict: a caller that applies the value budget alone
|
||||
/// (`draft.restore`, whose reader is not depth-capped) must see every value, else a text
|
||||
/// nested past the cap would hide its size (R4.2 security gate finding).
|
||||
///
|
||||
/// The count is an allocation proxy, not a strict JSON value count: every `{`, every `[`,
|
||||
/// every string literal (object keys included, the parser allocates them too), and every
|
||||
@@ -71,26 +74,21 @@ pub struct JsonPrescan {
|
||||
pub fn prescan(data: &[u8]) -> JsonPrescan {
|
||||
let body = data.strip_prefix(BOM).unwrap_or(data);
|
||||
let mut lexer = Lexer::new();
|
||||
let (mut depth, mut values) = (0_i64, 0_u64);
|
||||
let (mut depth, mut values, mut within_depth) = (0_i64, 0_u64, true);
|
||||
for &byte in body {
|
||||
match lexer.next(byte) {
|
||||
Lexeme::Open => {
|
||||
depth += 1;
|
||||
values += 1;
|
||||
within_depth &= depth <= MAX_DEPTH;
|
||||
}
|
||||
Lexeme::Close => depth -= 1,
|
||||
Lexeme::StringStart | Lexeme::NumberStart | Lexeme::LiteralStart => values += 1,
|
||||
Lexeme::InString | Lexeme::Other => {}
|
||||
}
|
||||
if depth > MAX_DEPTH {
|
||||
return JsonPrescan {
|
||||
within_depth: false,
|
||||
values,
|
||||
};
|
||||
}
|
||||
}
|
||||
JsonPrescan {
|
||||
within_depth: true,
|
||||
within_depth,
|
||||
values,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user