diff --git a/.claude/tasks/TODO.md b/.claude/tasks/TODO.md index d10ca38a..424a481b 100644 --- a/.claude/tasks/TODO.md +++ b/.claude/tasks/TODO.md @@ -326,13 +326,19 @@ feat/findings-tree-v2 merged (FindingsTreePicker tracked in HEAD); all work belo - [x] R5.4c `render_draft_artifacts` complete (TS order); `import-equine --check` covers the 12 draft files with explicit criteria (bytes; declared tree rule), PENDING list and `shortcut:` removed; fails on any missing or unauthorised difference - [x] R5.4d proofs: `tests/curation_review.rs` (the 6 files, ledger digests, determinism); TS generator `r5.review-synthetic-oracles.test.ts` → `oracles/r5/review-synthetic.json` (changelog raw + formatted, review report, overlays, app drawings on mini-sheet cases: Unicode, pipes, newlines, long values, emoji refused, every status); R5.3 LOW carry-over (destination identity, cite membership, unreadable-seed message, stale comment, doc nits) - [x] R5.4e gates: 14 rounds; c4e717d security PASS (LOW only) + check:all green 16:41:34; eb667ad (docs only over it) fresh verifier 9/9 clauses MET, ECARTS = 13 FLOOR hits on audit prose (owner Clarification); docs RUST.md §11.14-11.15, CHANGELOG, CLAUDE.md; memory LRN-074, LRN-075, EVAL-029 → checkpoint delivered, STOP before R5.5 (R5.4 closes on owner validation) - - [ ] R5.5 `upload-sheet-assets` bin + runbook (checkpoint delivered 2026-10-23, closes on owner validation; owner go 2026-10-23 « Validation R5.4 et autorisation conditionnelle R5.5 »; local stack only; no production, no deploy; stop at the checkpoint, no R5.6) + - [x] R5.5 `upload-sheet-assets` bin + runbook (CLOSED 2026-10-24 by the owner: real local stack `pnpm e2e:sheet-assets` PASS 11/11, run by the owner by hand; owner go 2026-10-23 « Validation R5.4 et autorisation conditionnelle R5.5 »; local stack only; no production, no deploy; stop at the checkpoint, no R5.6) - [x] R5.5-0 R5.4 closing adjustments: FLOOR clarification (contract + `scripts/floor-clarification-check.sh`, 19 audit-prose hits requalified, 1 STUB in evals.md:205 outside scope, reported), 4 MiB cap on `changes.json` / `references.json` (core `MAX_CURATION_FILE_BYTES`, bin bounded read; 222 KB + 17 KB committed, parse of a 3.98 MB changeset peaks +39 MB), `tests/curation_caps.rs` (cap, margin, stable ids of any species), serde R2 tolerance probe (characterisation only, nothing changed) - [x] R5.5-1 core module `curation::upload` (pure, no I/O): manifest parse + entry rules, destination rule, key derivation, per-entry plan from observed states, closed error kinds - [x] R5.5-2 bin `upload-sheet-assets` (feature `upload`, ureq): `--check` read-only and write mode, destination verified (loopback or the DEV host, http, explicit port) then handshake (private bucket) before any transfer, existing objects verified by sha256 never overwritten, confirm only after the object is verified, bounded retries, secrets never printed - [x] R5.5-3 scripts: `assets:upload` points at the Rust bin (`scripts/upload-sheet-assets.sh`), TS script kept until R5.7 census; runbook SUPABASE.md §3.1, RUST.md §11.16 - [x] R5.5-4 tests (real-stack e2e NOT run by Claude: permission layer refused; owner runs `pnpm e2e:sheet-assets`): unit (core), bin tests against a stub HTTP server (refusals, partial failure, resume, network errors), local Supabase run (upload, rerun, anon refused, wrong key, tamper); `pnpm rust:check`, `pnpm check:all`, `r5:import-equine --check` - [x] R5.5-5 gates (round 1: verifier ECARTS(4) + security PASS 1 MEDIUM 9 LOW, fixed; round 2 fad85c0: verifier C1-C13 MET, check:all green 21:47:11, security PASS 5 LOW declared RUST.md §11.16): fresh security audit, fresh verifier on the contract `.claude/tasks/contracts/2026-10-23-rust-r5-upload.md`; memory; checkpoint report (9 points), STOP + - [ ] R5.5 closing corrections (owner 2026-10-24 « Clôture R5.5, corrections ciblées et préparation R5.6 »; contract `.claude/tasks/contracts/2026-10-24-rust-r5-5-closing.md`) + - [x] LOW: `Protocol` final, DEV address own unicast (broadcast refused), tests + - [x] serde: `check_v3_shape` at `read_report_document` v3 + `parse_envelope` (sheet, practitioner, severity), test turned around + - [x] `confirm_sheet_asset_upload` characterised (no SQL change), DEV keys + 54321 documented (ENVIRONMENTS.md §3.6) + - [x] R5.6 plan `.claude/tasks/plans/2026-10-24-rust-r5-6-fingerprint-switch.md` (4 decisions open) + - [ ] gates: check:all, security, fresh verifier → checkpoint, STOP (no R5.6 without signal) - [ ] R5.6 D-R5 switch (convention, aliases, membership test in gated readers with per-file go, seed regenerated once by the Rust tool, `.prettierignore`, CHANGELOG) - [ ] R5.7 consumer census, domain fixtures moved, packages deleted, xmldom + prettier out of the lockfile, docs, ledger; `pnpm check:all`; merge only on the owner's signal - [ ] separate chore (D-R7): form constraints module + PGlite `pg_constraint`/config.toml parity test diff --git a/.claude/tasks/contracts/2026-10-24-rust-r5-5-closing.md b/.claude/tasks/contracts/2026-10-24-rust-r5-5-closing.md new file mode 100644 index 00000000..912550f5 --- /dev/null +++ b/.claude/tasks/contracts/2026-10-24-rust-r5-5-closing.md @@ -0,0 +1,33 @@ +# CONTRACT: R5.5 closing corrections and R5.6 preparation + +- date: 2026-10-24 | branch `feature/rust-r5-pipeline-core` | base dead546..13db553 (R5.5 checkpoint) +- brief: owner 2026-10-24 « Clôture R5.5, corrections ciblées et préparation R5.6 » §1 to §9 + +## Clauses + +K1. **Owner result recorded.** RUST.md §11.18, TODO, journal and registries record `pnpm e2e:sheet-assets` PASS (11 checks) as +the owner's manual run, distinct from the simulated suite; no new measurement invented, no rerun with the service key, no secret +in any file or commit. + +K2. **Retry.** `Protocol` errors are final (no retry); only `Io`, `Timeout`, `HostNotFound`, `ConnectionFailed` and 5xx are +retried; negative test proves one request on a malformed answer. + +K3. **DEV address.** The DEV address counts only if it is an own unicast address; a subnet broadcast is refused; tests cover +loopback, the host's unicast and broadcast addresses, a public and a malformed value. + +K4. **Serde hardening, three shapes only.** `sheet` and `practitioner` as non-objects and `severity` as a non-string are refused +with `ReadFailure::Invalid` (field named) at the v3 entries `read_report_document` and `parse_envelope`; no other contract, +version or field narrowed or widened; frozen N-1 copies, goldens, fixtures and oracles untouched; every R2-R4, PDF import, +rebase and envelope suite green unchanged. + +K5. **SQL characterised, not changed.** No file under `supabase/migrations` or `supabase/tests` changes; RUST.md §11.18 states the +grants, the missing hash/existence check and why it is not exploitable from an API role. + +K6. **DEV stack documented, not changed.** ENVIRONMENTS.md §3.6 lists the 54321 checks and the default-key finding; no firewall, +network or Supabase configuration changed. + +K7. **R5.6 plan only.** `.claude/tasks/plans/2026-10-24-rust-r5-6-fingerprint-switch.md` covers the owner's 12 points and lists the +open decisions; no fingerprint code, no seed or `sheet-ref.json` change, `documentSha256` stays `27c7383c…2660`. + +K8. **Gates.** `pnpm rust:check` and `pnpm check:all` green; security PASS on the diff; fresh verifier on this contract. No merge, +no release, no deploy, no R5.6. diff --git a/.claude/tasks/plans/2026-10-24-rust-r5-6-fingerprint-switch.md b/.claude/tasks/plans/2026-10-24-rust-r5-6-fingerprint-switch.md new file mode 100644 index 00000000..18d63334 --- /dev/null +++ b/.claude/tasks/plans/2026-10-24-rust-r5-6-fingerprint-switch.md @@ -0,0 +1,85 @@ +# PLAN: Rust R5.6, the sheet fingerprint switch to `sha256-rfc8785-validated-v1` (prepared, NOT started) + +- date: 2026-10-24 | branch `feature/rust-r5-pipeline-core` | parent plan `2026-10-22-rust-r5-pipeline-core.md` §B.3.5, §B.5 row R5.6 +- brief: owner 2026-10-24 « Clôture R5.5, corrections ciblées et préparation R5.6 » §6 (convention confirmed, file scope + approved: `report/rebase.rs`, `archive/read.rs`, `session.rs`, `pdf/write/input.rs`; switch NOT authorised yet) +- decisions on record: BDR-068 (D-R5 = B), owner 2026-10-24 (one alias, `27c7383c…2660`; Report v3 unchanged) + +## A. What changes, what does not + +The value: `sha256(JCS(validated document))`, where the validated document is the R2 typed reading +(`AnatomicalSheetDocument` + `validate_sheet_document`, no issue) serialised back, and JCS is RFC 8785. Frozen study value for +the current draft: `12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2` (`oracles/r5/fingerprints.json`, +`b_rfc8785_validated`). `27c7383c…2660` (sha256 of the Prettier file bytes) becomes the one alias of that content. + +Unchanged: Report v3 shape (`sheet.documentSha256`, 64 hex), the N-1 reader, every PDF fixture, golden and R2-R4 oracle (they +keep `27c7…`, valid through the alias), `horse.svg` bytes and asset ids (no re-upload), the document content, the integrity +rules, the migration behaviour for a real content change. No view, zone, region, category or reference is renamed. + +## B. Steps (one subtask = one commit, gates at the end) + +1. **Canonicaliser.** `crates/core/src/domain/sheet/fingerprint.rs` (no feature: the core and the tools share it): JCS over + `serde_json::Value` (keys sorted by UTF-16 code units, ES number printing through the core's `js_number` (F1-fixed `zmij`), + string escaping of RFC 8785 §3.2.2.2, no whitespace), then sha256. Refuses non-finite numbers and lone surrogates (the core's + parser already does). Tests: every vector of `oracles/r5/fingerprints.json` `rfc8785_vectors` (RFC appendix inputs) and the + 44,322-row V8 number oracle `oracles/r5/numbers.json` through the canonical number printer; UTF-16 key order cases + (astral vs BMP, `é` vs `é`). +2. **Independent differential.** `scripts/r5-fingerprints.mjs` (its own Node JCS, RFC vectors first) is the reference, never + the Rust: a new `--json` field per document, then a Rust test reads a frozen output + `oracles/r5/fingerprint-differential.json` written once by `MAKE_R5_ORACLES=1` over the draft, the legacy document and + N synthetic documents (numbers at the ES formatting edges, Unicode keys, deep nesting). Added, never an existing oracle + rewritten. +3. **Validation before hashing.** `sheet_fingerprint(document_text) -> Result`: strict parse, + R2 shape, `validate_sheet_document` with zero issues, then JCS of the typed reading. A document with an issue has no + fingerprint (closed error), so an invalid sheet can never be stamped official. +4. **Versioned convention.** `const FINGERPRINT_CONVENTION: &str = "sha256-rfc8785-validated-v1"`; `v1` freezes steps 1 and 3. + No default anywhere: a sheet reference without a known convention name is refused at sheet load (`invalid_sheet`), and the + pre-switch reference (no convention field) is read as the legacy convention only for the frozen registry of step 5. +5. **Trusted alias registry.** A compiled-in table in the core, the only authority: + `(sheet_id 503dc301-…, convention "prettier-file-bytes-v0", alias 27c7383c…2660, canonical 12d78872…74e2)`, built from the + frozen oracle (P4) and the committed pre-switch `sheet-ref.json`, never from an archive, a PDF, a bundle or the database. + `sheet-ref.json` gains an additive `fingerprint` block (`convention`, `aliases: [...]`) for visibility; at sheet load the + core checks that every listed alias is in the registry for that `sheet_id` and canonical value, else the sheet is refused. +6. **Old archives.** An archive stamped `27c7…` on sheet `503dc301…` is "same document" as the loaded sheet whose canonical + value is `12d7…`: no rebase, no message, labels and snapshots kept. The exported envelope is never rewritten. +7. **The four gated readers** (owner approved scope; one membership test each, `same_document(report_sha, loaded_ref)`): + `report/rebase.rs:40` (skip the rebase on current value or registered alias), `session.rs` (draft restore and import go + through the rebase: no own comparison expected, verified), `archive/read.rs:118` (`snapshot_matches`: report vs archived + snapshot, both archived; equality kept, alias accepted only when the pair is the registered one), `pdf/write/input.rs:207` + (the export's sheet match: a report stamped with an alias exports against the current sheet). Nothing else in them moves. +8. **Seed and sheet-ref.** Regenerated once by `import-equine --out` into an empty folder, copied over `draft/` by the owner's + go after review of the diff: `sheet-ref.json` (value + block), `report-migration-map.json` (`sheet.documentSha256`, so a + migrated v2 report is stamped with the canonical value), nothing else (`equine_draft.sql` carries no fingerprint; checked). + `--check` then compares with the regenerated folder; the R5 oracles (TypeScript output) stay as frozen, and the two files' + difference from them is a declared rule (fingerprint field and block only), never an oracle regeneration. +9. **`.prettierignore`.** `supabase/seed/anatomical-sheets/equine/{legacy-import,draft}/` (the Rust-written paths, narrow), + with a reproducible check: `prettier --check` on the repo passes and `git diff` after `pnpm format` is empty on those paths. +10. **Non-regression.** Every R2-R4 suite, the 99 PDF fixtures (import, compare), the envelope goldens, the N-1 reader, the + 612-leaf migration, the app suite on the bundled sheet (wasm rebuilt), `report-compare` on the fixtures, `check:all`. +11. **Refusals.** A sheet-ref alias absent from the registry, an alias bound to another `sheet_id`, an alias whose canonical + value is not the loaded one, a malformed hex value, an unknown convention: each a closed refusal with a test; a report + stamped with an unknown fingerprint rebases as today (never treated as the official sheet). +12. **Rollback before merge.** Everything stays on the branch: revert the R5.6 commits (`git revert`, never a reset of pushed + history); the seed returns to `27c7…` from git; no database, Storage or asset change exists to undo; an archive exported + during the R5.6 tests with `12d7…` would rebase after a rollback (DEV only, labels kept). Rollback trigger: any authentic + archive (fixtures, goldens, owner devices) opening with a message or a rebase it did not have before. + +## C. Decisions still needed before the switch + +1. **The fifth comparison.** `crates/core/src/pdf/compare/lines.rs:350` (`report-compare`, the archive check tool) compares + fingerprints too and is not in the approved list. Without the membership test, it reports alias-stamped archives as "other + sheet". Recommendation: include it (same one-line test). +2. **The TypeScript writer.** `pnpm import:equine` and its golden test in `packages/anatomy` would write and expect `27c7…` + after the switch. Recommendation: the Rust tool becomes the only writer of the seed at R5.6, `pnpm import:equine` refuses to + write (message pointing at `r5:import-equine`) and its golden test is pinned to the pre-switch commit as historical evidence, + both deleted at R5.7. +3. **Where the alias authority lives.** Recommendation as step 5 (core registry authoritative, `sheet-ref.json` block for + visibility, mismatch refused). Alternative: `sheet-ref.json` alone (simpler, but then the bundle is the authority, which the + owner excluded). +4. **The app side.** `apps/app/src/sheets/provider.ts` reads `ref.document_sha256` only; the additive block needs no TS change. + Confirm no TS display of the fingerprint should show the convention. + +## D. Out of scope + +The migration-map cascade debt (§11.12) stays open: no rename in this switch. Step 13 (`anatomical_sheet_versions` storing a +fingerprint) is later. No merge, no release, no deploy. diff --git a/crates/core/src/bin/upload_sheet_assets/main.rs b/crates/core/src/bin/upload_sheet_assets/main.rs index f975833f..dc7ccb83 100644 --- a/crates/core/src/bin/upload_sheet_assets/main.rs +++ b/crates/core/src/bin/upload_sheet_assets/main.rs @@ -107,11 +107,18 @@ 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. +/// The DEV address counts only if it is one of this machine's own unicast addresses: a socket +/// binds to it, and connecting it to that address (no packet is sent) keeps it as the local +/// end. A subnet broadcast binds but refuses the connect (no `SO_BROADCAST`). Otherwise the +/// DEV address 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()) + let Ok(ip) = host.parse::() else { + return false; + }; + let Ok(socket) = std::net::UdpSocket::bind((ip, 0)) else { + return false; + }; + socket.connect((ip, 9)).is_ok() && socket.local_addr().is_ok_and(|a| a.ip() == ip) } fn connect() -> Result { @@ -221,3 +228,41 @@ fn main() { } } } + +#[cfg(test)] +mod tests { + use super::own_address; + + /// `inet` and `brd` addresses of this host, as `ip -4 -o addr` prints them. + fn host_addresses() -> (Vec, Vec) { + let listing = std::process::Command::new("ip") + .args(["-4", "-o", "addr"]) + .output() + .map(|output| String::from_utf8_lossy(&output.stdout).into_owned()) + .unwrap_or_default(); + let field = |line: &str, name: &str| { + let mut words = line.split_whitespace(); + words.find(|w| *w == name)?; + Some(words.next()?.split('/').next()?.to_owned()) + }; + let lines: Vec<&str> = listing.lines().collect(); + let unicast = lines.iter().filter_map(|l| field(l, "inet")).collect(); + let broadcast = lines.iter().filter_map(|l| field(l, "brd")).collect(); + (unicast, broadcast) + } + + #[test] + fn only_an_own_unicast_address_counts_as_the_dev_address() { + assert!(own_address("127.0.0.1")); + for refused in ["255.255.255.255", "203.0.113.7", "not-an-ip", ""] { + assert!(!own_address(refused), "{refused}"); + } + let (unicast, broadcast) = host_addresses(); + for address in &unicast { + assert!(own_address(address), "{address}"); + } + for address in &broadcast { + assert!(!own_address(address), "broadcast {address}"); + } + } +} diff --git a/crates/core/src/bin/upload_sheet_assets/storage.rs b/crates/core/src/bin/upload_sheet_assets/storage.rs index d5599368..f33178c5 100644 --- a/crates/core/src/bin/upload_sheet_assets/storage.rs +++ b/crates/core/src/bin/upload_sheet_assets/storage.rs @@ -193,13 +193,13 @@ fn is_transient(error: &ureq::Error) -> bool { | 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. +/// object larger than any drawing); anything else is a request the tool cannot build or an +/// answer it cannot read (a key a header refuses, a malformed HTTP answer: `Protocol`), the +/// same on every attempt. The key itself is never part of the message. fn final_error(error: &ureq::Error) -> UploadError { match error { ureq::Error::BodyExceedsLimit(limit) => refuse( diff --git a/crates/core/src/domain/archive/read.rs b/crates/core/src/domain/archive/read.rs index 8f06b607..14bf5b3a 100644 --- a/crates/core/src/domain/archive/read.rs +++ b/crates/core/src/domain/archive/read.rs @@ -12,7 +12,7 @@ use ts_rs::TS; use super::envelope::{ENVELOPE_VERSION, EnvelopeVersion1, EnvelopeWrapper, ReportArchiveEnvelope}; use super::snapshot::SheetSnapshot; use super::snapshot_rules::{SnapshotRejection, assert_snapshot}; -use crate::domain::report::read::{ReadFailure, read_report_document, version_of}; +use crate::domain::report::read::{ReadFailure, check_v3_shape, read_report_document, version_of}; use crate::domain::report::{Report, ReportMigrationMap}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] @@ -121,5 +121,7 @@ fn snapshot_matches(snapshot: &SheetSnapshot, report: &Report) -> bool { /// `ReportArchiveEnvelopeSchema.parse`: the shape of a whole envelope (the snapshot's shape /// only, no caps nor integrity), as the session keeps it. pub fn parse_envelope(data: &Value) -> Result { + let report = data.get("report").unwrap_or(&Value::Null); + check_v3_shape(report).map_err(serde::de::Error::custom)?; ReportArchiveEnvelope::deserialize(data) } diff --git a/crates/core/src/domain/report/read.rs b/crates/core/src/domain/report/read.rs index e3fb57fe..0fb983ef 100644 --- a/crates/core/src/domain/report/read.rs +++ b/crates/core/src/domain/report/read.rs @@ -49,6 +49,27 @@ pub fn version_of(data: &Value) -> Option { data.as_object()?.get("version")?.as_f64() } +/// The shapes Zod refused and serde's derives accept (owner go 2026-10-24, RUST.md §11.18): +/// `sheet` and `practitioner` written as positional arrays, a `severity` written as a one-key +/// map. Checked on the raw value before the typed read; nothing else is widened or narrowed. +pub fn check_v3_shape(data: &Value) -> Result<(), String> { + for key in ["sheet", "practitioner"] { + if data.get(key).is_some_and(|value| !value.is_object()) { + return Err(format!("{key}: expected an object")); + } + } + let observations = data.get("observations").and_then(Value::as_array); + for (index, observation) in observations.into_iter().flatten().enumerate() { + let severity = observation.get("severity"); + if severity.is_some_and(|value| !value.is_string()) { + return Err(format!( + "observations[{index}].severity: expected a string or null" + )); + } + } + Ok(()) +} + /// Reads a report document of version 2 or 3 and returns it as v3. pub fn read_report_document( data: &Value, @@ -56,6 +77,7 @@ pub fn read_report_document( ) -> Result { let version = version_of(data); if version == Some(3.0) { + check_v3_shape(data).map_err(ReadFailure::Invalid)?; let report = Report::deserialize(data).map_err(ReadFailure::invalid)?; return Ok(MigrationOutcome { report, diff --git a/crates/core/tests/characterisation_serde_tolerance.rs b/crates/core/tests/characterisation_serde_tolerance.rs index 5586830c..89cdaa57 100644 --- a/crates/core/tests/characterisation_serde_tolerance.rs +++ b/crates/core/tests/characterisation_serde_tolerance.rs @@ -1,14 +1,15 @@ -//! Characterisation, not a requirement (owner 2026-10-23, R5.5 brief §7): the serde derives of -//! the R2 contracts read a struct from a positional array and a unit enum from a one-key map, -//! where the frozen Zod reader refused both. These cases pin today's Rust verdict so that any -//! change to the gated readers is visible; they are to be turned around, not extended, if the -//! owner decides a strict deserialisation (`docs/development/RUST.md` §11.16). +//! The serde tolerance of the R2 report contract (RUST.md §11.16): serde's derives read a struct +//! from a positional array and a unit enum from a one-key map, where the frozen Zod reader +//! refused both. Owner go 2026-10-24: the three v3 shapes found are refused at the readers' +//! entry (`check_v3_shape`), with a controlled `ReadFailure::Invalid`; the canonical forms and +//! the non-strict unknown fields are read as before (RUST.md §11.18). #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] mod common; -use faunosteo_core::domain::report::Report; -use serde::Deserialize; +use faunosteo_core::domain::archive::parse_envelope; +use faunosteo_core::domain::report::read::check_v3_shape; +use faunosteo_core::domain::report::{ReadFailure, ReportMigrationMap, read_report_document}; use serde_json::{Value, json}; fn demo() -> Value { @@ -18,41 +19,90 @@ fn demo() -> Value { .unwrap() } -fn read(value: &Value) -> Result { - Report::deserialize(value) +fn map() -> ReportMigrationMap { + let (source, _) = common::zod_oracle("migration-map.draft.json"); + serde_json::from_str(&source).unwrap() +} + +fn read(value: &Value) -> Result<(), ReadFailure> { + read_report_document(value, &map()).map(|_| ()) +} + +fn sheet_as_array() -> Value { + let mut report = demo(); + let parts = [ + report["sheet"]["sheetId"].clone(), + Value::Null, + report["sheet"]["documentSha256"].clone(), + ]; + report["sheet"] = json!(parts); + report +} + +fn practitioner_as_array() -> Value { + let mut report = demo(); + let p = report["practitioner"].clone(); + report["practitioner"] = json!([p["name"], p["cabinet"], p["contact"], p["siret"]]); + report +} + +fn severity_as_map() -> Value { + let mut report = demo(); + report["observations"][0]["severity"] = json!({ "mild": null }); + report +} + +fn refused_with(value: &Value, what: &str) { + match read(value) { + Err(ReadFailure::Invalid(message)) => assert!(message.starts_with(what), "{message}"), + other => panic!("{what}: {other:?}"), + } } #[test] fn the_canonical_report_is_read() { assert!(read(&demo()).is_ok()); + assert!(check_v3_shape(&demo()).is_ok()); } #[test] -fn a_struct_written_as_a_positional_array_is_accepted_where_zod_refused() { - let mut sheet = demo(); - let parts = [ - sheet["sheet"]["sheetId"].clone(), - Value::Null, - sheet["sheet"]["documentSha256"].clone(), - ]; - sheet["sheet"] = json!(parts); - let mut practitioner = demo(); - let p = practitioner["practitioner"].clone(); - practitioner["practitioner"] = json!([p["name"], p["cabinet"], p["contact"], p["siret"]]); - assert!(read(&sheet).is_ok()); - assert!(read(&practitioner).is_ok()); +fn a_struct_written_as_a_positional_array_is_refused() { + refused_with(&sheet_as_array(), "sheet: expected an object"); + refused_with(&practitioner_as_array(), "practitioner: expected an object"); } #[test] -fn a_unit_enum_written_as_a_one_key_map_is_accepted_where_zod_refused() { - let mut report = demo(); - report["observations"][0]["severity"] = json!({ "mild": null }); - assert!(read(&report).is_ok()); +fn a_unit_enum_written_as_a_one_key_map_is_refused() { + refused_with( + &severity_as_map(), + "observations[0].severity: expected a string", + ); } #[test] -fn an_unknown_field_is_accepted_as_the_non_strict_zod_objects_did() { +fn an_absent_severity_and_an_unknown_field_are_read_as_before() { let mut report = demo(); + report["observations"][0] + .as_object_mut() + .unwrap() + .remove("severity"); report["extra"] = json!(1); assert!(read(&report).is_ok()); + // `severity` is optional, never nullable: null stays refused, as Zod refused it. + report["observations"][0]["severity"] = Value::Null; + refused_with(&report, "observations[0].severity: expected a string"); +} + +#[test] +fn the_envelope_entry_refuses_the_same_shapes() { + let envelope: Value = serde_json::from_str(&common::repo_file( + "apps/app/tests/fixtures/goldens/r2/envelope.demo.fr.nologos.json", + )) + .unwrap(); + assert!(parse_envelope(&envelope).is_ok()); + for bad in [sheet_as_array(), practitioner_as_array(), severity_as_map()] { + let mut wrapped = envelope.clone(); + wrapped["report"] = bad; + assert!(parse_envelope(&wrapped).is_err()); + } } diff --git a/crates/core/tests/upload_tool.rs b/crates/core/tests/upload_tool.rs index d145acba..75c9393e 100644 --- a/crates/core/tests/upload_tool.rs +++ b/crates/core/tests/upload_tool.rs @@ -25,6 +25,8 @@ struct State { row: Option<(String, bool)>, bucket_status: u16, fail_confirm: bool, + /// Answer every request with bytes that are not HTTP. + garbage: bool, log: Vec, } @@ -121,7 +123,14 @@ fn serve(state: Shared) -> u16 { for stream in listener.incoming() { let mut stream = stream.unwrap(); let (method, path, body) = read_request(&mut stream); - let (status, reply) = route(&mut state.lock().unwrap(), &method, &path, body); + let mut held = state.lock().unwrap(); + if held.garbage { + held.log.push(format!("{method} {path}")); + stream.write_all(b"NOT-HTTP garbage\r\n\r\n").unwrap(); + continue; + } + let (status, reply) = route(&mut held, &method, &path, body); + drop(held); respond(&mut stream, status, &reply); } }); @@ -352,3 +361,14 @@ fn a_dev_address_that_is_not_this_machine_is_ignored() { assert_eq!(output.status.code(), Some(1), "{said}"); assert!(said.contains("Destination"), "{said}"); } + +#[test] +fn a_malformed_answer_is_final_and_not_retried() { + let (state, url) = stub(); + state.lock().unwrap().garbage = true; + let output = run(&url, &["--check", DRAFT]); + let said = text(&output); + assert_eq!(output.status.code(), Some(1), "{said}"); + assert!(said.contains("not retried"), "{said}"); + assert_eq!(state.lock().unwrap().log.len(), 1); +} diff --git a/docs/development/ENVIRONMENTS.md b/docs/development/ENVIRONMENTS.md index 14cb52ad..b40be575 100644 --- a/docs/development/ENVIRONMENTS.md +++ b/docs/development/ENVIRONMENTS.md @@ -149,6 +149,31 @@ service-worker features) and OAuth providers that refuse `http://` redirect URIs. When one of those lands, the answer is a private HTTPS origin (local CA or a private name with a certificate), never the production domain. +### 3.6 The API port 54321 and the stack's keys (checked 2026-10-24) + +Measured on the host, no secret printed: `supabase/config.toml` sets no `jwt_secret` and no +signing keys, so the stack signs with the Supabase CLI's **public default** JWT secret and its +`service_role` key is the public demo key (`iss: supabase-demo`). Port 54321 is open to the LAN by +design (table above): any device of the private network that knows those public values can call +the API as `service_role`, past every RLS policy, on DEV data. Nothing of production is involved. +This is an owner decision (no automatic change): give the stack its own secret (a `jwt_secret` / +signing keys read with `env()` from a git-ignored file, then `pnpm db:reset` and new keys in +`.env.worker`), or narrow the 54321 allow rule to the test devices' addresses. + +What the UFW listing alone does not prove for 54321, and how to check it (owner, by hand): + +```bash +ss -ltnp 'sport = :54321' # who listens, on which address (0.0.0.0 / [::] expected) +sudo ufw status verbose # default incoming policy + any rule naming 54321 +docker info -f '{{.SecurityOptions}}' # "rootless" expected: published ports are host sockets, UFW applies +sudo iptables -S DOCKER-USER 2>/dev/null # rootful Docker only: DNAT bypasses UFW, rules belong here +nc -zv -w 3 54321 # from ANOTHER device: succeeds today (open by design) +nc -zv -w 3 54322 # from another device: must fail (54323, 54324 too) +``` + +A DENY on 54322 to 54324 is effective only if the daemon is rootless (§3.1) and the probe from +another device fails; this machine cannot test its own LAN firewall (§3.3). + ## 4. Procedure from another device (owner validation) On the Ubuntu machine: `pnpm db:start`, `pnpm dev:gateway`, the worker diff --git a/docs/development/RUST.md b/docs/development/RUST.md index 3671d5ca..0c293e7f 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -3392,3 +3392,47 @@ together with the serde decision above; the seed regenerated once by the Rust to re-uploaded only if their bytes change (they do not: `horse.svg` is frozen); `.prettierignore` on the Rust-written paths; the migration-map cascade debt (§11.12) fixed before any changeset renames a view, zone, region, category or cited reference. + +### 11.18 R5.5 closed; targeted corrections before R5.6 (2026-10-24) + +Owner brief « Clôture R5.5, corrections ciblées et préparation R5.6 ». Contract +`.claude/tasks/contracts/2026-10-24-rust-r5-5-closing.md`. + +**Real local stack: PASS, run by the owner by hand** (`pnpm e2e:sheet-assets`, 11 checks, verdict +`e2e:sheet-assets OK`). This result is the owner's, distinct from the assistant's simulated-server suite +(`upload_tool`); the assistant did not rerun it. R5.5 is closed. FLOOR clarification and the 4 MiB cap +accepted; `evals.md:205` stays declared, not requalified. + +**Two LOW corrections.** `Protocol` errors (a malformed HTTP answer, a key a header refuses) are final, no +longer retried (test `a_malformed_answer_is_final_and_not_retried`: one request, exit 1). The DEV address must +be an own unicast address: bound, then connected to itself (no packet) with that address kept as the local end; +a subnet broadcast binds but refuses the connect (binary test over the host's `inet` and `brd` addresses). The +other LOW reserves of §11.16 stay tracked. + +**Serde hardening of three v3 shapes (owner go).** `report::read::check_v3_shape` refuses on the raw value, +before the typed read, `sheet` or `practitioner` not an object and an observation `severity` not a string +(`ReadFailure::Invalid` with the field named). Called at the two entries that read a v3 report from outside: +`read_report_document` (v3 branch: PDF import, bare archives, draft restore) and `parse_envelope` (the session's +archive copy). Nothing else narrowed: an unknown field and an absent `severity` are read as before; `null` +severity was and stays refused (optional, never nullable). Test `characterisation_serde_tolerance` turned +around (5 cases, the envelope entry included). Every R2-R4 suite, the PDF import suites, the envelope goldens and +the N-1 reader pass unchanged; no authentic archive was rejected. Not covered by the go, left as is: the same +derives on Report v2 and on the other contracts. + +**`confirm_sheet_asset_upload`, characterised (no SQL change).** EXECUTE revoked from `public`, `anon`, +`authenticated`, granted to `service_role` only (migration 20261009100000; pgTAP 006: an editor gets 42501, +the service role confirms, twice is idempotent; `anon` is covered by the grant, not by its own assertion). The +function sets `uploaded_at` for the row id without checking that `.svg` exists in `storage.objects` or +that its bytes match: a service-role caller can confirm a row with no object or with other bytes under the key. +Not exploitable from an API role: no INSERT, UPDATE or DELETE policy exists on the bucket, so only the service +role can place bytes, and the service role already bypasses RLS. In the tool, the row is confirmed only after the +object is read back and hashed. Debt before any public exposure: check the object's existence (and size) in the +function. On DEV, see the next point: the service role is reachable from the LAN today. + +**DEV stack (owner §5).** Measured without printing a secret: the stack uses the Supabase CLI's public default +JWT secret, the service key is the public demo key (`iss: supabase-demo`), and 54321 is open to the LAN by +design, so a device of the network can act as `service_role` on DEV. The checks to run for 54321 (listener, UFW, +rootless or rootful Docker, a probe from another device) and the two remedies are in ENVIRONMENTS.md §3.6. No +firewall or configuration was changed. + +**R5.6 prepared, not started.** Plan `.claude/tasks/plans/2026-10-24-rust-r5-6-fingerprint-switch.md`.