feat(supabase): V1-2.1 M4/M5 secure sheet access and review workflow

M4 20261028100000_sheet_access_hardening + M5 20261028110000_sheet_review_workflow
(applied together; no unprotected window). Implements contract §5-§14.

- Catalogue rights table + owner RPCs; has_mfa / has_aal2_session (live
  auth.sessions check); grant_active vs caller_has split; catalogue reader
  predicate; can_edit_sheet on the sheet_editor capability (F14).
- AR-1 B: aal2 required on every platform role change, target verified factor,
  race-free last-owner guard (F18/F19); service_role loses table privileges on
  the catalogue + profiles/profile_roles and EXECUTE on the role RPCs (F12/F17).
- Reviewed lifecycle: version provenance + review columns, content/freeze/
  author triggers, rounds/comments/acknowledgments, save/submit/withdraw/
  comment/acknowledge/decide, atomic publish_sheet_version(review_id) on the
  stored verdict, deprecate/archive/restore, species RPCs, equine working copy,
  uploads with the storage check (F7) + editor INSERT policy + 2 MiB bucket.
  F1-F3,F5,F6,F8,F9,F13,F15,F16 closed. Verdict is a fixture until M6 (V1-2.3).
- O22: SQL business mirror removed, declared status check kept; pgTAP 011 and
  the Rust generator's SQL parts deleted, ledger retargeted.
- Access layer sheets.ts (closed SheetFailure kinds); old client publish gate
  deleted; types regenerated; upload tool reads via get_sheet_asset_state.

Tested on the throwaway sandbox only; DEV untouched. pgTAP 850 (real stack) /
122 (PGlite) green; real-GoTrue AR-1 B + F18 race green; fresh install and
M3->M5 upgrade green; V1-1 regression green; rust:check, typecheck, lint,
build, types drift clean. BDR-084, LRN-095.
This commit is contained in:
bchanot committed 2026-10-28 15:52:49 +01:00
1 parent 9c13bae4bc
commit 77e5fa4cf5
33 files changed
+4613 -1617

No files matched your search

+67 -87
View File
@@ -1,62 +1,38 @@
-- Official equine sheet seed (supabase/seed/anatomical-sheets/equine/draft):
-- present as a platform-owned DRAFT, never published by the seed, publishable
-- by platform admins only, content accepted by the SQL referential gate.
-- Official equine seed after V1-2.1: the historical sheet is present as a
-- platform-owned DRAFT, protected on the server (never saved, submitted,
-- published, archived or deleted), visible to capability holders at aal2 and
-- to no one else (no published version → not in the catalogue). Contract §14.2.
begin;
select plan(20);
insert into auth.users (id, email) values
('40000000-0000-0000-0000-000000000001', 'root-seed@test.local'),
('40000000-0000-0000-0000-00000000000a', 'alice-seed@test.local');
select private.assign_platform_role(null, '40000000-0000-0000-0000-000000000001', 'owner');
select plan(17);
create table pg_temp.ids (key text primary key, id uuid not null);
create function pg_temp.id(p_key text) returns uuid language sql stable
as $$ select id from pg_temp.ids where key = p_key $$;
create function pg_temp.id(p_key text) returns uuid language sql stable as $$ select id from pg_temp.ids where key = p_key $$;
grant all on pg_temp.ids to anon, authenticated, service_role;
grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
insert into pg_temp.ids select 'sheet', id from public.anatomical_sheets where slug = 'faunosteo-cheval' and is_official;
insert into pg_temp.ids select 'draft', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet');
-- 1. Seeded state
select is(pg_temp.id('sheet'), '503dc301-a8b1-50d4-9e44-0919b2fc2916'::uuid, 'the historical sheet keeps its fixed id');
select results_eq(
$$ select species_id, creator_user_id, is_official, visibility::text, status::text, current_version_id
from public.anatomical_sheets where id = pg_temp.id('sheet') $$,
$$ values ('equine', null::uuid, true, 'private', 'active', null::uuid) $$,
'official equine sheet: platform-owned, private, active, no published version'
'historical sheet: platform-owned, private, active, no published version'
);
select results_eq(
$$ select version_number, status::text, document_schema_version, published_at
from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet') $$,
$$ values (1, 'draft', 2, null::timestamptz) $$,
'exactly one version, draft v1, never published'
);
select is((select count(*) from public.anatomical_sheet_assets where sheet_id = pg_temp.id('sheet')), 1::bigint, 'one SVG asset registered (the cleaned drawing)');
select is(
(select content ->> 'species_id' from public.anatomical_sheet_versions where id = pg_temp.id('draft')),
'equine', 'document species is equine'
);
select is(
(select jsonb_array_length(content -> 'views') from public.anatomical_sheet_versions where id = pg_temp.id('draft')),
2, 'document has two views'
$$ select version_number, status::text, document_schema_version, revision, coalesce(array_length(authors,1),0), published_at
from public.anatomical_sheet_versions where id = pg_temp.id('draft') $$,
$$ values (1, 'draft', 2, 0, 0, null::timestamptz) $$,
'one version: draft v1, schema 2, revision 0, empty author set, never published'
);
select is((select count(*) from public.anatomical_sheet_assets where sheet_id = pg_temp.id('sheet')), 1::bigint, 'one SVG asset registered');
select is((select content ->> 'species_id' from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 'equine', 'document species is equine');
select is((select jsonb_array_length(content -> 'annotations') from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 28, '28 annotations');
select is((select jsonb_array_length(content -> 'finding_zones') from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 27, '27 finding zones');
select ok(
(select content -> 'views' -> 1 -> 'svg_asset_id' = 'null'::jsonb
and jsonb_typeof(content -> 'views' -> 1 -> 'view_box') = 'object'
from public.anatomical_sheet_versions where id = pg_temp.id('draft')),
'head view is a crop of the main drawing: no asset, a view_box'
);
select is(
(select jsonb_array_length(content -> 'annotations') from public.anatomical_sheet_versions where id = pg_temp.id('draft')),
28, 'document has 28 annotations (27 legacy + carpus)'
);
select is(
(select jsonb_array_length(content -> 'finding_zones') from public.anatomical_sheet_versions where id = pg_temp.id('draft')),
27, 'document has 27 finding zones (26 legacy + carpus)'
);
select is(
(select private.sheet_document_issues(content) from public.anatomical_sheet_versions where id = pg_temp.id('draft')),
'{}'::text[], 'SQL referential gate finds no issue'
(select private.sheet_document_has_shape(content) from public.anatomical_sheet_versions where id = pg_temp.id('draft')),
'the seeded document has the structural shape SQL still checks'
);
select is(
(select private.sheet_document_unknown_assets(sheet_id, content) from public.anatomical_sheet_versions where id = pg_temp.id('draft')),
@@ -67,54 +43,58 @@ select is(
1::bigint, 'the seed import is audited as a system action'
);
-- 2. Nobody publishes it by accident: plain users cannot even see the draft
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 0::bigint, 'a private official draft is invisible to plain users');
select throws_ok(
$$ select public.publish_sheet_version(pg_temp.id('draft')) $$,
'42501', null, 'a plain user cannot publish the official draft'
);
-- A platform admin can (the explicit decision step), and the gate would accept the document
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is((select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 1::bigint, 'a platform admin sees the official draft');
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('draft')) $$, 'a platform admin could publish it (rolled back here)');
-- 2. An editor with the capability (aal2) is set up, then the protections.
insert into auth.users (id, email, email_confirmed_at) values
('40000000-0000-0000-0000-000000000001', 'own-eq@test.local', now()),
('40000000-0000-0000-0000-00000000000e', 'ed-eq@test.local', now()),
('40000000-0000-0000-0000-0000000000a0', 'pl-eq@test.local', now());
insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at)
values (gen_random_uuid(), '40000000-0000-0000-0000-00000000000e', 'totp', 'verified', now(), now());
insert into auth.sessions (id, user_id, aal) values ('41000000-0000-0000-0000-00000000000e', '40000000-0000-0000-0000-00000000000e', 'aal2');
select set_config('request.jwt.claims', '', true);
select private.assign_platform_role(null, '40000000-0000-0000-0000-000000000001', 'owner');
select private.assign_platform_role(null, '40000000-0000-0000-0000-00000000000e', 'admin');
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values
('40000000-0000-0000-0000-00000000000e', 'sheet_editor', 'equine work', 'checked', '40000000-0000-0000-0000-000000000001'),
('40000000-0000-0000-0000-00000000000e', 'sheet_publisher', 'equine work', 'checked', '40000000-0000-0000-0000-000000000001');
create function pg_temp.act(p_uid uuid, p_sid uuid, p_aal text) returns void language sql as $$
select set_config('request.jwt.claims', jsonb_build_object('sub',p_uid,'role','authenticated','aal',p_aal,'session_id',p_sid)::text, true);
$$;
grant execute on function pg_temp.act(uuid,uuid,text) to authenticated;
-- 3. Storage: the seed registers metadata only; after the operator upload + confirmation,
-- a platform admin reads the object and a plain user none. A base where the operator
-- already uploaded and confirmed the drawing (DEV) is brought back to the seed's state
-- inside this transaction: the confirmation is cleared, and the object already in the
-- bucket stands for the upload (content-addressed key, never written twice). Reads are
-- counted on the seed's own key: other objects of the bucket do not count.
reset role;
create table pg_temp.seed_objects as
select a.sha256 || '.svg' as name from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet');
grant select on pg_temp.seed_objects to authenticated, service_role;
update public.anatomical_sheet_assets set uploaded_at = null where sheet_id = pg_temp.id('sheet');
insert into storage.objects (bucket_id, name)
select 'anatomical-sheet-assets', k.name from pg_temp.seed_objects k
where not exists (select 1 from storage.objects o where o.bucket_id = 'anatomical-sheet-assets' and o.name = k.name);
select is(
(select count(*) from storage.objects o join pg_temp.seed_objects k on k.name = o.name where o.bucket_id = 'anatomical-sheet-assets'),
1::bigint, 'the drawing is in the bucket once, under its content address'
);
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)),
0::bigint, 'seed objects are not readable before confirmation');
set local role service_role;
select is(
(select count(*) from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet')
and public.confirm_sheet_asset_upload(a.id) = a.id),
1::bigint, 'operator confirms the upload'
select pg_temp.act('40000000-0000-0000-0000-00000000000e', '41000000-0000-0000-0000-00000000000e', 'aal2');
-- The capability holder sees the draft (caller_can_see_official_sheet).
select is((select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 1::bigint,
'a capability holder at aal2 sees the historical draft');
-- Every write path refuses the historical sheet.
select throws_ok(
$$ select public.save_sheet_draft(pg_temp.id('draft'), 0, '{"schema_version":2,"species_id":"equine"}'::jsonb, 'x') $$,
'42501', 'sheet.historical_read_only', 'save refused on the historical sheet'
);
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)),
1::bigint, 'a platform admin reads the official SVG');
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)),
0::bigint, 'a plain user reads no official draft SVG');
select throws_ok(
$$ select public.submit_sheet_review(pg_temp.id('draft'), 0, 'x') $$,
'42501', 'sheet.historical_read_only', 'submit refused on the historical sheet'
);
select throws_ok(
$$ select public.register_sheet_asset(pg_temp.id('sheet'), repeat('e',64), 100, 'x.svg') $$,
'42501', 'sheet.historical_read_only', 'register refused on the historical sheet'
);
select throws_ok(
$$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$,
'42501', 'sheet.historical_read_only', 'archive refused on the historical sheet'
);
select throws_ok(
$$ delete from public.anatomical_sheet_versions where id = pg_temp.id('draft') $$,
'42501', null, 'the historical draft cannot be deleted (trigger)'
);
-- 3. A plain confirmed account sees nothing (no published version, no capability).
select pg_temp.act('40000000-0000-0000-0000-0000000000a0', gen_random_uuid(), 'aal1');
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 0::bigint,
'a plain account does not see the historical sheet (no published version)');
select * from finish();
rollback;