Files
faunosteo/.claude/audits/2026-10-25-rust-r5-6-rehearsal-security.md

12 KiB

Security audit: R5.6 fingerprint switch rehearsal (worktree 943a25a + r5-6-switch.patch, seed switched)

  • date: 2026-10-25 | mode: audit (delta), fresh run | contract .claude/tasks/contracts/2026-10-25-rust-r5-6-rehearsal.md (S1-S8)
  • scope: git -C <worktree> diff HEAD, 28 files (9 Rust sources, Cargo.toml, 8 Rust test files and 2 fixtures, 5 TypeScript test files, package.json, rust-check.sh, .prettierignore, the two switched seed files). Worktree: /tmp/claude-1000/-home-bchanot-Documents-faunosteo/3571c861-cd6e-4751-a25e-c26da95beb85/scratchpad/r56wt. git apply --check -R of the stored patch is clean there, so the worktree is exactly the patch.
  • isolation (S7) checked: main tree at 6ade917, seed sheet-ref.json still 27c7…, fingerprint.rs still "PREPARED, NOT ACTIVE", the main-tree diff since 943a25a touches only .claude/ and RUST.md.
  • untouched, checked file by file: domain/archive/* (incl. read.rs), domain/session.rs, domain/report/read.rs, pdf/read/*, pdf/import.rs, pdf/json_js.rs, packages/core-wasm/tests/n-1, crates/core/tests/oracles, apps/app/tests/fixtures, tests/pdf/LEDGER.tsv, Cargo.lock, deny.toml. Seed document.json, horse.svg, equine_draft.sql identical to HEAD; sha256sum document.json = 27c7383c…2660 (the registered alias).
  • hermetic runs in the worktree (CARGO_TARGET_DIR=<worktree>/target): --features cli r5_6_switch 8, sheet_fingerprint 10, pdf_compare 16, pdf_import 4; --features curation curation_draft 20, curation_review 12; vitest app r5-6.switch 2, core-wasm r5-6.n-1 1 (resolves the worktree's packages/core-wasm). All green.
  • not done: no service key, no Supabase, no transfer tool, no delete, no code edited, no pnpm format. The only network access was semgrep fetching its three pinned packs (metrics off).

Focus answers

Forged, unknown, foreign-sheet or unverified stamp counted as the same document? Not in the readers. equivalence (fingerprint.rs:63-75) admits only the registry pair, one way (alias stamped, canonical current), bound to the sheet id. A forged 64-hex stamp is Different and rebases (r5_6_switch.rs:169-179); a case-changed one is refused by the v3 contract; a foreign sheet id matches no entry and fails the sheet-id checks (rebase.rs:43-48, input.rs:212). check_reference (loaded.rs:161-180) recomputes v1 from the same typed document the index is built from, so fingerprint_verified implies the loaded content is the registered content. The pair itself is bound to one content: sha256 of the bytes is the alias, v1 of the same bytes is the canonical value (curation_draft the_document_fingerprint_is_v1_and_its_bytes_keep_the_historical_alias, plus my sha256sum). Rebase and report:compare grant the alias only on a verified sheet (rebase.rs:43-48, lines.rs:356). The export check is the exception: see M1.

Does M1 write a snapshot that is not the trusted document, or loosen archive checks? Through the application, no: export.ts:146 always builds the snapshot with archive.snapshot from the loaded sheet, and an old stamp only survives to export when that sheet was verified (otherwise rebase moved the report first). archive/read.rs:116-119 is unchanged and strict; the mixed envelope is still rejected (r5_6_switch.rs:185-188). At the core boundary the binding is not enforced (M1).

Can a v0 reference gain alias privilege? At load, no: no block means fingerprint_verified = false, and a v0 reference carrying the canonical value still rebases an old report (r5_6_switch.rs:255-263). At export, yes, if a caller pairs an old report with a snapshot cut from such a sheet (M1).

Do the declared test rules hide a content difference? No. Each rule is targeted and fails loudly on anything else. curation_draft r5_6_rule asserts the pre-switch value is the alias, moves only that value, adds the block, then compares whole bytes through the generator's formatter; the pre-switch copies are pinned to the frozen R5.1 ledger (sha and length). pdf_compare r5_6_rule rewrites only the "matches the bundled sheet" line of non-migrated blocks into the alias line; a !! line or any other drift still mismatches, and the alias count is pinned (77). O7, O10, the R2 Zod oracle, the R5.1 ledger and seed.golden read the pre-switch files; their post-switch twins (o7_after_r5_6_…, o10_on_the_switched_seed_…, the committed migration map after R5.6) allow the stamp alone to move. migratedBeforeR56 changes only report.sheet.documentSha256; observations still go through the switched map, so a mapping change would break the byte goldens.

Wasm sha2 and the load-time fingerprint: cost or DoS? The hasher is reached only from loaded.rs:175 and the pipeline (pipeline.rs:174). Archive snapshots and imported files are never fingerprinted, and the bundle is build-time content. The cost is one validation, one JCS and one SHA-256 of a 296 KB document per sheet handle (page plus each import worker): the 8-test switch suite with its dozen loads runs in 0.46 s in a debug build. sha2 0.11.1 was already locked; the worker keeps its own dependency. No attacker-driven DoS. Two LOWs below (L4, L5).

Findings

M1 [MEDIUM] The export grants the alias on the snapshot's word, with no verified sheet behind it

crates/core/src/pdf/write/input.rs:205-213 (assert_exportable) and :238-248 (keep_the_report_stamp) call equivalence with the snapshot's own reference.document_sha256 as "current". The export never sees a LoadedSheet, so fingerprint_verified is not consulted. The snapshot document is a lean cut and cannot be re-hashed. As a result:

  • export_from_envelope (pdf/write/export.rs:117-136, cargo only) took a mixed envelope (report 27c7…, snapshot 12d7…, any lean document) as sheet_mismatch before the patch. It now accepts it and writes the snapshot under 27c7…, a self-consistent envelope that the strict reader keeps. The writer normalises what the reader rejects.
  • A snapshot cut from an unverified v0 reference that carries the canonical value, exported with an old report, gets the alias that rebase and report:compare deny for the same sheet.
  • packages/core-wasm/tests/r5-6.n-1.test.ts:46-49 feeds a hand-edited snapshot and the core accepts it, which shows there is no provenance check.
  • RUST.md §11.20 (main tree, lines 3538-3545) says M1 "needs the loaded sheet's fingerprint_verified". At this site it does not. Contract S1's "with the loaded sheet's v1 value recomputed … at load" holds only as an application-flow precondition.

Not exploitable today. The only shipped caller builds the snapshot from the verified loaded sheet. Archives are unsigned, so anyone can already write a 27c7/27c7 envelope around arbitrary content: no capability is gained. This is a trust-boundary gap, not a vulnerability. Hint: make the M1 decision where verification is known. build_sheet_snapshot / archive.snapshot can stamp the report's value when sheet.fingerprint_verified and the pair is TrustedAlias, and assert_exportable goes back to strict equality (that also keeps export_from_envelope strict). This needs an S2 scope amendment (snapshot_rules.rs). Otherwise, state the precondition at input.rs:232-237 and correct RUST.md §11.20.

L1 [LOW] "fingerprint": null reads as no block (v0)

crates/core/src/domain/sheet/loaded.rs:38-39: #[serde(default)] Option<…> maps null to None. A present but null block is not refused, against S3's "a present block is strict". It fails closed: no privilege, old reports rebase. Hint: refuse an explicit null, for example with a custom deserializer.

L2 [LOW] BundledRef accepts unknown keys, so a misspelt block loads as v0

loaded.rs:30-40: DeclaredFingerprint is strict, but its parent is not. A typo such as fingerprints silently yields an unverified sheet. This fails closed, and the struct shape predates the patch. Hint: deny_unknown_fields on BundledRef, if every bundle producer (vite plugin, report_compare SeedRef, tests) emits only known keys.

L3 [LOW] Verification state and verified value are public mutable fields

loaded.rs:53 (reference) and :66 (fingerprint_verified) are pub. Crate code holding &mut LoadedSheet could change one without the other. No such writer exists today. Hint: private fields with getters.

L4 [LOW] A warning the drafts allow makes a v1 bundle unloadable and the generator fail

fingerprint.rs:228-231 refuses any issue, warnings included. validate.rs:366-380 (annotation.no_findings, documented "allowed in a draft") and :440-455 (finding_zone.unreferenced) are warnings. Since the patch, loaded.rs:175-179 turns that into invalid_sheet at load, and pipeline.rs:174-175 refuses to render the draft. The behaviour fails closed and follows the frozen v1 rule, but a content-only curation step can now take the sheet out of the application. Hint: owner decision. Either keep it (and document it next to the curation workflow) or have v1 refuse errors only, which would need a new convention version.

L5 [LOW] canonical_json recursion has no depth bound

fingerprint.rs:129-170 recurses on the typed document. Only the build-time bundle reaches it today. Hint: bound the depth, or reuse the import prescan caps, before any user- or database-supplied sheet goes through from_bundle (Step 13).

L6 [LOW, context] New test file carries the standard clippy allow

crates/core/tests/r5_6_switch.rs:8 has #![allow(clippy::disallowed_methods, clippy::unwrap_used, …)], the same header as all 40 integration tests. It is test-only, not shipped, and not a security-rule suppression (no nosemgrep, nosec or eslint-disable in the diff). Not blocking.

Out of scope, filtered: guardrails WARN SB-POLICY-NOT-OWNER at supabase/migrations/20261008110000_anatomical_sheets.sql:1177 (not in the diff, unchanged).

Verdict block

SECURITY — VERDICT: PASS
TOOL: semgrep 1.168.0 — p/security-audit, p/secrets, p/owasp-top-ten
SCOPE: 28 files
BLOCKING:
  (none)
REPORTED (non-blocking):
  - [MEDIUM] M1 export grants the trusted alias on the snapshot's unverified reference; export_from_envelope now normalises a mixed envelope; RUST.md §11.20 claims a fingerprint_verified guard absent at this site — crates/core/src/pdf/write/input.rs:205-248, crates/core/src/pdf/write/export.rs:117-136
  - [LOW] L1 "fingerprint": null read as v0 instead of refused — crates/core/src/domain/sheet/loaded.rs:38-39
  - [LOW] L2 BundledRef accepts unknown keys, misspelt block loads as v0 — crates/core/src/domain/sheet/loaded.rs:30-40
  - [LOW] L3 reference and fingerprint_verified public mutable fields — crates/core/src/domain/sheet/loaded.rs:53,66
  - [LOW] L4 draft-allowed warning makes a v1 bundle unloadable and the generator fail — crates/core/src/domain/sheet/fingerprint.rs:228-231, crates/core/src/domain/sheet/loaded.rs:175-179
  - [LOW] L5 canonical_json recursion unbounded (trusted input only today) — crates/core/src/domain/sheet/fingerprint.rs:129-170
  - [LOW] L6 standard clippy allow in new test file, not a security suppression — crates/core/tests/r5_6_switch.rs:8
PROOF: semgrep 163 rules on 28 files → 0 findings (0 errors); checklist 9 checks (secrets, SQL concat, unsafe render, authz, error exposure, secret in log, tracked .env, guardrails 0 FAIL / 1 WARN out of diff, new suppressions) → 1 finding (L6); targeted review 5 focus areas → 1 MEDIUM, 5 LOW; worktree runs: cargo r5_6_switch 8, sheet_fingerprint 10, pdf_compare 16, pdf_import 4, curation_draft 20, curation_review 12, vitest r5-6.switch 2, r5-6.n-1 1, all green