diff --git a/crates/core/src/ops.rs b/crates/core/src/ops.rs index 29db9e5e..511b1755 100644 --- a/crates/core/src/ops.rs +++ b/crates/core/src/ops.rs @@ -316,8 +316,10 @@ fn versioned(kind: &'static str, found: Option) -> Failure { fn draft_restore(sheet: &LoadedSheet, input: &str) -> Outcome { let input: DraftRestoreInput = parse_input(input)?; // OD-3: a token-dense text is refused before the parser allocates its tree; the caller - // sets the text aside verbatim, as for any unreadable draft (`invalid`). - if prescan(input.draft.as_bytes()).values > MAX_JSON_VALUES { + // sets the text aside verbatim, as for any unreadable draft (`invalid`). The archive is + // checked too: a stored one was accepted under the budget, so only a crafted one fails. + let texts = [Some(input.draft.as_str()), input.archive.as_deref()]; + if texts.into_iter().flatten().any(over_value_budget) { return Err(Failure::new("invalid", "json value budget exceeded")); } session::restore_draft(sheet, &input.draft, input.archive.as_deref()) @@ -330,6 +332,10 @@ fn draft_restore(sheet: &LoadedSheet, input: &str) -> Outcome { .and_then(|restore| to_value(&restore)) } +fn over_value_budget(text: &str) -> bool { + prescan(text.as_bytes()).values > MAX_JSON_VALUES +} + /// The input is the attachment's JSON as the byte scan read it (any shape). fn archive_import(sheet: &LoadedSheet, input: &str) -> Outcome { session::import_archive(sheet, input) diff --git a/crates/core/src/pdf/json_js.rs b/crates/core/src/pdf/json_js.rs index 2158e20d..df25ec29 100644 --- a/crates/core/src/pdf/json_js.rs +++ b/crates/core/src/pdf/json_js.rs @@ -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, } } diff --git a/crates/core/tests/pdf_json_js.rs b/crates/core/tests/pdf_json_js.rs index 1f2273b2..0d3670e9 100644 --- a/crates/core/tests/pdf_json_js.rs +++ b/crates/core/tests/pdf_json_js.rs @@ -63,6 +63,16 @@ fn prescan_caps_the_depth_at_64() { assert!(prescan(objects.as_bytes()).within_depth); } +#[test] +fn prescan_keeps_counting_values_past_the_depth_cap() { + // A text nested past the cap must still report its size: the rescue copy applies the + // value budget alone (R4.2 security gate). + let deep = format!("{}{}{}", "[".repeat(65), "1,".repeat(1000), "1]".repeat(65)); + let scan = prescan(deep.as_bytes()); + assert!(!scan.within_depth); + assert_eq!(scan.values, 65 + 1001); +} + #[test] fn prescan_skips_strings_and_their_escapes() { let braces = format!("{{\"a\":\"{}\"}}", "{[".repeat(100)); diff --git a/packages/core-wasm/tests/pdf-import.budget.test.ts b/packages/core-wasm/tests/pdf-import.budget.test.ts index bfe96557..aabff2a0 100644 --- a/packages/core-wasm/tests/pdf-import.budget.test.ts +++ b/packages/core-wasm/tests/pdf-import.budget.test.ts @@ -264,6 +264,11 @@ const CASES: Case[] = [ { bytes: deepArrays(16 * MiB - 64, 64), name: 'osteo-report.json' }, ]), }, + { + name: '16 MiB at depth 65 as a rescue copy (security gate: counted past the depth cap)', + path: 'copy', + build: () => deepArrays(16 * MiB - 64, 65), + }, { name: '16 MiB dense numbers as a rescue copy', path: 'copy',