From c4e717da86fadbdeb4905f4b70425ca64901ca75 Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 23 Oct 2026 16:38:06 +0200 Subject: [PATCH] fix(anatomy): R5.4 drawings projection charges each drawing's fixed parts --- .../2026-10-23-rust-r5-review-security-11.md | 153 ++++++++++++++++++ .../2026-10-23-rust-r5-review-verify.md | 8 +- crates/core/src/curation/draft/pipeline.rs | 9 +- crates/core/tests/curation_review.rs | 23 ++- docs/development/RUST.md | 8 +- 5 files changed, 193 insertions(+), 8 deletions(-) create mode 100644 .claude/audits/2026-10-23-rust-r5-review-security-11.md diff --git a/.claude/audits/2026-10-23-rust-r5-review-security-11.md b/.claude/audits/2026-10-23-rust-r5-review-security-11.md new file mode 100644 index 00000000..8ad3048e --- /dev/null +++ b/.claude/audits/2026-10-23-rust-r5-review-security-11.md @@ -0,0 +1,153 @@ +# Security gate R5.4 (round 11): review files in Rust + +Mode: audit (delta). Repository `/home/bchanot/Documents/faunosteo`, branch +`feature/rust-r5-pipeline-core`, HEAD `411e105`. Scope: +`git diff c6e0d85..HEAD -- crates/core packages/anatomy/tests docs/development/RUST.md` +(29 files, 21 commits). Blind run: no prior verdict or finding list consulted. + +``` +SECURITY — VERDICT: PASS +TOOL: semgrep 1.168.0 — p/security-audit, p/secrets, p/owasp-top-ten +SCOPE: 29 files +BLOCKING: + (none) +REPORTED (non-blocking): + - [LOW] anti-gaming/new-lint-allow — crates/core/tests/curation_markdown.rs:6, crates/core/tests/curation_review.rs:6 + - [LOW] report-guard/bare-url-autolink — crates/core/src/curation/report_guard.rs:119 +PROOF: semgrep 128 rules on 29 files → 0 findings; checklist 14 checks → 2 findings +``` + +## Semgrep + +`semgrep scan --config p/security-audit --config p/secrets --config p/owasp-top-ten +--metrics=off --quiet --json <29 files>`: 0 results, 0 errors, 29 targets scanned, 128 rules +run (most of the 725 loaded rules do not apply to Rust). Ruleset content is fetched at run +time, so the rule count can change on unchanged code. + +## Checklist (14 checks) + +| # | Check | Result | +|---|---|---| +| 1 | Hardcoded secret, token, key, auth-bearing URL (added lines and the two oracle JSON files) | 0 | +| 2 | SQL built by concatenation | 0 (no SQL in the diff; `seed.rs` untouched) | +| 3 | Unsanitized render, `eval`, process spawn | 0 | +| 4 | Sensitive endpoint without authz | n/a (no endpoint, offline CLI and library) | +| 5 | Stack trace or internal path to an end user | 0 (the CLI prints paths to the local operator only) | +| 6 | Secret or PII in a log | 0 | +| 7 | Tracked `.env` or credential file | 0 (only `.env.example`, outside the diff) | +| 8 | Guardrails scanner (`--domain api,supabase`) | 0 FAIL, 1 WARN (`SB-POLICY-NOT-OWNER`, `supabase/migrations/20261008110000_anatomical_sheets.sql:1177`), outside the diff, filtered | +| 9 | New suppressions (anti-gaming) | 2 new lint allows in test files: LOW, see L1 | +| 10 | Markdown structure forging (sections, list items, code spans, links, HTML) | 0 forging path; one LOW note, see L2 | +| 11 | Silent difference from Prettier 3.8.3 on admitted text (differential probe) | 0 | +| 12 | Non-well-formed review SVG | 0 | +| 13 | Resource bounds (records, tables, drawings, quadratic scans) | 0 new | +| 14 | CLI file-system confinement (`--check`, `--out`) | 0 | + +### 10. Report structure forging + +Every authored channel that reaches CHANGELOG.md or REVIEW-REPORT.md was traced: + +- Line breaks: list-item fields (title, rationale, question, citation, URL, DOI, vet-flag + values) refuse any control character (`review_report.rs:364`). Table cells turn `\n` into a + space (`markdown.rs:47`), and the guard refuses `\r` and `\t` (`report_guard.rs:198`). + No authored text starts a line: every template line begins with generated text (`- `, + `N. `, `| `). Line-start constructs (headings, setext underlines, link reference + definitions, footnote definitions, blockquotes, tables) are therefore out of reach. +- Code spans: authored texts refuse backticks (`assert_authored`). Ids are validated at the op + (`ops.rs` `assert_stable_id` on add, nested children and rename). Change ids match + `^CHG-\d{3,}$`. Change reference ids must be declared references (`assert_declared`), whose ids + are stable ids. Field segments are limited to `[A-Za-z0-9_-]` (`fields.rs`) and ids are not + settable. Targets resolve to existing items. The code-span alphabet check + (`[A-Za-z0-9_:/. -]`) and the per-line backtick parity hold as a second layer. +- Inline constructs: links, images, references, HTML tags, autolinks `<…>`, character + references, liquid and padded wiki brackets are refused. Cross-field pairs were checked: + `**` opened in the title and closed in the rationale is refused by `outside_strong`, and `](` + across fields is refused by `rewritten_construct`. +- Invisible or bidi characters (U+202E, U+200B, NBSP) are neither alphanumeric nor in + `PUNCTUATION`: refused. + +### 11. Differential probe against Prettier 3.8.3 + +Probe: `/tmp/claude-1000/-home-bchanot-Documents-faunosteo/9b0edf59-ccd2-4662-b90c-e473dd78028e/scratchpad/diff.mjs` +and `.../diff2.mjs`. A Rust binary calls `report_guard::format_report`, and the repository's +`prettier@3.8.3` runs on the same raw report with the `format.ts` options. Random authored +texts (letters, accents, CJK, every admitted punctuation sign, `_`, `\`, `*`, `**word**`, URL +starts, `&name=`, `&`, `&#`, brackets, list and heading markers) are placed in four +templates: a list-item title and rationale, a table row (cell escape as `cell()` does), a +bibliography citation and URL, and a trailing question. Eight seeds produced 146,000 cases. +44,330 were admitted by the guard and 0 differed from Prettier byte for byte. + +### 12. Review SVG well-formedness + +Drawings are built as trees and printed by `anatomy::svg::serialize`. Attributes escape +`& < > "` and `\t \n \r`, and text escapes `& < >`. `drawing::first_unwritable` refuses any +code point outside the XML 1.0 `Char` production, and any carriage return, in every attribute +and text node before writing. Element and attribute names are fixed. Colours come from the +strict document (`#rrggbb`), and no `href`, `on*` or `style` value is author-controlled. + +### 13. Resource bounds + +- Records: `records::charge` runs before the change and op are cloned. It is called from every + `record` site (set, subtree set, add, remove, move, cite, rename links and citations, B/C + marking). Each record's weight includes the change texts, the op, and the before and after + values. +- Tables: the three CHANGELOG level tables are summed in `assert_table_bound` from the cell + bytes before `table()` joins them. Bytes are never fewer than the display width: the first + width-2 code point is U+1100, which takes 3 bytes. Every other table is checked in `align` + before padding. +- Review drawings: per-annotation projection. A probe binary + (`.../scratchpad/probe/src/bin/drawings.rs`) compared projected and written bytes on hostile + documents built from the equine draft: 40 points with 6,000-character `&` names (ratio + 0.405), `"<&` names (0.262), 6,000-byte `&"<` paths (0.170), unnamed triggers of a long view + name (0.120), equine (0.406). The projection stays above what is written. The projection loop + itself (views × annotations) took 1.2 s on a 7,000-view, 6,000-annotation document, which + the bound then refuses. +- Guard and aligner scans are linear per line. `underscore_pairs_later` only rescans after + authored plain parts, and generated separators hold no `_`. The `&` and `<` splits are + segment-local. +- Already declared and not re-reported: the 65× parse of `changes.json`, the per-op bound walk + (§11.13), and the linear trigger lookup in `--check`. + +### 14. CLI confinement (`crates/core/src/bin/import_equine.rs`) + +Every read is canonicalized and must lie inside the canonical seed folder. Reads accept only +regular files and are capped at 16 MiB. Output file names come from stable view ids, and +`write_exclusive` accepts only `Normal` components and `create_new`. Exercised in the +scratchpad (fresh folders only, nothing in the repository): + +- `--out` into an absent folder wrote 18 files. `diff -r` against the seed matched except + `review/app-main.svg`, the declared R3 difference. +- A second run into the same folder was refused: `not empty, nothing written`. +- A symbolic link was refused with and without a trailing `/`. +- A path holding `..` was refused, and so was an empty path. + +`--check` against the repository reported `legacy-import: 6 files, 0 differences` and +`draft: 12 files, 0 differences` (one `declared:` line), with `git status` clean afterwards. +The `--out` refusal of a destination inside the seed was verified by reading the code and the +unit test. It was not run against the real seed. + +Tests run: `cargo test -p faunosteo-core --features curation --release` on `curation_review` +(11), `curation_markdown` (2), `curation_draft` (18), `curation_format` (7) and the +`import-equine` binary tests (3): all green. + +## Reported findings (non-blocking) + +### L1. [LOW] New lint allows in two new test files + +`crates/core/tests/curation_markdown.rs:6` and `crates/core/tests/curation_review.rs:6` +both add `#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]`. +The lint allowed here is the core's no-I/O and no-clock rule, which `clippy.toml` documents +as allowed locally in tests. The same line heads the ten existing integration test files. +It is a test-harness convention and does not suppress a security scanner (no `nosemgrep`, +`nosec` or eslint-security in the diff), so it does not block. Context only. + +### L2. [LOW] Bare URLs in any authored field render as autolinks + +`report_guard.rs` admits bare URLs (`url_tokens`, line 119; the `&name=` query rule) in every +authored text, not only in the reference `url` and `doi` fields. In a GFM viewer (Gitea), a +URL typed in a title, rationale or question becomes a clickable link. This does not forge or +disguise anything: the visible text is the target, and the output is the TypeScript's byte +for byte. The link cannot swallow generated text either, because every authored field is +followed by a space, ` |`, a line end, or a trailing `,` or `)` that GFM excludes. Owner +choice: keep it, or confine URLs to the bibliography fields if links in rationales are +unwanted. diff --git a/.claude/audits/2026-10-23-rust-r5-review-verify.md b/.claude/audits/2026-10-23-rust-r5-review-verify.md index 02f1a024..95aca340 100644 --- a/.claude/audits/2026-10-23-rust-r5-review-verify.md +++ b/.claude/audits/2026-10-23-rust-r5-review-verify.md @@ -76,6 +76,12 @@ Fresh verifier and security on the fixed commit: below. - Security (`.claude/audits/2026-10-23-rust-r5-review-security-10.md`): HIGH the drawings bound counted the cleaned drawing only: an unnamed trigger copies the opened view's names four times (485 KB changeset → 131 MB `app-main.svg`; 1.2 MB → abort at 3 GB). LOW tables bound per table (three level tables ≈97 MB together); LOW stale `report_guard.rs` header; LOW add/move position anchor not charged; LOW test allows. - Fixed: drawings projected as an upper bound per annotation (`pipeline::review_drawings_bytes`: 1,024 + 24 × (JSON text + longest view name) per annotation of the view, overlay's two documents and the app drawing; equine 1,390,428), test `unnamed_triggers_repeating_a_long_view_name_are_refused_before_rendering`; CHANGELOG tables bound summed over the three level tables (`assert_table_bound` with a running total), number cells 25; position anchor charged (equine records 1,966,163); `report_guard.rs` header; RUST.md counts (draft 4,600 lines, tests 11/2/18/5/3/7), ASCII letter wording, rows rewritten. -## Round 11 +## Round 11 on 411e105: verifier C1 to C8 MET, C9 NOT-MET (2 gaps) + 10 FLOOR (audit prose), security PASS (2 LOW); `pnpm check:all` green 16:02:45 + +- Verifier: own differential 64,000 changesets TS + Prettier vs Rust, 0 silent, 0 raw mismatch; bounds far below their limits on equine and synthetic oracles; adversarial projections held except one. C9: (a) draft line count 4,600 → 4,601; (b) `review_drawings_bytes` 477 bytes under what is written for a change removing every annotation (fixed parts of each drawing, layer group, overlay group and legend, never charged). Obs: test comment cites §11.12 for the emoji refusal (§11.10/§11.14). +- Security (`.claude/audits/2026-10-23-rust-r5-review-security-11.md`): PASS. 146,000 random cases vs Prettier, 44,330 admitted, 0 differ; drawings wrote 0.12 to 0.41 of the projection on hostile documents. LOW test allows (convention); LOW bare URLs autolinked in any authored field (visible text = target, TypeScript alike: owner choice). +- Fixed: 4,096 bytes per drawing for its fixed parts (`pipeline::DRAWING_FIXED_BYTES`; equine projection 1,406,812 for 564,239 written), test `the_drawings_projection_bounds_what_is_written` (equine and no annotations); RUST.md counts (4,606 lines, review 12 tests); test comment sections. + +## Round 12 Fresh verifier and security on the fixed commit: below. diff --git a/crates/core/src/curation/draft/pipeline.rs b/crates/core/src/curation/draft/pipeline.rs index 78cb1228..19126aaf 100644 --- a/crates/core/src/curation/draft/pipeline.rs +++ b/crates/core/src/curation/draft/pipeline.rs @@ -202,7 +202,7 @@ fn view_ids(document: &Value) -> Vec { } /// The review drawings' bytes, projected as an upper bound before any is rendered (the equine -/// draft projects 1.4 MB for 0.56 MB written): every drawing repeats the whole cleaned drawing, +/// draft projects 1.41 MB for 0.56 MB written): every drawing repeats the whole cleaned drawing, /// two per view, and each annotation of a view adds its markup. Views added by a changeset /// multiply the drawing (2,000 views asked for 547 MB, security gate R5.4 round 8); unnamed /// triggers repeat the opened view's names in theirs (a 485 KB changeset wrote a 131 MB @@ -247,6 +247,10 @@ fn longest_view_name(documents: [&Value; 2]) -> Result { Ok(longest) } +/// What a drawing writes besides the cleaned drawing and its annotations: the layer group, the +/// overlay's group and legend (under 1 KB each; a view without annotations carries them alone). +const DRAWING_FIXED_BYTES: usize = 4096; + /// The projected bytes of the review drawings: per view, the overlay (both documents' markers) /// and the application's drawing (the curated layer), each over the cleaned drawing. pub fn review_drawings_bytes(draft: &DraftResult) -> Result { @@ -255,7 +259,8 @@ pub fn review_drawings_bytes(draft: &DraftResult) -> Result usize { + render_draft_artifacts(draft, anomalies) + .unwrap() + .iter() + .filter(|(name, _)| name.starts_with("review/")) + .map(|(_, text)| text.len()) + .sum() + }; + let (result, mut draft) = equine(); + assert!(review_drawings_bytes(&draft).unwrap() >= written(&draft, &result.anomalies)); + draft.document["annotations"] = Value::Array(Vec::new()); + draft.baseline["annotations"] = Value::Array(Vec::new()); + assert!(review_drawings_bytes(&draft).unwrap() >= written(&draft, &result.anomalies)); +} diff --git a/docs/development/RUST.md b/docs/development/RUST.md index 58d9c65d..886f5ad6 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -3101,7 +3101,7 @@ Owner go « Validation R5.3 et autorisation conditionnelle R5.4 » (2026-10-23), fresh verification of the final R5.3 state: met on c6e0d85 (CONFORME 13/13, security PASS with LOWs only, §11.12). Contract `.claude/tasks/contracts/2026-10-23-rust-r5-review.md`. -`crates/core/src/curation/draft` gains 1,692 lines (4,600 in all), plus `curation/report_guard.rs` (258): +`crates/core/src/curation/draft` gains 1,698 lines (4,606 in all), plus `curation/report_guard.rs` (258): | Module | Ports | |---|---| @@ -3116,7 +3116,7 @@ LOWs only, §11.12). Contract `.claude/tasks/contracts/2026-10-23-rust-r5-review | `pipeline.rs` | `render_draft_artifacts` complete, in the TypeScript order: document, assets, sheet-ref, migration map, the overlays and the application's drawings per view, CHANGELOG, REVIEW-REPORT, seed SQL | | `bin/import_equine.rs` | `--check` compares the 6 + 12 files with explicit criteria (byte identity; the application's drawings may fall back on parsed trees under the declared rule, printed as `declared:`); a file missing, not produced or different fails; the pending list and its `shortcut:` are gone; `--out` writes the 18 files | -Parity, every expectation written by the TypeScript (`tests/curation_review.rs`, 11 tests): +Parity, every expectation written by the TypeScript (`tests/curation_review.rs`, 12 tests): - `CHANGELOG.md` (1,114,533 bytes) and `REVIEW-REPORT.md` of the committed draft byte for byte; their raw texts equal the ledger's pre-Prettier digests, the aligned texts its digests; @@ -3205,7 +3205,7 @@ pass the guard unchanged. | a pictograph in a table cell (an emoji, and ©, ®, ™, ✓, ★, ⚠ and the other characters `text::is_emoji_candidate` lists) | measured with Prettier's tables | refused (`UnsupportedWidth`, R5.2): the set is wider than the emoji, the refusal stays loud | | aligned report tables past 32 Mi display columns in all (rows × padded row width, the CHANGELOG's three level tables summed; the equine CHANGELOG's widest table is about 0.5 MB): one long authored cell pads every row of its level, so an 80 KB rationale over 14,000 records asked for 1 GB (security gate R5.4 round 6) | Prettier aligned it (the same amplification) | refused (`Guard`): the CHANGELOG's tables are projected from their cells (bytes bound the display width) before the raw table is joined (test `a_changelog_whose_alignment_would_explode_is_refused_before_it_is_built`, through the pipeline), every other table in the aligner before any padded row is built (test `an_aligned_table_past_the_bound_is_refused_before_it_is_built`), import report included | | records weighing more than 16 Mi units in all (`records::record_weight`, about the bytes in memory: 64 per JSON value and per string, plus one per byte of text, for the values, the change and the op, which each record clones; the equine records weigh 1,966,163; an add or a move also clones its position anchor): a rename or a remove records the whole item and one op, or the B/C marking, records once per item it reaches, so 400 renames of a 5,000-child item asked for 4.3 GB (round 7), a level B add of 10,000 children with a 128 KiB rationale 1.37 GB inside one marking, a 64 KiB value set over a subtree 2.0 GB (round 8), 200,000 one-character reference ids cloned into each record 948 MB at one unit per byte (round 9) | recorded (the same copies) | refused at the record that passes the bound, charged before the change and the op are cloned into it (`records::charge`, tests `the_records_cannot_outgrow_their_bound`, `the_records_bound_holds_inside_one_op_and_the_marking`); the raw report rows, built from the records, are bounded with them; the 200,000-value document bound now holds the baseline (test from a padded baseline) | -| review drawings past 64 MiB in all, projected as an upper bound before any is rendered (`pipeline::review_drawings_bytes`: per view, the cleaned drawing twice, plus for each annotation of the view in the overlay's two documents and in the application's drawing 1,024 bytes and 24 times its JSON text and the longest view name, four copies escaped up to six times longer; the equine draft projects 1,390,428 bytes for about 0.56 MB written): 2,000 views asked for 547 MB (security gate R5.4 round 8), unnamed triggers repeating a long opened view's names 131 MB from a 485 KB changeset (round 10) | written | refused before rendering (`pipeline::MAX_REVIEW_DRAWINGS_BYTES`, tests `review_drawings_past_their_bound_are_refused_before_rendering`, `unnamed_triggers_repeating_a_long_view_name_are_refused_before_rendering`) | +| review drawings past 64 MiB in all, projected as an upper bound before any is rendered (`pipeline::review_drawings_bytes`: per view, the cleaned drawing twice with 4,096 bytes each for the drawing's fixed parts (layer group, overlay group and legend), plus for each annotation of the view in the overlay's two documents and in the application's drawing 1,024 bytes and 24 times its JSON text and the longest view name, four copies escaped up to six times longer; the equine draft projects 1,406,812 bytes for 564,239 written): 2,000 views asked for 547 MB (security gate R5.4 round 8), unnamed triggers repeating a long opened view's names 131 MB from a 485 KB changeset (round 10) | written | refused before rendering (`pipeline::MAX_REVIEW_DRAWINGS_BYTES`, tests `review_drawings_past_their_bound_are_refused_before_rendering`, `unnamed_triggers_repeating_a_long_view_name_are_refused_before_rendering`; the projection bounds what is written, a draft without annotations included: test `the_drawings_projection_bounds_what_is_written`) | | an id an `add` writes (the item's, and those nested in its `children` or `categories`) or a `rename` id that is not a stable id (a region code for a region), or a field segment outside `[A-Za-z0-9_-]` (a control character, NUL included, a backtick, a `]`, a space, a non-ASCII letter) | accepted when a later op removed or renamed the item: the raw id stayed in the records and reached the reports' code spans (a line break there forged a section, security gate R5.4 round 2) | refused at the op, the check the final document applies anyway (test `ids_and_field_segments_that_could_forge_report_structure_are_refused`) | | a label, id or path holding a code point XML 1.0 refuses, or a carriage return, in a review drawing | written (a non-well-formed file; a raw CR read back as a line feed) | refused: the drawing is not written | @@ -3220,7 +3220,7 @@ holds (a 100,000-level file is refused, never a stack overflow). The overlay pri that overflowed as `Infinity`, as `String(number)` did. The renderers are indexed (citation counts in one walk, previous markers and kept ids hashed, subjects grouped per change). -Tests: `cargo test -p faunosteo-core --features curation` (review 11, Markdown 2, draft 18, +Tests: `cargo test -p faunosteo-core --features curation` (review 12, Markdown 2, draft 18, importer 5, binary 3, formatting 7, plus the crate's suites); anatomy 144 (two generators added). ### 11.15 Checkpoint R5.4