From b44dd80af2842881897e172e667da994837b300a Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 18 Oct 2026 23:39:31 +0200 Subject: [PATCH] =?UTF-8?q?fix(domain):=20R4.5=20gate=20hardenings:=20self?= =?UTF-8?q?-check=20without=20the=20import=20size=20cap,=20Ref(0)=20safe,?= =?UTF-8?q?=20pdf-write=20implies=20pdf-read;=20=C2=A710.7=20records=20the?= =?UTF-8?q?=20gate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/core/Cargo.toml | 2 +- crates/core/src/pdf/write/export.rs | 15 ++++++++------- crates/core/src/pdf/write/objects.rs | 2 +- docs/development/RUST.md | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 0e2f0d89..790deeb1 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -18,7 +18,7 @@ ts = ["dep:ts-rs"] # facade enables it; the worker never does. pdf-read = ["dep:miniz_oxide"] # The PDF writer (R4, plan §B.1.3): layout to a draw log (R4.4), the serialiser (R4.5). -pdf-write = [] +pdf-write = ["pdf-read"] [dependencies] serde.workspace = true diff --git a/crates/core/src/pdf/write/export.rs b/crates/core/src/pdf/write/export.rs index bc2fe754..2b4f4106 100644 --- a/crates/core/src/pdf/write/export.rs +++ b/crates/core/src/pdf/write/export.rs @@ -12,7 +12,7 @@ use super::serialize::{Document, Metadata, export_date, serialize}; use crate::domain::archive::{EnvelopeFormat, EnvelopeVersion1, Locale, ReportArchiveEnvelope}; use crate::json::to_js_pretty; use crate::pdf::read::MAX_ATTACHMENT_BYTES; -use crate::pdf::read::probe::{ProbeLimits, read_attached_document}; +use crate::pdf::read::probe::{ProbeLimits, locate_attachment}; /// What an export gives back: the file, the envelope text it carries and the warnings. #[derive(Debug, Clone, PartialEq, Eq)] @@ -49,13 +49,14 @@ fn envelope_text(input: &CheckedInput) -> Result { } /// `finish()` re-reads its own output: one scan and one inflation, the attachment's text -/// against the envelope written. +/// against the envelope written. The scan runs without the reader's 50 MiB file cap: a +/// larger export (a report near the caps with real rasters) is a valid file that the import +/// refuses by size, as the TypeScript's did (plan §B.1.2); the export is not where to refuse it. fn self_check(pdf: &[u8], envelope: &str) -> Result<(), ExportError> { - let located = - read_attached_document(pdf, ProbeLimits::PRODUCTION).map_err(|kind| ExportError { - kind: ExportErrorKind::SelfCheckFailed, - detail: kind.as_str().to_owned(), - })?; + let located = locate_attachment(pdf, ProbeLimits::PRODUCTION).map_err(|kind| ExportError { + kind: ExportErrorKind::SelfCheckFailed, + detail: kind.as_str().to_owned(), + })?; if located.text != envelope { return Err(ExportError { kind: ExportErrorKind::SelfCheckFailed, diff --git a/crates/core/src/pdf/write/objects.rs b/crates/core/src/pdf/write/objects.rs index 89921d30..381491b1 100644 --- a/crates/core/src/pdf/write/objects.rs +++ b/crates/core/src/pdf/write/objects.rs @@ -34,7 +34,7 @@ impl Objects { } pub fn set(&mut self, at: Ref, body: Vec) { - if let Some(slot) = self.bodies.get_mut(at.0 - 1) { + if let Some(slot) = at.0.checked_sub(1).and_then(|i| self.bodies.get_mut(i)) { *slot = body; } } diff --git a/docs/development/RUST.md b/docs/development/RUST.md index e2645a53..cefdbb4b 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -1581,6 +1581,22 @@ shipped module, inside the plan's estimate (+100 to 150 KB) and under 600 KB; above 550 KB it is to be reported, so R4.6 measures the shipped module again once the session links the writer. +Independent security gate (R4.5): PASS, 4 LOW, no blocker. Its probe ran +131,648 mutated or synthetic PNGs through the gate and the decoder (0 panic, +0 overflow, slowest 198 ms), 6.7 million rounds over the base64, date and +number helpers, and hostile exports: report names holding PDF syntax reach +the file as hex only (0 raw occurrence, `qpdf --check` clean, every `/Length` +and xref offset exact), hostile URLs and `exportedAt` refused. Applied: the +self-check scans without the reader's 50 MiB file cap (an export above it is +a valid file the import refuses by size, as before); `Ref(0)` cannot +underflow; `pdf-write` implies `pdf-read`. Reported: the self-check proves +the attachment and the reader's read only (poppler covers the rendering of +the corpus); the external-tools test skips on a host without qpdf and +poppler (it ran here); the one-shot path holds each PNG twice and the file +twice at the end (R4.6 input, OD-2); a view raster whose header reads but +whose body fails to decode fails the export as `self_check_failed` where R4.6 +makes it a per-key `view_raster_failed` warning. + Residual risks and open points (R4.5): - The one-shot export of L with real rasters is slow (5 s native, dominated