From 55819fbd321545b481fd40a071c7c445e616d9dc Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 18 Oct 2026 15:44:53 +0200 Subject: [PATCH] feat(domain): R4.2b inflate_flate on miniz_oxide with DecompressionStream verdicts (O9 26/26) Non-wrapping output buffer growing to limit + 1, trailing bytes refused, Adler-32 checked; 12 flate rows of O7 and the browser columns agree. --- crates/core/src/pdf/read/inflate.rs | 73 ++++++++++++++- crates/core/tests/pdf_inflate.rs | 135 +++++++++++++++++++++++++++- 2 files changed, 206 insertions(+), 2 deletions(-) diff --git a/crates/core/src/pdf/read/inflate.rs b/crates/core/src/pdf/read/inflate.rs index d3e39f29..5dc52225 100644 --- a/crates/core/src/pdf/read/inflate.rs +++ b/crates/core/src/pdf/read/inflate.rs @@ -1 +1,72 @@ -//! R4.2b: `inflateFlate` of inflate.ts on miniz_oxide. Written by the R4.2b track. +//! FlateDecode under a cap (port of `inflateFlate`, `packages/pdf/src/attachment/inflate.ts`). +//! +//! The TypeScript fed `DecompressionStream('deflate')` (zlib framing) and answered null on any +//! stream error; Node, Chromium, Firefox and WebKit give the same verdicts (O9, +//! `tests/oracles/r4/inflate.json` and `inflate.browsers.json`): the zlib header is required +//! (CM 8, FCHECK, no FDICT, a window up to 32 KiB), the Adler-32 trailer is required and +//! checked, a truncated stream fails, and the output is refused as soon as it exceeds the +//! limit. +//! +//! The inflater is miniz_oxide's `decompress` over one growing, non-wrapping output buffer. +//! Non-wrapping matters: in that mode a match reaching before the first output byte is +//! refused, as zlib refuses it ("invalid distance too far back"); the wrapping 32 KiB window +//! of the streaming API would read stale window bytes instead. The buffer doubles from +//! `INITIAL_OUTPUT_BYTES` up to `limit + 1` and never beyond, so a bomb stops once that +//! much output exists: the work and the memory spent before the refusal are bounded by the +//! limit, whatever the bomb's expansion. + +use miniz_oxide::inflate::TINFLStatus; +use miniz_oxide::inflate::core::{DecompressorOxide, decompress, inflate_flags}; + +/// First output allocation: a report archive inflates to a few hundred KB, a small payload +/// never pays for the whole limit. +const INITIAL_OUTPUT_BYTES: usize = 64 * 1024; + +/// zlib framing (header parsed, Adler-32 computed and checked) and the whole output kept in +/// one buffer, which is what lets matches be checked against the output start. +const FLAGS: u32 = inflate_flags::TINFL_FLAG_PARSE_ZLIB_HEADER + | inflate_flags::TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; + +/// The FlateDecode (zlib framing) output of `bytes` when it holds at most `limit` bytes. +/// `None` on any failure (empty, truncated or corrupt stream, bad header, missing or wrong +/// Adler-32, trailing bytes) or as soon as the output exceeds `limit`; an output of exactly +/// `limit` bytes is returned. +pub fn inflate_flate(bytes: &[u8], limit: usize) -> Option> { + let cap = limit.saturating_add(1); + let mut out = vec![0; INITIAL_OUTPUT_BYTES.min(cap)]; + let mut decompressor = Box::::default(); + let mut rest = bytes; + let mut written = 0; + loop { + let (status, read, produced) = + decompress(&mut decompressor, rest, &mut out, written, FLAGS); + written += produced; + rest = rest.get(read..)?; + match status { + TINFLStatus::Done => return finish(out, written, rest, limit), + TINFLStatus::HasMoreOutput => grow(&mut out, cap)?, + _ => return None, + } + } +} + +/// The output of a completed stream. DecompressionStream errors on any byte after the stream +/// end (one 0x00, a second stream): a PDF stream holds exactly one zlib stream, and data +/// after it is not part of the attachment. +fn finish(mut out: Vec, written: usize, rest: &[u8], limit: usize) -> Option> { + if !rest.is_empty() || written > limit { + return None; + } + out.truncate(written); + Some(out) +} + +/// Doubles the output buffer up to `cap`; `None` when it is already full at `cap`, that is +/// when the output exceeds the limit. +fn grow(out: &mut Vec, cap: usize) -> Option<()> { + if out.len() >= cap { + return None; + } + out.resize(out.len().saturating_mul(2).min(cap), 0); + Some(()) +} diff --git a/crates/core/tests/pdf_inflate.rs b/crates/core/tests/pdf_inflate.rs index 194024a4..d98adb14 100644 --- a/crates/core/tests/pdf_inflate.rs +++ b/crates/core/tests/pdf_inflate.rs @@ -1 +1,134 @@ -//! R4.2: placeholder, written by its track. +//! O9: `inflate_flate` against the frozen `DecompressionStream('deflate')` verdicts of +//! `inflateFlate` (`tests/oracles/r4/inflate.json`, its browser columns +//! `inflate.browsers.json`) and the `flate` inputs of O7 (`read.json`). +#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] + +mod common; + +use common::r4::{r4_input, r4_oracle, sha256_hex}; +use faunosteo_core::pdf::read::inflate::inflate_flate; +use serde_json::{Value, json}; + +/// The oracle form of an outcome: null, or `{ length, sha256 }` of the output. +fn verdict(output: Option>) -> Value { + output.map_or( + Value::Null, + |bytes| json!({ "length": bytes.len(), "sha256": sha256_hex(&bytes) }), + ) +} + +/// The bytes of an O9 input, checked against the length and digest the oracle recorded. +fn o9_input(oracle: &Value, file: &str) -> Vec { + let bytes = r4_input("inflate", file); + let recorded = oracle["inputs"] + .as_array() + .unwrap() + .iter() + .find(|input| input["file"] == file) + .unwrap_or_else(|| panic!("{file}: not listed in inputs")); + assert_eq!(recorded["length"], json!(bytes.len()), "{file}: length"); + assert_eq!( + recorded["sha256"], + json!(sha256_hex(&bytes)), + "{file}: digest" + ); + bytes +} + +fn mismatch(file: &str, case: &str, limit: u64, expected: &Value, got: &Value) -> String { + format!("{file} ({case}), limit {limit}: node {expected}, rust {got}") +} + +#[test] +fn every_o9_row_matches_node() { + let oracle = r4_oracle("inflate.json"); + let rows = oracle["rows"].as_array().unwrap(); + assert_eq!(rows.len(), 26, "O9 row count"); + let mut failures = Vec::new(); + for row in rows { + let (file, case) = (row["file"].as_str().unwrap(), row["case"].as_str().unwrap()); + let limit = row["limit"].as_u64().unwrap(); + let bytes = o9_input(&oracle, file); + let got = verdict(inflate_flate(&bytes, limit as usize)); + let expected = &row["verdicts"]["node"]; + if got != *expected { + failures.push(mismatch(file, case, limit, expected, &got)); + } + } + assert!( + failures.is_empty(), + "O9 rows differ:\n{}", + failures.join("\n") + ); +} + +#[test] +fn every_o7_flate_input_matches_node() { + let oracle = r4_oracle("read.json"); + let inputs: Vec<&Value> = oracle["inputs"] + .as_array() + .unwrap() + .iter() + .filter(|input| input["kind"] == "flate") + .collect(); + assert_eq!(inputs.len(), 11, "O7 flate input count"); + let mut checked = 0; + let mut failures = Vec::new(); + for input in inputs { + let file = input["file"].as_str().unwrap(); + let bytes = r4_input("read", file); + for outcome in input["outcomes"]["inflateFlate"].as_array().unwrap() { + let limit = outcome["limit"].as_u64().unwrap(); + let got = verdict(inflate_flate(&bytes, limit as usize)); + if got != outcome["verdict"] { + failures.push(mismatch(file, "O7", limit, &outcome["verdict"], &got)); + } + checked += 1; + } + } + assert_eq!(checked, 12, "O7 inflateFlate verdict count"); + assert!( + failures.is_empty(), + "O7 flate rows differ:\n{}", + failures.join("\n") + ); +} + +#[test] +fn every_browser_agrees_with_node_on_every_o9_row() { + let browsers = r4_oracle("inflate.browsers.json"); + let node_rows = r4_oracle("inflate.json")["rows"].clone(); + assert_eq!( + browsers["disagreements"], + json!([]), + "recorded disagreements" + ); + let rows = browsers["rows"].as_array().unwrap(); + assert_eq!( + rows.len(), + node_rows.as_array().unwrap().len(), + "browser row count" + ); + for (row, node_row) in rows.iter().zip(node_rows.as_array().unwrap()) { + let label = format!("{} ({})", row["file"], row["case"]); + assert_eq!(row["file"], node_row["file"], "{label}: file"); + assert_eq!(row["limit"], node_row["limit"], "{label}: limit"); + let node = &node_row["verdicts"]["node"]; + assert_eq!(row["verdicts"]["node"], *node, "{label}: node column"); + for engine in ["chromium", "firefox", "webkit"] { + assert_eq!(row["verdicts"][engine], *node, "{label}: {engine} vs node"); + } + } +} + +/// Not an oracle row: the zlib rule the non-wrapping output buffer exists for. A match whose +/// distance reaches before the first output byte is "invalid distance too far back" for zlib +/// (checked with the system zlib); the Adler-32 is the one of the three zero bytes a decoder +/// reading a zeroed window would produce, so only the distance check can refuse it. +#[test] +fn a_match_before_the_output_start_is_refused() { + let before_start = [0x78, 0x01, 0x03, 0x02, 0x00, 0x00, 0x03, 0x00, 0x01]; + assert_eq!(inflate_flate(&before_start, 1024), None); + let control = [0x78, 0x01, 0x4b, 0x04, 0x02, 0x00, 0x03, 0xce, 0x01, 0x85]; + assert_eq!(inflate_flate(&control, 1024).as_deref(), Some(&b"aaaa"[..])); +}