diff --git a/.claude/audits/2026-10-23-rust-r5-upload-security.md b/.claude/audits/2026-10-23-rust-r5-upload-security.md new file mode 100644 index 00000000..68cb78d7 --- /dev/null +++ b/.claude/audits/2026-10-23-rust-r5-upload-security.md @@ -0,0 +1,185 @@ +# 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//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 ]` 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. diff --git a/.claude/audits/2026-10-23-rust-r5-upload-verify.md b/.claude/audits/2026-10-23-rust-r5-upload-verify.md new file mode 100644 index 00000000..d420cc7e --- /dev/null +++ b/.claude/audits/2026-10-23-rust-r5-upload-verify.md @@ -0,0 +1,21 @@ +# R5.5 verification — round 1 (fresh verifier, Opus; pinned model out of credits) on 2c44506 + a481af5 + +Contract `.claude/tasks/contracts/2026-10-23-rust-r5-upload.md`. Persisted by the orchestrator (the verifier is read-only). + +VERIFY — VERDICT: ECARTS(4) + +- C1 MET, C2 MET (`http://127.0.0.1:80` refused, stricter, pinned), C3 MET, C4 MET +- C5 NOT-MET: an object over MAX_ASSET_BYTES+1 hits ureq BodyExceedsLimit, mapped to Network and retried; exit 3 instead of a final State (no write happens, safety holds) +- C6 NOT-MET: ATTEMPTS = 3 is three attempts (two retries), not "retried three times" +- C7 MET (observation: e2e script puts the key on `env` argv, security M1) +- C8 MET, C9 MET, C10 MET +- C11 NOT-MET: SUPABASE.md exit codes say "2 usage", code exits 1 on usage; exit-1 list misses manifest and handshake. Nit: serde table "zone as array" row unpinned and contradictory wording +- C12 MET (no frozen path in the diff, documentSha256 27c7383c…2660, import --check 0 differences 6+12) +- C13 MET with caveat: `floor-clarification-check.sh` cannot print OK while the declared evals.md:205 STUB stands +- C14 UNVERIFIABLE by the verifier (check:all ran elsewhere: green 21:21:38, log in the session scratchpad) +- FLOOR diff-scoped: clean, 4 waived (fixture lines authorised by the R5.4 clarification) +- Runs: cargo test upload 251/29 suites, curation 241/27, workspace 277 (1 ignored), cli 259; fmt, clippy (5 configs), deny ok; `--check` RSS 18696 KB + +## Fixes after round 1 (orchestrator) + +C5: `final_error` maps BodyExceedsLimit to a final State, test `an_oversized_object_is_a_final_state_refusal_not_retried`. C6: ATTEMPTS = 4 (one try, three retries). C11: SUPABASE.md exit codes and retry wording; RUST.md table row reworded. C13: the script lists the declared line apart (still not requalified) and prints OK. Security M1, L1, L2, L3 fixed (RUST.md §11.16, gate round 1). diff --git a/crates/core/src/bin/upload_sheet_assets/main.rs b/crates/core/src/bin/upload_sheet_assets/main.rs index 99edff90..f975833f 100644 --- a/crates/core/src/bin/upload_sheet_assets/main.rs +++ b/crates/core/src/bin/upload_sheet_assets/main.rs @@ -107,8 +107,17 @@ fn required_env(name: &str) -> Result { .ok_or_else(|| refuse(UploadKind::Destination, format!("{name} is required"))) } +/// The DEV address counts only if it is one of this machine's own addresses (a socket can be +/// bound to it); otherwise it is ignored and only loopback stays allowed. +fn own_address(host: &str) -> bool { + host.parse::() + .is_ok_and(|ip| std::net::UdpSocket::bind((ip, 0)).is_ok()) +} + fn connect() -> Result { - let dev_host = env::var("FAUNOSTEO_DEV_HOST").ok(); + let dev_host = env::var("FAUNOSTEO_DEV_HOST") + .ok() + .filter(|h| own_address(h)); let destination = check_destination(&required_env("SUPABASE_URL")?, dev_host.as_deref())?; let key = Secret::new(required_env("SUPABASE_SERVICE_ROLE_KEY")?); Ok(Storage::new(&destination, key)) diff --git a/crates/core/src/bin/upload_sheet_assets/storage.rs b/crates/core/src/bin/upload_sheet_assets/storage.rs index cfc35479..7fa0734f 100644 --- a/crates/core/src/bin/upload_sheet_assets/storage.rs +++ b/crates/core/src/bin/upload_sheet_assets/storage.rs @@ -13,7 +13,8 @@ use serde_json::{Value, json}; use ureq::Agent; const MAX_BODY: u64 = 1024 * 1024; -const ATTEMPTS: u32 = 3; +/// One try and three retries. +const ATTEMPTS: u32 = 4; /// A key that never prints: `Debug` hides it and there is no `Display`. pub struct Secret(String); @@ -61,8 +62,8 @@ impl Storage { } } - /// One exchange, retried on a transport error or a 5xx only (a refusal is final). The - /// request is built again on each attempt. + /// One exchange, retried on a transport error or a 5xx only (a refusal, an oversized body + /// and a malformed request are final). The request is built again on each attempt. fn exchange( &self, limit: u64, @@ -77,7 +78,10 @@ impl Storage { { Ok(reply) if reply.status < 500 => return Ok(reply), Ok(reply) => last = refuse(UploadKind::Network, format!("HTTP {}", reply.status)), - Err(error) => last = refuse(UploadKind::Network, error.to_string()), + Err(error) if is_transient(&error) => { + last = refuse(UploadKind::Network, error.to_string()); + } + Err(error) => return Err(final_error(&error)), } } Err(last) @@ -181,6 +185,34 @@ impl Storage { } } +/// The connection could not be made or broke: worth another attempt. +fn is_transient(error: &ureq::Error) -> bool { + matches!( + error, + ureq::Error::Io(_) + | ureq::Error::Timeout(_) + | ureq::Error::HostNotFound + | ureq::Error::ConnectionFailed + | ureq::Error::Protocol(_) + ) +} + +/// Not retried: a body over its cap is a destination holding something it should not (an +/// object larger than any drawing), anything else a request the tool cannot build (a key with +/// characters a header refuses). The key itself is never part of the message. +fn final_error(error: &ureq::Error) -> UploadError { + match error { + ureq::Error::BodyExceedsLimit(limit) => refuse( + UploadKind::State, + format!("the destination answered more than {limit} bytes"), + ), + _ => refuse( + UploadKind::Handshake, + "the request could not be built (check the key)", + ), + } +} + fn row_state_of(row: &Value, expected_sha256: &str) -> RowState { let found = row .get("sha256") diff --git a/crates/core/tests/upload_tool.rs b/crates/core/tests/upload_tool.rs index 8c040ba4..d145acba 100644 --- a/crates/core/tests/upload_tool.rs +++ b/crates/core/tests/upload_tool.rs @@ -314,3 +314,41 @@ fn a_usage_error_names_the_usage() { assert_eq!(output.status.code(), Some(1)); assert!(text(&output).contains("usage")); } + +#[test] +fn an_oversized_object_is_a_final_state_refusal_not_retried() { + let (state, url) = stub(); + let huge = vec![b'x'; 16 * 1024 * 1024 + 2]; + state + .lock() + .unwrap() + .objects + .insert(format!("{SHA}.svg"), huge); + let output = run(&url, &[DRAFT]); + let said = text(&output); + assert_eq!(output.status.code(), Some(1), "{said}"); + assert!(said.contains("State"), "{said}"); + let log = state.lock().unwrap().log.clone(); + assert_eq!( + log.iter() + .filter(|l| l.contains("/storage/v1/object/")) + .count(), + 1 + ); + assert_eq!(writes(&state), 0); +} + +#[test] +fn a_dev_address_that_is_not_this_machine_is_ignored() { + let output = Command::new(env!("CARGO_BIN_EXE_upload-sheet-assets")) + .args(["--check", DRAFT]) + .env_clear() + .env("SUPABASE_URL", "http://10.255.255.254:54321") + .env("FAUNOSTEO_DEV_HOST", "10.255.255.254") + .env("SUPABASE_SERVICE_ROLE_KEY", KEY) + .output() + .unwrap(); + let said = text(&output); + assert_eq!(output.status.code(), Some(1), "{said}"); + assert!(said.contains("Destination"), "{said}"); +} diff --git a/docs/development/RUST.md b/docs/development/RUST.md index dfcdbdd3..fe449168 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -3287,15 +3287,17 @@ DELETE, no bucket call but the read). **Destination rule.** `http`, explicit port, no credentials, path, query or fragment; host `localhost`, a loopback address, or the private IPv4 address equal to `FAUNOSTEO_DEV_HOST`. A name other than `localhost` is never accepted (it cannot be proven local without resolving it). The handshake then -checks the private bucket and the key before any object is read or written. A local port held by +checks the private bucket and the key before any object is read or written. The DEV address counts only if a socket can be bound to it (an address of this machine). +A local port held by something else would receive the key: accepted on a single-user machine, the same stance as the worker's `SUPABASE_URL`. **Tests.** `curation_upload` (7: manifest family, bytes, 15 destination refusals + DEV host rule, -plan), `upload_tool` (10, the binary against a loopback stub with a made-up key: `--check` writes +plan), `upload_tool` (12, the binary against a loopback stub with a made-up key: `--check` writes nothing, run then rerun, different bytes never overwritten, missing and foreign row stop before any write, 401/403, resume after a failed confirmation, unreachable port, refused destinations, folder -outside the seed, usage), `curation_caps` (4). **Not run by the assistant**: the session against the +outside the seed, usage, an oversized object refused once as `State`, a DEV address that is not this +machine), `curation_caps` (4). **Not run by the assistant**: the session against the real local stack (`pnpm e2e:sheet-assets`: upload, rerun, anon key, wrong key, refused destinations, unreachable port); the assistant's own run of the tool with the service key was refused by the permission layer (CLAUDE.md: no transfer tool run by Claude) and was not retried by another route. @@ -3333,7 +3335,7 @@ and a unit enum variant from a one-key map. Probed on Report v3 (test | `"sheet": [sheetId, null, documentSha256]` | accepted | refused | | `"practitioner": [name, cabinet, contact, siret]` | accepted | refused | | `observations[0].severity` = `{"mild": null}` | accepted | refused | -| a zone written as an array | refused only when the order is wrong, accepted when it matches the field order | refused | +| a zone written as an array (probe, not pinned by the test) | accepted when the types of the positions fit the fields, whatever they mean; refused in the probe because a label landed on `laterality` | refused | | `"extra": 1` (unknown field) | accepted | accepted (non-strict objects) | Consequence of accepting: a hand-built or foreign attachment imports; the report is re-serialised in @@ -3349,3 +3351,18 @@ the same readers there; until then the gated readers are unchanged. Open points carried: debt list of §11.12 and §11.13 stands (migration map cascade replay, `clippy.toml` I/O list); the upload tool is not run against the real stack by the assistant. + +Gate round 1 (2c44506 + a481af5): `pnpm check:all` green 21:21:38; security PASS (1 MEDIUM, 9 LOW, +`.claude/audits/2026-10-23-rust-r5-upload-security.md`); fresh verifier ECARTS(4) +(`.claude/audits/2026-10-23-rust-r5-upload-verify.md`): an object over the size cap was classified +`Network` and retried (now a final `State`, test), "retried three times" meant three attempts (now four, +one try and three retries), the runbook's exit codes (fixed), the FLOOR script could not print OK while +the declared `evals.md:205` line stands (it is now listed as declared, still not requalified). Security +fixes taken: keys off every argv in `sheet-assets-e2e.sh` (M1); the tool is compiled without the key in +the environment, then run (L1); the DEV address must be one of this machine's (L2); a malformed request +is final, not retried (L3); CLAUDE.md names the three files allowed `std::fs` (L7). Kept and declared: +the cleartext key on plain http to a local port (L2), the file-read race mitigated by the hash (L5), the +pre-existing `confirm_sheet_asset_upload` that does not re-check the hash (L6, SQL outside R5.5), the +waived fixture lines (L8), the serde tolerance (L9, owner decision). Outside the diff, reported by the +audit and not verified: `supabase/config.toml` sets no JWT secret, so the local stack likely uses the +CLI's public default keys while port 54321 is open to the LAN. diff --git a/docs/development/SUPABASE.md b/docs/development/SUPABASE.md index 9fdb374a..aa51a57e 100644 --- a/docs/development/SUPABASE.md +++ b/docs/development/SUPABASE.md @@ -147,9 +147,12 @@ write: hash on their own sheet opens nothing. A rerun after any failure resumes: an object already verified is kept, a -confirmed row is left alone. Network errors and 5xx answers are retried three -times; a refusal (401, 403, a state mismatch) is final. Exit codes: 0 done, 1 -refused (destination, content, state, access), 2 usage, 3 network. The key is +confirmed row is left alone. A connection failure or a 5xx answer is retried +three times (four attempts); a refusal (401, 403, a state mismatch, an +oversized answer) is final. Exit codes: 0 done, 1 refused (usage, manifest, +destination, content, handshake, state, access), 2 an argument that is not +UTF-8, 3 network. The DEV address counts only if it is an address of this +machine. The key is never printed and goes through no proxy. `pnpm e2e:sheet-assets` runs the whole behaviour against the local stack (upload, rerun, anon and wrong key, refused destinations, unreachable port). The TypeScript tool stays as diff --git a/scripts/floor-clarification-check.sh b/scripts/floor-clarification-check.sh index fd92ca06..44576e7b 100644 --- a/scripts/floor-clarification-check.sh +++ b/scripts/floor-clarification-check.sh @@ -11,14 +11,19 @@ set -uo pipefail GUARD="$HOME/.claude/lib/floor-guard.sh" BASE="${1:-develop}" ALLOWED_RE='^FLOOR SUPPRESS \.claude/audits/[^:]+\.md:[0-9]+ ' +# Declared in the clarification and left to the owner: reported, never requalified, not +# counted as a new finding. Anything else outside ALLOWED_RE fails. +DECLARED_RE='^FLOOR STUB \.claude/memory/evals\.md:205 ' # rc 0: every finding is requalified; rc 1: at least one real finding remains. classify() { local report="$1" total requalified remaining total=$(grep -c '^FLOOR [A-Z_]* ' "$report") requalified=$(grep -cE "$ALLOWED_RE" "$report") - remaining=$(grep '^FLOOR [A-Z_]* ' "$report" | grep -vE "$ALLOWED_RE") + declared=$(grep -E "$DECLARED_RE" "$report") + remaining=$(grep '^FLOOR [A-Z_]* ' "$report" | grep -vE "$ALLOWED_RE" | grep -vE "$DECLARED_RE") echo "findings $total, requalified $requalified" + [ -z "$declared" ] || { echo "DECLARED (owner decision pending):"; echo "$declared" | cut -c1-200; } [ -z "$remaining" ] || { echo "REMAINING:"; echo "$remaining" | cut -c1-200; return 1; } } diff --git a/scripts/sheet-assets-e2e.sh b/scripts/sheet-assets-e2e.sh index a3acb752..d3d76cfc 100755 --- a/scripts/sheet-assets-e2e.sh +++ b/scripts/sheet-assets-e2e.sh @@ -15,7 +15,7 @@ SEED=supabase/seed/anatomical-sheets/equine/draft BIN=target/release/upload-sheet-assets FAIL=0 -cargo build --release --locked -q -p faunosteo-core --features upload --bin upload-sheet-assets || exit 2 +env -u SUPABASE_SERVICE_ROLE_KEY cargo build --release --locked -q -p faunosteo-core --features upload --bin upload-sheet-assets || exit 2 STATUS=$(bash scripts/supabase.sh status -o json 2>/dev/null) || { echo "stack not running (pnpm db:start)"; exit 2; } field() { printf '%s' "$STATUS" | python3 -c "import json,sys;print(json.load(sys.stdin)['$1'])"; } SERVICE=$(field SERVICE_ROLE_KEY) @@ -26,9 +26,10 @@ LOCAL=http://127.0.0.1:54321 expect() { local name=$1 want=$2 url=$3 key=$4; shift 4 local out rc - out=$(env -i PATH="$PATH" SUPABASE_URL="$url" SUPABASE_SERVICE_ROLE_KEY="$key" "$BIN" "$@" 2>&1); rc=$? + # Keys go through the environment of the binary and grep's pattern file, never on an argv. + out=$(unset FAUNOSTEO_DEV_HOST; SUPABASE_URL="$url" SUPABASE_SERVICE_ROLE_KEY="$key" "$BIN" "$@" 2>&1); rc=$? LAST=$out - if [ "$rc" -eq "$want" ] && ! grep -qF -e "$key" -e "$SERVICE" -e "$ANON" <<<"$out"; then + if [ "$rc" -eq "$want" ] && ! grep -qF -f <(printf '%s\n' "$key" "$SERVICE" "$ANON") <<<"$out"; then echo "PASS $name (exit $rc)" else echo "FAIL $name (exit $rc, wanted $want)"; FAIL=1 diff --git a/scripts/upload-sheet-assets.sh b/scripts/upload-sheet-assets.sh index 3ea2ff5e..e4aeea97 100755 --- a/scripts/upload-sheet-assets.sh +++ b/scripts/upload-sheet-assets.sh @@ -11,4 +11,6 @@ set -euo pipefail cd "$(dirname "$0")/.." . scripts/rust-env.sh -exec cargo run --release --locked -q -p faunosteo-core --features upload --bin upload-sheet-assets -- "$@" +# Compile without the key in the environment (build scripts never see it), then run the binary. +env -u SUPABASE_SERVICE_ROLE_KEY cargo build --release --locked -q -p faunosteo-core --features upload --bin upload-sheet-assets +exec target/release/upload-sheet-assets "$@"