From 4a829416c69dbee484c14165d002250f65ea41b4 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 28 Oct 2026 18:14:11 +0100 Subject: [PATCH] chore(memory): V1-2.1 corrections evidence, TODO --- .../2026-10-28-v1-2-1-corrections-evidence.md | 101 +++++++++++ .../2026-10-28-v1-2-1-repro-old-schema.sql | 158 ++++++++++++++++++ .claude/tasks/TODO.md | 12 ++ 3 files changed, 271 insertions(+) create mode 100644 .claude/audits/2026-10-28-v1-2-1-corrections-evidence.md create mode 100644 .claude/audits/2026-10-28-v1-2-1-repro-old-schema.sql diff --git a/.claude/audits/2026-10-28-v1-2-1-corrections-evidence.md b/.claude/audits/2026-10-28-v1-2-1-corrections-evidence.md new file mode 100644 index 00000000..17337ebc --- /dev/null +++ b/.claude/audits/2026-10-28-v1-2-1-corrections-evidence.md @@ -0,0 +1,101 @@ +# V1-2.1 corrections after the independent audit: evidence + +Owner brief 2026-10-28 « V1-2.1 : corrections après audit indépendant » (verdict CHANGES +REQUIRED on 3914eaa). Every run below used the isolated `pnpm db:sandbox` stack only +(containers `faunosteo_dbsandbox_*`, ports 57xxx); DEV was not read or written. + +## 1. Defects F1-F19 on the old schema (before M4) + +Script `2026-10-28-v1-2-1-repro-old-schema.sql` (one rolled-back transaction) on a sandbox of +the 12 base migrations of develop, run during the first V1-2.1 pass. Output: + +``` +F1 plain account created a personal PUBLIC sheet for species other +F5 an account with an UNCONFIRMED email created a sheet +F6 set_sheet_visibility changes visibility with no gate +F15 another plain account reads the public personal sheet: 1 row +F9 editor rewrote draft content / renamed the sheet by direct UPDATE +F4 version published with a drawing never confirmed +F2 the draft of an ARCHIVED sheet was published; create_sheet_draft opened a draft on it +F8 after archiving, another account reads 0 published versions +F13 a DEPRECATED version was deleted physically +F3 copied asset rows not confirmed +F14 a platform admin at aal1 published an official sheet +F19 grant_platform_role(owner) accepted at aal1; the new owner holds 0 verified factors +F7 upload confirmed with no object in Storage +F12 service_role published a draft by direct UPDATE +F17 service_role inserted an owner row directly into profile_roles +F16 sha256 of an asset used by a published version changed +``` + +F18 (last-owner race) was reproduced with two sessions; its test is +`packages/supabase/tests/sandbox/sheets.sandbox.ts`. + +## 2. service_role effective table privileges (§16.2 prerequisite) + +Measured on the M7 rehearsal stack (DEV path), before and after M4+M5: + +- before: ALL (DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE) on + `species`, `anatomical_sheets`, `anatomical_sheet_versions`, `anatomical_sheet_assets`, + `profiles`, `profile_roles`; +- after: none on those six tables (pgTAP 005 and 018 assert it, with the EXECUTE census). + +## 3. New tests red on 3914eaa, green on the corrected HEAD + +Stack built from the base migrations plus the M4/M5 of 3914eaa, then the new and changed +files of this pass: + +| File | On 3914eaa | On the corrected HEAD | +|---|---|---| +| 006 | 2 not ok: another editor uploads a pending key it did not register (L5) | 16/16 | +| 016 | 1 not ok: a draft from a deprecated version takes it as base (M2) | 50/50 | +| 017 | M1 approval on an archived sheet succeeds; M2 base from a deprecated version; the file aborts there | 78/78 | +| 018 | 93 not ok of 337: capability holders at aal1 get `rights.capability` instead of `rights.mfa` (§6.7); `delete_sheet_asset` answers a raw code | 337/337 | +| 019 | 38/38 (lost coverage of behaviour that was already correct) | 38/38 | +| sheets-concurrency | 5 failed of 15: publish against withdraw (no version lock), two deprecations (no sheet lock), archive against approve (no sheet lock) | 15/15 | + +## 4. Real-stack results on the corrected HEAD + +| Run | Fresh stack | Upgraded stack (M3 + drawing object, then M4+M5) | +|---|---|---| +| pgTAP (`test-db`) | 1 325/1 325 | 1 325/1 325 | +| sandbox suite, run 1 | 86/86 | 86/86 | +| sandbox suite, run 2 (same stack) | 86/86 | 86/86 | +| pgTAP again after both suite runs | 1 325/1 325 | 1 325/1 325 | +| `types --check` | | types match | + +PGlite: 125/125 (packages/supabase), the 17 pgTAP files included. + +## 5. Timing at the caps (§15, budget 2 000 ms for save, submit, publish) + +Synthetic document: 64 views, 1 024 annotations, 8 192 finding nodes (depth 8), 1 089 review +groups, 4 drawings of 2 000 000 bytes; text 1 807 583 bytes, stored jsonb 2 057 488 bytes +(the binding measure, cap 2 097 152). HTTP wall-clock through PostgREST on the development +machine, milliseconds (min / median / max): + +| Stack, run | save (7) | submit (5) | approve (3) | publish (3) | +|---|---|---|---|---| +| fresh, 1 | 97 / 102 / 111 | 3 / 4 / 4 | 278 / 283 / 286 | 272 / 285 / 288 | +| fresh, 2 | 95 / 110 / 119 | 5 / 6 / 6 | 200 / 283 / 297 | 192 / 274 / 287 | +| upgraded, 1 | 107 / 144 / 374 | 6 / 9 / 17 | 197 / 303 / 304 | 209 / 294 / 330 | +| upgraded, 2 | 98 / 108 / 114 | 4 / 6 / 7 | 198 / 282 / 286 | 193 / 281 / 290 | + +The core's at-caps fixture is V1-2.2 work; the contract fixes the budgets at the V1-2.3 +checkpoint and measures verification (120 s) there. + +## 6. M7: upgrade rehearsal with the drawing object + +DEV path: `up upgrade 20261028100000` from a copy whose migrations stop at M3, the historical +drawing placed as the operator tool placed it on DEV (the Git file +`supabase/seed/anatomical-sheets/equine/draft/assets/horse.svg`, 132 997 bytes, sha256 +`0ea5202f…08541` = the seeded row's; service-key upload, pre-M4 confirmation RPC), then M4+M5 by +`supabase migration up`. + +- `storage.objects` row (id, owner, metadata, version, timestamps), the asset row, the + historical sheet row and its version (content md5, updated_at): identical before and after. +- Changed, as M4/M5 intend: bucket limit 20 971 520 → 2 097 152; service_role privileges + ALL → none. +- Bytes through the Storage API before and after: identical to the Git file. +- After M4+M5, through the Storage API with real tokens: an editor at aal2 reads the 132 997 + bytes (sha256 equal to the key); the same editor at aal1, a catalogue reader and an anonymous + caller are refused (the historical sheet has no published version). diff --git a/.claude/audits/2026-10-28-v1-2-1-repro-old-schema.sql b/.claude/audits/2026-10-28-v1-2-1-repro-old-schema.sql new file mode 100644 index 00000000..dfd7fd50 --- /dev/null +++ b/.claude/audits/2026-10-28-v1-2-1-repro-old-schema.sql @@ -0,0 +1,158 @@ +-- V1-2.1 phase A: reproduce F1-F9, F12-F17, F19 on the OLD schema (develop, 12 migrations) +-- of the throwaway sandbox. One transaction, rolled back: nothing persists. +\set ON_ERROR_STOP 1 +begin; +set local client_min_messages = notice; + +insert into auth.users (id, email, email_confirmed_at) values + ('a1000000-0000-0000-0000-000000000001', 'repro-owner@sandbox.local', now()), + ('a1000000-0000-0000-0000-00000000000a', 'repro-alice@sandbox.local', now()), + ('a1000000-0000-0000-0000-00000000000b', 'repro-bob@sandbox.local', now()), + ('a1000000-0000-0000-0000-00000000000c', 'repro-unconfirmed@sandbox.local', null), + ('a1000000-0000-0000-0000-00000000000d', 'repro-admin@sandbox.local', now()); +select private.assign_platform_role(null, 'a1000000-0000-0000-0000-000000000001', 'owner'); +select private.assign_platform_role(null, 'a1000000-0000-0000-0000-00000000000d', 'admin'); + +create function pg_temp.doc(p_species text, p_asset uuid) returns jsonb language sql as $$ + select jsonb_build_object( + 'schema_version', 2, 'species_id', p_species, + 'name', jsonb_build_object('fr', 'Repro', 'en', 'Repro'), + 'description', '{}'::jsonb, 'scope_disclaimer', '{}'::jsonb, + 'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null), + 'vertebral_config', jsonb_build_object('regions', '[]'::jsonb), + 'references', '[]'::jsonb, + 'categories', jsonb_build_array(jsonb_build_object('id', 'c', 'name', jsonb_build_object('fr', 'C', 'en', 'C'), 'color', null)), + 'views', jsonb_build_array(jsonb_build_object('id', 'main', 'kind', 'main', + 'name', jsonb_build_object('fr', 'V', 'en', 'V'), 'svg_asset_id', p_asset, 'view_box', null)), + 'annotations', '[]'::jsonb, 'finding_zones', '[]'::jsonb); +$$; +grant execute on function pg_temp.doc(text, uuid) to authenticated, service_role; +create table pg_temp.ids (key text primary key, id uuid); +grant all on pg_temp.ids to authenticated, service_role; +create function pg_temp.id(k text) returns uuid language sql as $$ select id from pg_temp.ids where key = k $$; +grant execute on function pg_temp.id(text) to authenticated, service_role; +create function pg_temp.as_user(p uuid) returns void language sql as $$ + select set_config('request.jwt.claims', jsonb_build_object('sub', p, 'role', 'authenticated')::text, true); +$$; +grant execute on function pg_temp.as_user(uuid) to authenticated, service_role; + +-- F1, F5, F6, F15 ------------------------------------------------------------ +set local role authenticated; +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000a'); +insert into pg_temp.ids select 'f1', public.create_anatomical_sheet('other', 'Perso', 'perso-other', pg_temp.doc('other', null), '', 'public'); +do $$ begin raise notice 'F1 observed: plain account created a personal PUBLIC sheet for species other: %', + (select format('creator=%s official=%s visibility=%s species=%s', creator_user_id is not null, is_official, visibility, species_id) + from public.anatomical_sheets where id = pg_temp.id('f1')); end $$; +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000c'); +do $$ begin + perform public.create_anatomical_sheet('equine', 'Unconfirmed', 'unconfirmed-sheet', pg_temp.doc('equine', null)); + raise notice 'F5 observed: an account with an UNCONFIRMED email created a sheet'; +exception when others then raise notice 'F5 not reproduced: %', sqlerrm; end $$; +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000a'); +do $$ begin + perform public.set_sheet_visibility(pg_temp.id('f1'), 'private'); + perform public.set_sheet_visibility(pg_temp.id('f1'), 'public'); + raise notice 'F6 observed: set_sheet_visibility changes visibility with no gate'; +exception when others then raise notice 'F6 not reproduced: %', sqlerrm; end $$; +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000b'); +do $$ begin raise notice 'F15 observed: another plain account reads the public personal sheet: % row(s)', + (select count(*) from public.anatomical_sheets where id = pg_temp.id('f1')); end $$; + +-- F2, F4, F8, F9, F13: alice publishes v1 with an UNCONFIRMED drawing, archives, publishes v2 +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000a'); +insert into pg_temp.ids select 's', public.create_anatomical_sheet('equine', 'Repro', 'repro-sheet', pg_temp.doc('equine', null), '', 'public'); +insert into pg_temp.ids select 'asset', public.register_sheet_asset(pg_temp.id('s'), repeat('e', 64), 100, 'r.svg'); +insert into pg_temp.ids select 'v1', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('s'); +update public.anatomical_sheet_versions set content = pg_temp.doc('equine', pg_temp.id('asset')), change_note = 'direct' + where id = pg_temp.id('v1'); +do $$ begin raise notice 'F9 observed: editor rewrote draft content by direct UPDATE (no RPC): change_note=%', + (select change_note from public.anatomical_sheet_versions where id = pg_temp.id('v1')); end $$; +update public.anatomical_sheets set name = 'Renamed directly' where id = pg_temp.id('s'); +do $$ begin raise notice 'F9 observed: sheet renamed by direct UPDATE: %', + (select name from public.anatomical_sheets where id = pg_temp.id('s')); end $$; +select public.publish_sheet_version(pg_temp.id('v1')); +do $$ begin raise notice 'F4 observed: version published with a drawing never confirmed: uploaded_at null = %', + (select uploaded_at is null from public.anatomical_sheet_assets where id = pg_temp.id('asset')); end $$; +insert into pg_temp.ids select 'v2', public.create_sheet_draft(pg_temp.id('s'), null, 'v2'); +select public.archive_anatomical_sheet(pg_temp.id('s')); +do $$ begin + perform public.publish_sheet_version(pg_temp.id('v2')); + raise notice 'F2 observed: the draft of an ARCHIVED sheet was published'; +exception when others then raise notice 'F2 not reproduced: %', sqlerrm; end $$; +do $$ begin + perform public.create_sheet_draft(pg_temp.id('s'), null, 'v3 on archived'); + raise notice 'F2 observed: create_sheet_draft opened a draft on an ARCHIVED sheet'; +exception when others then raise notice 'F2 (draft) not reproduced: %', sqlerrm; end $$; +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000b'); +do $$ begin raise notice 'F8 observed: after archiving, another account reads % published version(s) of the sheet', + (select count(*) from public.anatomical_sheet_versions where sheet_id = pg_temp.id('s') and status <> 'draft'); end $$; +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000a'); +select public.restore_anatomical_sheet(pg_temp.id('s')); +select public.deprecate_sheet_version(pg_temp.id('v1'), 'old'); +do $$ begin + perform public.delete_sheet_version(pg_temp.id('v1')); + raise notice 'F13 observed: a DEPRECATED (once published) version was deleted physically: rows left %', + (select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('v1')); +exception when others then raise notice 'F13 not reproduced: %', sqlerrm; end $$; + +-- F3: bob forks alice's public sheet; copied asset rows lack uploaded_at +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000b'); +insert into pg_temp.ids select 'fork', public.fork_anatomical_sheet(pg_temp.id('v2'), 'Copie', 'copie-repro'); +reset role; +update public.anatomical_sheet_assets set uploaded_at = now() where id = pg_temp.id('asset'); +do $$ begin raise notice 'F3 observed: source drawing confirmed, copied rows confirmed: %', + (select string_agg((uploaded_at is not null)::text, ',') from public.anatomical_sheet_assets where sheet_id = pg_temp.id('fork')); end $$; + +-- F14: a platform admin, no capability, no MFA claim, creates and publishes an official sheet +set local role authenticated; +select pg_temp.as_user('a1000000-0000-0000-0000-00000000000d'); +insert into pg_temp.ids select 'off', public.create_anatomical_sheet('equine', 'Off', 'off-repro', pg_temp.doc('equine', null), '', 'public', true); +insert into pg_temp.ids select 'off-asset', public.register_sheet_asset(pg_temp.id('off'), repeat('f', 64), 100, 'o.svg'); +update public.anatomical_sheet_versions set content = pg_temp.doc('equine', pg_temp.id('off-asset')) where sheet_id = pg_temp.id('off'); +do $$ begin + perform public.publish_sheet_version((select id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('off'))); + raise notice 'F14 observed: a platform admin at aal1 (claims carry no aal) published an official sheet'; +exception when others then raise notice 'F14 not reproduced: %', sqlerrm; end $$; + +-- F19: an owner session with NO aal claim grants owner to another account that has no factor +select pg_temp.as_user('a1000000-0000-0000-0000-000000000001'); +do $$ begin + perform public.grant_platform_role('a1000000-0000-0000-0000-00000000000b', 'owner'); + raise notice 'F19 observed: grant_platform_role(owner) accepted at aal1 (claims: %)', current_setting('request.jwt.claims'); +exception when others then raise notice 'F19 not reproduced: %', sqlerrm; end $$; + +-- F7, F12, F17: the service_role session +reset role; +do $$ begin raise notice 'F19 observed: the new owner holds % verified factor(s); active owner rows now %', + (select count(*) from auth.mfa_factors where user_id = 'a1000000-0000-0000-0000-00000000000b' and status = 'verified'), + (select count(*) from public.profile_roles where user_id = 'a1000000-0000-0000-0000-00000000000b' and role = 'owner' and revoked_at is null); end $$; +insert into pg_temp.ids select 'pending', public.register_sheet_asset(pg_temp.id('off'), repeat('9', 64), 100, 'p.svg') + from (select pg_temp.as_user('a1000000-0000-0000-0000-00000000000d')) x; +select set_config('request.jwt.claims', '{"role":"service_role"}', true); +set local role service_role; +do $$ begin + perform public.confirm_sheet_asset_upload(pg_temp.id('pending')); + raise notice 'F7 observed: upload confirmed with no object in Storage: objects=% uploaded_at set=%', + (select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name = repeat('9', 64) || '.svg'), + (select uploaded_at is not null from public.anatomical_sheet_assets where id = pg_temp.id('pending')); +exception when others then raise notice 'F7 not reproduced: %', sqlerrm; end $$; +do $$ begin + update public.anatomical_sheet_versions set status = 'published', published_at = now() + where sheet_id = pg_temp.id('f1') and status = 'draft'; + raise notice 'F12 observed: service_role published a draft by direct UPDATE: %', + (select status from public.anatomical_sheet_versions where sheet_id = pg_temp.id('f1')); +exception when others then raise notice 'F12 not reproduced: %', sqlerrm; end $$; +do $$ begin + insert into public.profile_roles (user_id, role) values ('a1000000-0000-0000-0000-00000000000c', 'owner'); + raise notice 'F17 observed: service_role inserted an owner row directly into profile_roles'; +exception when others then raise notice 'F17 not reproduced: %', sqlerrm; end $$; + +-- F16: privileged session rewrites a drawing row's sha256 under a published version +reset role; +do $$ begin + update public.anatomical_sheet_assets set sha256 = repeat('0', 64) where id = pg_temp.id('off-asset'); + raise notice 'F16 observed: sha256 of an asset used by a published version changed: %', + (select left(sha256, 8) from public.anatomical_sheet_assets where id = pg_temp.id('off-asset')); +exception when others then raise notice 'F16 not reproduced: %', sqlerrm; end $$; + +rollback; diff --git a/.claude/tasks/TODO.md b/.claude/tasks/TODO.md index 17821757..bd35772f 100644 --- a/.claude/tasks/TODO.md +++ b/.claude/tasks/TODO.md @@ -544,6 +544,18 @@ Order (BDR-071): P0 docs + decisions → V1-1 clients and animals → V1-2 sheet - [x] D packages/supabase `sheets.ts` (closed SheetFailure kinds), old `anatomical-sheets.ts` gate + test deleted, types regenerated (no drift), schema-types map updated; upload tool `row_state` on `get_sheet_asset_state` - [x] E sandbox: real GoTrue MFA (totp.ts/mfa.ts helpers), aal1 refused / aal2 allowed on the role RPC, target-factor required, F18 cross-revoke leaves one owner; fresh install + M3→M5 upgrade green; V1-1 regression (65 sandbox tests) green - [x] F gates: rust:check, wasm:build, typecheck, test:run (566 app + 122 supabase), lint, build, db:types:check, sandbox test-db (850) + test (65) all green; BDR-084 + LRN-095 + journal; commit + push. NOT done: fresh verifier + security gates (owner to run); at-caps timing (needs the V1-2.2 synthetic doc); fuller MFA factor-management matrix → V1-2.5; exhaustive two-session concurrency beyond F18; check:all Docker image/worker stages not re-run (no app/worker/Dockerfile change) + - [ ] G corrections after the independent audit (owner brief 2026-10-28 « V1-2.1 : corrections après audit indépendant »; verdict CHANGES REQUIRED on 3914eaa, verifier M1-M7 + L1-L11): M4/M5 edited in place (never applied to DEV); sandbox only, DEV untouched; STOP at checkpoint, no V1-2.2 + - [x] G1 M1 `decide_sheet_review`: lock sheet → version → round, refuse an inactive or historical sheet; pgTAP; two-session archive vs approve + - [x] G2 M2 `create_sheet_draft`: base only from a `published` version with `published_review_id`; pgTAP: deprecated and pre-M5 sources give no base, every group to cover + - [x] G3 M3 pgTAP coverage from the verifier list (§5.3 matrix, F14 per RPC and policy + capability at aal1, F2, F8/D8, submit conflict, `review.uncoverable`, `publish.drawing.changed`, no carry for an author, removal group, `verdict.*`, species refusals, working copy slot / unchanged / verbatim, historical triggers, table-owner immutability + deprecation freeze, document guards at save, 2 MiB asset, species mismatch, one draft, number never reused, deactivated, Storage key variants, service_role confirm, SVG mime); no SQL-mirror test restored. Done: 017 rules (77), 018 access matrix (337), 019 immutability + Storage (38); 006/016 adapted; PGlite 125/125 + - [x] G3b found while testing: withdrawal of a published version's round (now `draft.not_a_draft`, lock version → round); approver in the author set refused at publication too (§6.7); working-copy slot race (historical row lock) and restore while another copy is active + - [ ] G4 LOW: stale refs (`mfa.sandbox.ts`, LEDGER 011 note, sheet-model rule) + shim-vs-image column test; write budget on the 10 unmetered RPCs; `rights.mfa` before `rights.capability`; slug collision closed code; `view.asset.unknown` at submit; INSERT policy = registering caller; acknowledgments append-only; deprecate locks the sheet first (two-deprecation race); `delete_sheet_asset` closed codes + historical; red-on-old evidence + service_role grants record versioned + - [ ] G5 M5 sandbox suite re-runnable (F18 restores the other owners, workspaces uses its own owner); full suite twice on one stack + - [ ] G6 M4 the six two-session scenarios of §15 + archive vs approve + revoke vs grant; at-caps timing (synthetic document: 1 024 annotations, 8 192 nodes, 64 views, depth 8) + - [ ] G7 M7 upgrade rehearsal from the M3 state with a drawing-object fixture (Git bytes, sha256 = DEV row), bytes and metadata before/after; DEV-copy criterion → owner decision + - [ ] G8 AR-1 on the upgraded stack: seed owner with two factors works at aal2, refused at aal1; without a factor usable at aal1 except roles and rights + - [ ] G9 gates: pgTAP fresh + upgraded, PGlite, unit, sandbox ×2, types, check:all; fresh verifier + security audit + - [ ] M6 `grant.self_review`: divergence and proposal presented; owner decision pending, rule unchanged - [ ] V1-2.2 core verdict, preview, change list, edit primitives, SVG cleaning · V1-2.3 drawings and worker verification (M6) · V1-2.4 wireframes (editor, review, admin entry) · V1-2.5 catalogue, admin space, rights, species, MFA · V1-2.6 editor level 1 · V1-2.7 preview, review, publication UI · V1-2.8 equine working copy and review preparation · V1-2.9 FR/EN acceptance and closure (owner checkpoints V1-2.3, V1-2.4, V1-2.6, V1-2.7, V1-2.8, V1-2.9 + each DEV operation of contract §16.11) ## Release readiness track (owner 2026-10-25, distinct from the functional roadmap; nothing here authorises a release or exposure)