Files
faunosteo/.claude/audits/2026-10-23-rust-r5-draft-security-final.md

16 KiB

SECURITY — VERDICT: PASS
TOOL: semgrep 1.168.0 — p/security-audit, p/secrets, p/owasp-top-ten
SCOPE: 31 files
BLOCKING:
  (none)
REPORTED (non-blocking):
  - [MEDIUM] unbounded-nesting-stack-overflow — crates/core/src/curation/draft/fields.rs:111
  - [MEDIUM] out-writes-not-exclusive-toctou — crates/core/src/bin/import_equine.rs:232
  - [LOW] reserved-nul-key-bypass-via-field-path — crates/core/src/curation/draft/fields.rs:83
  - [LOW] out-dotdot-and-parent-creation — crates/core/src/bin/import_equine.rs:192
  - [LOW] out-inside-seed-not-refused — crates/core/src/bin/import_equine.rs:186
  - [LOW] init-cwd-base-resolution — crates/core/src/bin/import_equine.rs:223
  - [LOW] args-non-utf8-panic — crates/core/src/bin/import_equine.rs:282
  - [LOW] core-no-io-lint-partial (pre-existing, context) — clippy.toml:4
  - [LOW] suppressions (context) — crates/core/tests/curation_draft.rs:7 (new), crates/core/src/bin/import_equine.rs:15 (pre-existing)
PROOF: semgrep 163 rules on 31 files → 0 findings; checklist 18 checks → 9 findings (0 CRITICAL, 0 HIGH, 2 MEDIUM, 7 LOW)

Security gate, fresh run, 2026-10-23. Scope: git diff 5e114da..HEAD (HEAD 8654905, branch feature/rust-r5-pipeline-core), 31 changed files, all present. Product files read whole: crates/core/src/curation/draft/*.rs (11), crates/core/src/bin/import_equine.rs, crates/core/src/curation/mod.rs, crates/core/Cargo.toml, crates/core/tests/curation_draft.rs, packages/anatomy/tests/r5.curation-synthetic-oracles.test.ts, scripts/import-equine.sh, package.json. Docs and .claude files grepped for secrets only.

Semgrep's packs carry few Rust rules, so the Rust review below is manual. Two behaviours were confirmed by a scratch probe (a throwaway binary in the session scratchpad linking faunosteo-core with curation, run on the mini baseline of oracles/r5/curation-synthetic.json; nothing in the repository was touched). import-equine --out was never run: its behaviour is traced by reading. import-equine --check was run as the brief allows: legacy-import: 6 files, 0 differences, draft: 6 files, 0 differences, 6 pending R5.4 files listed, exit 0, git status clean afterwards.

Checklist

# Check Result
1 Hardcoded secret, token, key, auth URL (diff incl. docs, .claude, oracle JSON) none
2 SQL by interpolation (seed.rs) none, see note A
3 Unsanitized render / eval n/a (no web output; the TS test has no eval)
4 Sensitive endpoint without authz n/a (no endpoint; seed SQL design pre-existing)
5 Stack trace / internal path to an end user none (dev CLI, stderr to the developer)
6 Secret / PII in a log none
7 Tracked .env / credential file none in the diff (repo tracks .env.example only)
8 Guardrails scanner (api,supabase) 0 FAIL; 1 WARN SB-POLICY-NOT-OWNER in supabase/migrations/20261008110000_anatomical_sheets.sql:1177, outside the diff, dropped (gate rule)
9 Panics from changes.json / references.json: indexing, unwrap, byte slicing none, see note B
10 Unbounded recursion, depth and resource caps MEDIUM-1
11 Reserved NUL-key refusal LOW-1
12 --out claim: symlinks, trailing /, . / .., TOCTOU MEDIUM-2, LOW-2, LOW-3
13 INIT_CWD LOW-4
14 --check writes nothing verified (run + read: no write call on that path)
15 Generator writes outside crates/core/tests/oracles/r5/ none: one constant path, curation-synthetic.json, only under MAKE_R5_ORACLES=1
16 Core has no fs / env / clock access holds by inspection; lint partial, LOW-6
17 CLI argument handling LOW-5
18 New suppressions (anti-gaming) LOW-7 (context, non-blocking)

Findings

MEDIUM-1: nesting built by ops is unbounded, a small changes.json aborts the tool

  • Files: crates/core/src/curation/draft/fields.rs:83-89 (split_field, no segment cap) and :111-126 (write_field creates one object per segment); first recursive walker hit crates/core/src/curation/draft/records.rs:114-123 (strip_marks, called at apply.rs:114); same class in records.rs:88-105 (touch_tree), targets.rs:298-322 (walk), apply.rs:129 (doc.clone() + from_value), and the recursive Value drop.
  • serde_json's 128-level parse limit (no unbounded_depth feature) caps each authored value, but the ops build depth past it. A set whose field is name followed by N .a segments creates N nested objects in one op.
  • Probe, release build (the profile scripts/import-equine.sh uses): N = 10,000 is refused cleanly (curated document is not shape-valid: unknown field 'a'). N = 20,000 (a ~40 KB changes.json) gives thread 'main' has overflowed its stack, fatal runtime error: stack overflow, aborting, exit 134. The debug build (cargo run without --release) has larger frames and a lower threshold.
  • The finding tree has the same problem, by analysis only (not run): repeated add ops (each up to ~63 finding levels under the parse limit) or move chains deepen the tree without limit. walk clones the growing path for every entry, so memory grows with depth² before the stack overflows.
  • Impact: the process aborts where it should give a typed refusal. It still fails closed: the pipeline (folders()) runs before claim_destination, so no folder is created and nothing is written. The input is a committed, owner-authored file. Robustness of a dev tool, and a gap in the "depth and resource caps kept" requirement. Not exploitable beyond a crash.
  • Fix: cap split_field segments, refusing beyond the deepest settable path (name.fr, count_range.min, geometry.cx: 2 segments, so a cap of 4 is generous). Cap the finding tree depth with an iterative check after each add and move, e.g. a MAX_FINDING_DEPTH constant that refuses with the change id and op index.

MEDIUM-2: --out writes are not exclusive, so the claim can be raced

  • File: crates/core/src/bin/import_equine.rs:232-247 (write: fs::create_dir_all at :239, fs::write at :242), together with :186-219 (claim_destination).
  • The claim checks the folder once: absent then mkdir, or existing and empty. The writes then use create_dir_all, which follows an existing symlink, and fs::write, which is O_CREAT | O_TRUNC and follows symlinks. Anything planted between the claim and the writes is followed or truncated. That could be <out>/draft turned into a symlink to the real seed draft/, or a file created early. The claimed path could also be swapped for a symlink before canonicalize (:218). An existing empty folder is accepted whatever its owner and mode. The cross-user vector is an empty, world-writable folder pre-created by another account in a shared location.
  • The doc comment at :182-185 says "created here, so nothing can appear between the check and the writes". That holds against a symlink at the final component (mkdir gives EEXIST), but not against entries created inside the folder afterwards.
  • Impact: a concurrent same-user process, or a hostile local account on a multi-user host, can redirect or overwrite files. On the single-user dev machine this needs an attacker who already has the user's rights. It is still an overstatement of the "never overwrites" guarantee the owner asked for, and the std-only fix is cheap.
  • Fix: create every subfolder under the root with fs::create_dir, never _all (fails on any existing entry and does not follow a final symlink). Write every file with OpenOptions::new().write(true).create_new(true) (O_EXCL: refuses an existing file or symlink). Optionally refuse an existing claimed folder that is not owned by the current uid or is group/world-writable (std::os::unix::fs::MetadataExt). Correct the comment.

LOW-1: the reserved NUL-key refusal does not cover dotted field segments

  • Files: crates/core/src/curation/draft/schema.rs:389-410 (reserved_key scans JSON object keys only), fields.rs:83-89 (split_field accepts any non-empty segment), records.rs:16 (TOUCH_MARK = "\u{0}touched"), records.rs:114-123 (strip_marks removes that key at every level).
  • Probe: {"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.\u0000touched","value":"author text"} is accepted. The document's name stays {"fr":"mobility fr","en":"mobility en"} and a record field="name.\0touched" after="author text" is still emitted. The authored value is dropped silently while the trace (the R5.4 CHANGELOG) claims the write. Any other NUL-prefixed segment, e.g. name.\u0000other, is refused by the strict shape gate. The TypeScript kept the key and refused at that gate.
  • No review-rule bypass: the first segment must be settable, so the mark cannot be set at item level. Every authored JSON key holding the mark is refused at parse.
  • Fix: refuse a field segment that starts with \0 (in split_field or the SetOp parse), with the same "reserved key" message.
  • File: crates/core/src/bin/import_equine.rs:192-200.
  • Rebuilding the path from components() drops a trailing / and inner . components, which is correct: a final symlink is then refused by symlink_metadata, dangling links included. .. is kept, though, and the kernel resolves it after any symlinked component. --out link/../new creates <parent of link target>/new, not the lexical ./new. --out link/.. is always refused (the parent of the target is never empty).
  • In the absent branch, create_dir_all(parent) runs before the final create_dir. A claim that then fails, e.g. --out link/new/.. (mkdir .. gives EEXIST), leaves <link target>/new behind, created through the symlink, while the tool prints "nothing written".
  • Parent symlinks are followed by design (doc comment), but the resolved destination is printed only after the files are written.
  • Fix: refuse any Component::ParentDir. Create only the final component, refusing a missing parent, or document the parent creation. Print the canonical destination before the first write.

LOW-3: a destination inside the official seed tree is not refused

  • File: crates/core/src/bin/import_equine.rs:186-219.
  • --out supabase/seed/anatomical-sheets/equine/x passes the claim, being absent, and writes x/legacy-import/... and x/draft/... inside the seed. Committed files are never overwritten, and the populated folders (legacy-import/, draft/) are refused as not empty. The owner requirement "never modifies the official seed automatically" holds, since this needs an explicitly typed path. But generated folders can land untracked in the seed, and --check then reports them as "not produced".
  • Fix: refuse when the canonical destination is under fs::canonicalize(SEED), or under the repository root.

LOW-4: INIT_CWD and the base of a relative --out

  • Files: crates/core/src/bin/import_equine.rs:223-229, scripts/import-equine.sh:12.
  • Any absolute INIT_CWD in the environment is trusted, including one inherited or left stale by an outer pnpm. Without pnpm, the script cds to the repository root, so a relative --out from bash scripts/import-equine.sh resolves against the repository root, not the caller's folder. The doc says "else the current one". If current_dir() fails, the base becomes empty and the path stays relative to a cwd that no longer exists, which fails harmlessly. Not dangerous with the claim rules (new or empty folder only), but surprising.
  • Fix: print the resolved destination before claiming, and state in the doc that direct runs resolve from the repository root. Or refuse a relative --out when INIT_CWD is unset.

LOW-5: a non-UTF-8 argument panics

  • File: crates/core/src/bin/import_equine.rs:282 (env::args() panics on non-Unicode argv). Self-inflicted, CLI only, not reachable from changes.json.
  • Fix: env::args_os() and a usage error on a non-UTF-8 argument.

LOW-6 (pre-existing, context): the core's no-I/O guarantee is only partly lint-enforced

  • File: clippy.toml:4-14 (not in this diff). disallowed-methods lists fs::read, read_to_string, write, File::open, File::create, env::var, var_os and the clocks. It does not list fs::create_dir(_all), remove_file, remove_dir(_all), read_dir, rename, copy, metadata, symlink_metadata, canonicalize, OpenOptions::open, env::current_dir, env::args(_os) or process::Command::new.
  • A grep of crates/core/src/curation/ finds no std::fs, std::env, std::process, OpenOptions or unsafe, and no allow(clippy in the draft modules. The claim holds today by inspection, not by lint.
  • Fix: extend the list so a future library change cannot add file system access unnoticed.

LOW-7 (context): suppressions

  • New: crates/core/tests/curation_draft.rs:7 #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]. This is a test target, the established pattern of the crate's integration tests (anatomy_*, archive_rules, curation_format, curation_legacy and others), and the pattern clippy.toml itself documents ("Tests ... allow these lints locally where they own I/O"). It allows a robustness/architecture lint, not a security scanner (nosemgrep, nosec, eslint-disable security), so it is not counted as an un-gated suppression.
  • Pre-existing: crates/core/src/bin/import_equine.rs:15 #![allow(clippy::disallowed_methods)] (present at 5e114da, line 11).
  • No nosemgrep, nosec, eslint-disable, ts-ignore or noqa was added in product files.

Notes (verified, no finding)

A. Seed SQL (seed.rs). Every single-quoted value is a constant or derived from one: sheet_id_for(EQUINE_SPECIES_ID, EQUINE_SHEET_VERSION), sha256 hex, the uuid asset id, usize, horse.svg, the description and the change note. Quotes are doubled, and backslashes are literal under standard_conforming_strings. Author data reaches only the dollar-quoted document literal, which is refused when the JSON contains $equine_doc$. The close cannot overlap the body's tail, because the body is a pretty-printed JSON object trimmed to its final }, so no suffix of it is a prefix of the tag. Ported verbatim from seed.ts.

B. Panics. Library unwrap/expect are denied by workspace lints. Every index is guarded: numbers[0..3] after len == 4, ids[0]/[1]/[2..] after the ids.len() >= 2 check for findings, and split_field(..)?[0] on a non-empty split. Byte slices cut on ASCII : and / (char boundaries). Vec::insert is bounded by insertion_index (checked <= len, After gives at most len). Vec::remove uses an index locate just resolved. move re-locates the destination after the removal. u64 positions saturate and are refused by the range check. The only aborts found are the stack overflows of MEDIUM-1 (panic = "abort" in release; a stack overflow aborts in any profile).

C. Strict validation. deny_unknown_fields on every authored struct; the op tag is removed before strict parsing of the variant. .optional() semantics: null is refused, absent is accepted. CHG-\d{3,} ids, duplicate change and reference ids, level C without a question, the reserved CHG-000 and empty ops or citations are all refused. Repeated JSON keys keep the last value, as JSON.parse does. references.json is parsed into the strict Reference.

D. --check only reads: fs::read, read_dir, is_dir. listed follows directory symlinks in the seed. A loop ends in an ELOOP error and a symlinked file is only compared, never printed. These are trusted, committed contents.

E. The oracle generator writes only crates/core/tests/oracles/r5/curation-synthetic.json (resolve(HERE, '../../../crates/core/tests/oracles/r5'), a constant), only when MAKE_R5_ORACLES === '1', and otherwise just compares. crates/core/tests/curation_draft.rs only reads.

F. Secrets: none in the 31 files (diff grep for key, token, password, bearer, service_role, JWT, PEM, cloud key prefixes and user:pass@ URLs; semgrep p/secrets 0). The references use example URLs without credentials.