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

13 KiB

Security audit: Rust R5.5 upload tool (2026-10-23)

Mode: audit. Scope: git diff 637ea63..HEAD (commits 2c44506, a481af5), 25 files, branch feature/rust-r5-pipeline-core. Contract: .claude/tasks/contracts/2026-10-23-rust-r5-upload.md. Fresh, blind scan. No run against a real stack, no transfer tool, no service key used.

SECURITY — VERDICT: PASS
TOOL: semgrep 1.168.0 — p/security-audit, p/secrets, p/owasp-top-ten
SCOPE: 25 files
BLOCKING:
  (none)
REPORTED (non-blocking):
  - [MEDIUM] secret-on-argv — scripts/sheet-assets-e2e.sh:29, :31
  - [LOW] secret-in-build-env — scripts/upload-sheet-assets.sh:14
  - [LOW] destination-trust (cleartext key, DEV address not proven local) — crates/core/src/curation/upload.rs:194-198
  - [LOW] error-classification (non-transient errors retried as Network) — crates/core/src/bin/upload_sheet_assets/storage.rs:76-81
  - [LOW] doc-exit-code — docs/development/SUPABASE.md:152 vs crates/core/src/bin/upload_sheet_assets/main.rs:167-175
  - [LOW] toctou-file-read (documented, hash-mitigated) — crates/core/src/bin/upload_sheet_assets/main.rs:49-67
  - [LOW] confirm-not-hash-bound (pre-existing RPC) — crates/core/src/bin/upload_sheet_assets/storage.rs:173-181
  - [LOW] lint-allow (architecture lint, not security) — crates/core/src/bin/upload_sheet_assets/main.rs:21 + 4 test files
  - [LOW] floor-guard-waiver (fixture lines, not security) — scripts/floor-clarification-check.sh:42-45
  - [LOW] serde-tolerance (pre-existing, characterised only) — crates/core/tests/characterisation_serde_tolerance.rs
PROOF: semgrep 45 rules on 25 files → 0 findings; checklist 21 checks → 10 findings

Method

  • semgrep 1.168.0, the three pinned packs, --metrics=off, on the 25 existing files of the diff: 45 rules applied (Rust, bash, JSON, YAML, Markdown), 0 findings, 0 errors, 0 skipped rules. Semgrep's Rust coverage is thin, so the review below is mostly manual reading.
  • Checklist (8): hardcoded secret, SQL by concatenation, unsanitised render, sensitive endpoint authz, internals surfaced, secret in a log, tracked .env / credential file, guardrails scanner (scan.py --domain api,supabase: 0 FAIL, 1 WARN outside the diff, see below).
  • Anti-gaming (1): every added suppression or waiver line in the diff.
  • Brief focus (12): destination allowlist, redirects, proxy, key exposure (logs, errors, argv, env, stubs, e2e), path traversal and symlinks, TOCTOU, size and memory bounds, overwrite / delete / visibility, partial failure and resume, error classification, retries on non-idempotent calls, dependency additions.
  • Dynamic, hermetic only: cargo test -p faunosteo-core --features upload --locked on upload_tool, curation_upload, curation_caps, characterisation_serde_tolerance: 25/25 pass (loopback stub, made-up key, SUPABASE_* unset). A scratch probe (scratchpad crate, path dependency, --offline) fed 40 crafted URLs to check_destination.

What holds (verified, not taken from the docs)

  • Destination allowlist. 40 probes: decimal, hex, octal and short IPv4 forms (2130706433, 0x7f000001, 0177.0.0.1, 127.1) all normalise to a loopback and are accepted as such; IPv4-mapped IPv6 ([::ffff:127.0.0.1], [::ffff:7f00:1]), 0.0.0.0, 0, localhost., localhost.localdomain, 127.0.0.1.nip.io, public addresses, names, ws:, file:, https:, zone ids are refused. Userinfo and backslash tricks (evil.com\@127.0.0.1, 127.0.0.1:54321@evil.com, evil.com#@127.0.0.1) are refused. Accepted oddities (http://@127.0.0.1:p, percent-encoded or full-width localhost, http:127.0.0.1:p, trailing /.) all reduce to an allowed host. The decisive property: the origin used for every request is rebuilt from the parsed Host and port (upload.rs:224-233), never the raw string, so no check/use parser differential exists.
  • The key is read only after the destination check (main.rs:110-115) and after every local file is verified (main.rs:177-178): a refused destination or a bad file sends nothing.
  • No redirect: max_redirects(0) in ureq 3.4.2 returns the 3xx as a response, never follows it and never errors (ureq-3.4.2/src/run.rs:224-236, config.rs:257-258); the 3xx then fails expect_status. No proxy: .proxy(None) overrides the default Proxy::try_from_env() (ALL_PROXY / HTTP_PROXY). No gzip / brotli features (workspace Cargo.toml:43), so no decompression bomb; every body is capped (1 MiB, objects 16 MiB + 1).
  • Key never in a URL, a message or a log: two headers only (storage.rs:88-89), Secret has a redacted Debug and no Display, Storage has no Debug; required_env names the variable, never the value; ureq error texts (error.rs:229-276) carry no header value; server bodies are never echoed (expect_status). Test key is a made-up literal (upload_tool.rs:14).
  • No overwrite, delete or visibility change: only GET and POST are issued (storage.rs:104, 123, 142, 161, 177), uploads carry x-upsert: false, the bucket must answer public: false, an existing object is hashed and other bytes refuse the run.
  • Order and resume: whole plan computed read-only before the first write (main.rs:180); the object is read back and hashed before the row is confirmed (main.rs:147-163). The POST retry is safe: a retried create that already landed answers Duplicate, which leads to the read-back check; the confirm RPC is idempotent (20261009100000_sheet_asset_storage.sql:41-50).
  • Paths: folder and files canonicalised and confined with component-wise starts_with; manifest file names are [A-Za-z0-9._-], no leading dot, .svg; ids are lowercase hex / uuid, so the object key and the PostgREST filter carry no injection.
  • Memory: drawings are read under 16 MiB, then refused above 2 MiB by the SVG reader (prescan.rs:33), so at most 64 x 2 MiB is held; curation files now capped at 4 MiB with take(cap + 1) (import_equine.rs:93-99) and in the core (schema.rs:386-392).
  • C1: curation/upload.rs has no std::fs, std::env, socket or clock. ureq is optional under upload only; crates/wasm enables pdf-read, pdf-write only. Cargo.lock adds one edge (faunosteo-core -> ureq); ureq 3.4.2 was already locked for the worker, no new package.

Findings

M1 [MEDIUM] service and anon keys on process argv in the e2e script

scripts/sheet-assets-e2e.sh:29 runs env -i ... SUPABASE_SERVICE_ROLE_KEY="$key" "$BIN": env is /usr/bin/env, an external process, so the key sits in its argv, readable by every local user through /proc/<pid>/cmdline and ps, and recorded by any execve audit rule. Line 31, grep -qF -e "$key" -e "$SERVICE" -e "$ANON", puts the service and anon keys on the argv of an external grep. This contradicts the script's own header ("held in variables, never printed") and the C7 intent. Impact is bounded: the key is the local stack's, the machine is single-user, the window is short, the owner runs the script. Hint: pass the key as a bash prefix assignment in a subshell that first unsets the variables you want cleared (( unset FAUNOSTEO_DEV_HOST INIT_CWD; SUPABASE_URL="$url" SUPABASE_SERVICE_ROLE_KEY="$key" exec "$BIN" "$@" )), and match with grep -qF -f <(printf '%s\n' "$key" "$SERVICE" "$ANON") (printf is a builtin).

L1 [LOW] service key in the environment of the compile step

scripts/upload-sheet-assets.sh:14 runs cargo run --release: on a first run, or after any source or lockfile change, cargo builds with the operator's exported SUPABASE_SERVICE_ROLE_KEY visible to every build script and proc-macro of the dependency tree. The tree is locked and cargo-deny'd, so this is a supply-chain exposure only. Hint: env -u SUPABASE_SERVICE_ROLE_KEY cargo build --release --locked ..., then exec target/release/upload-sheet-assets "$@".

L2 [LOW] destination trust: cleartext key, DEV address not proven local

The key always travels over plain http (https is refused), with no server authentication. Any process listening on the allowed local port receives it: declared and accepted in RUST.md §11.16. The DEV branch (upload.rs:194-198) accepts any private IPv4 equal to FAUNOSTEO_DEV_HOST without proving it is an address of this machine; if both variables name another LAN host, the key crosses the LAN in clear. C2 allows this and scripts/dev-env.sh:15 derives the address from hostname -I by default. Hint: in the binary, UdpSocket::bind((ip, 0)) succeeds only for a local interface address; refuse otherwise.

L3 [LOW] non-transient client-side errors retried and reported as Network

storage.rs:76-81 maps every ureq::Error to Network, retries three times, exit 3. That includes BodyExceedsLimit when the destination holds an object above 16 MiB + 1 under the key (a State condition: downloaded three times, then "network error") and Error::Http from a key holding characters invalid in a header (a usage error). No write follows either case, so no security consequence, but C6 says refusals are final and C8 that errors are classified. Hint: match BodyExceedsLimit to State and Http / BadUri to a non-retried kind.

L4 [LOW] exit code 2 documented as "usage"

docs/development/SUPABASE.md:152 lists "2 usage"; a wrong argument count exits 1 with kind Content (main.rs:167-175, asserted by upload_tool.rs:314). The code matches C8 ("2 usage (argument not UTF-8)"); the runbook overstates. Hint: say "2 an argument is not UTF-8".

L5 [LOW] check/open race on file reads (documented, mitigated)

main.rs:49-67: canonicalise, then metadata, then open the canonical path; a component swap between them is not defended, as the comment states. Mitigation is real: the bytes read must hash to the manifest's sha256 and pass the SVG reader, so a swap cannot exfiltrate unknown content. Single user working copy.

L6 [LOW] confirmation not bound to the hash (pre-existing RPC)

storage.rs:173-181 confirms by asset id; the row hash is checked at plan time and the object is verified just before, but confirm_sheet_asset_upload(asset_id) (supabase/migrations/20261009100000_sheet_asset_storage.sql:34-52, outside the diff) does not re-check sha256. A row hash change between plan and confirm would confirm an unverified pairing. No API role writes asset rows (authenticated holds SELECT only), so the window needs a service-role actor. Note for a future migration (an expected-hash parameter), not this diff.

L7 [LOW] new lint allows (context, not a security suppression)

main.rs:21 #![allow(clippy::disallowed_methods)] and the same inner attribute (plus unwrap_used, expect_used) at the head of the four new test files. clippy.toml lists clock, file and environment methods: an architecture lint for the core's I/O purity (C1), the same pattern as import_equine.rs and report_compare.rs. Not a security checker, not blocking. CLAUDE.md still says "the two files allowed std::fs"; it is three now.

L8 [LOW] floor-guard waivers on fixture lines (context)

scripts/floor-clarification-check.sh:42-45 carry # floor-guard: allow negative-proof fixture on lines that write @ts-ignore, nosemgrep and nosec into a mktemp -d throwaway repo. The tokens are bash string literals; they silence no semgrep or security check on this repository (no finding on those lines, semgrep ignores nosec). The waiver silences floor-guard (a test-floor guard, printed WAIVED) and carries no [gated <date>] marker. Not blocking under the security rule; reported so the orchestrator sees it.

L9 [LOW] serde tolerance of the gated readers (pre-existing, characterised only)

crates/core/tests/characterisation_serde_tolerance.rs pins that the R2 contracts accept a struct as a positional array and a unit enum as a one-key map, where Zod refused. This reaches the untrusted PDF import path: an integrity risk for crafted files (fields assigned in the wrong order without error), not a memory or execution one. No reader changed in this diff; the owner decision is pending before R5.6 (RUST.md §11.16).

Outside the diff (not counted)

  • guardrails WARN SB-POLICY-NOT-OWNER supabase/migrations/20261008110000_anatomical_sheets.sql:1177 (species_select_active not tied to auth.uid()): file not in the diff.
  • Blind spot for the owner: supabase/config.toml sets no jwt_secret and no signing keys, so the local stack most likely signs with the Supabase CLI default secret, making the local service_role key a publicly documented value. ENVIRONMENTS.md exposes the API port 54321 to the LAN. If that holds, anyone on the private network can already act as service_role on the DEV stack, and the care this tool takes with the key protects little there. I did not run supabase status to confirm the key value.
  • Not verified here: scripts/sheet-assets-e2e.sh against the real stack (owner-run by design; not run, per the brief). The anon-key and wrong-key cases rely on the real Storage answer.