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

+155 -137
View File
@@ -1,155 +1,173 @@
-- Storage read policy on bucket anatomical-sheet-assets: an object is readable
-- through a sheet (published reference for users, any version for editors),
-- never through its hash alone. No API writes.
-- Sheet-asset Storage after V1-2.1 (M4, M5): the bucket is private and 2 MiB,
-- the read predicate follows the catalogue rules (contract §9.4), the editor
-- upload INSERT policy exists, no UPDATE/DELETE policy. Rows are seeded
-- directly (table owner) to exercise the policy, not the RPCs (those are 016).
begin;
select plan(28);
select plan(15);
insert into auth.users (id, email) values
('30000000-0000-0000-0000-00000000000a', 'alice-assets@test.local'),
('30000000-0000-0000-0000-00000000000b', 'bob-assets@test.local');
-- Bucket
select is((select public from storage.buckets where id = 'anatomical-sheet-assets'), false, 'the bucket is private');
select is((select file_size_limit from storage.buckets where id = 'anatomical-sheet-assets'), 2097152::bigint, 'the bucket limit is 2 MiB (M5)');
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 $$;
grant all on pg_temp.ids to anon, authenticated, service_role;
grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
-- Policies on storage.objects for the bucket: read + editor insert, nothing else.
select is(
(select count(*) from pg_policies where schemaname='storage' and tablename='objects'
and (coalesce(qual,'')||coalesce(with_check,'')) like '%anatomical-sheet-assets%'),
2::bigint, 'two policies name the bucket (read, editor insert)'
);
select is(
(select count(*) from pg_policies where schemaname='storage' and tablename='objects' and cmd in ('UPDATE','DELETE')
and (coalesce(qual,'')||coalesce(with_check,'')) like '%anatomical-sheet-assets%'),
0::bigint, 'no UPDATE or DELETE policy on the bucket'
);
-- Accounts: owner, editor (admin + capability), reader (catalog_reader), plain.
insert into auth.users (id, email, email_confirmed_at) values
('30000000-0000-0000-0000-000000000001', 'own-st@test.local', now()),
('30000000-0000-0000-0000-00000000000e', 'ed-st@test.local', now()),
('30000000-0000-0000-0000-0000000000d0', 'rd-st@test.local', now()),
('30000000-0000-0000-0000-0000000000d1', 'pl-st@test.local', now());
insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at)
values (gen_random_uuid(), '30000000-0000-0000-0000-00000000000e', 'totp', 'verified', now(), now());
insert into auth.sessions (id, user_id, aal) values
('31000000-0000-0000-0000-000000000001', '30000000-0000-0000-0000-000000000001', 'aal2'),
('31000000-0000-0000-0000-00000000000e', '30000000-0000-0000-0000-00000000000e', 'aal2');
select set_config('request.jwt.claims', '', true);
select private.assign_platform_role(null, '30000000-0000-0000-0000-000000000001', 'owner');
select private.assign_platform_role(null, '30000000-0000-0000-0000-00000000000e', 'admin');
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values
('30000000-0000-0000-0000-00000000000e', 'sheet_editor', 'author', 'checked', '30000000-0000-0000-0000-000000000001'),
('30000000-0000-0000-0000-0000000000d0', 'catalog_reader', 'beta', null, '30000000-0000-0000-0000-000000000001');
-- An official sheet with a published version (asset A), a deprecated version
-- (asset B), and a draft (asset C, confirmed). A and B are referenced by
-- non-draft versions; C only by the draft.
insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status, current_version_id)
values ('32000000-0000-0000-0000-000000000001', 'equine', 'Pub', 'pub-storage', true, 'private', 'active', null);
insert into public.anatomical_sheet_assets (id, sheet_id, sha256, byte_size, file_name, uploaded_at) values
('32000000-0000-0000-0000-00000000000a', '32000000-0000-0000-0000-000000000001', repeat('a',64), 100, 'a.svg', now()),
('32000000-0000-0000-0000-00000000000b', '32000000-0000-0000-0000-000000000001', repeat('b',64), 100, 'b.svg', now()),
('32000000-0000-0000-0000-00000000000c', '32000000-0000-0000-0000-000000000001', repeat('c',64), 100, 'c.svg', now());
-- The content trigger needs an authenticated actor for a non-historical
-- sheet, so the direct version seeding carries the owner's claim.
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-000000000001"}', true);
-- Minimal publishable document with one main view on a given asset.
create function pg_temp.doc(p_asset uuid) returns jsonb language sql as $$
select jsonb_build_object(
'schema_version', 2, 'species_id', 'equine',
'name', jsonb_build_object('fr', 'A', 'en', 'A'),
'description', '{}'::jsonb, 'scope_disclaimer', '{}'::jsonb,
'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null),
'vertebral_config', jsonb_build_object('regions', '[]'::jsonb),
'categories', '[]'::jsonb,
'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);
select jsonb_build_object('schema_version',2,'species_id','equine',
'name',jsonb_build_object('fr','P','en','P'),'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,
'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(uuid) to authenticated, service_role;
insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, document_fingerprint)
values ('33000000-0000-0000-0000-000000000001', '32000000-0000-0000-0000-000000000001', 1, 'published', 2, pg_temp.doc('32000000-0000-0000-0000-00000000000a'), now(), repeat('0',64));
insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, deprecated_at, document_fingerprint)
values ('33000000-0000-0000-0000-000000000002', '32000000-0000-0000-0000-000000000001', 2, 'deprecated', 2, pg_temp.doc('32000000-0000-0000-0000-00000000000b'), now(), now(), repeat('0',64));
insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content)
values ('33000000-0000-0000-0000-000000000003', '32000000-0000-0000-0000-000000000001', 3, 'draft', 2, pg_temp.doc('32000000-0000-0000-0000-00000000000c'));
update public.anatomical_sheets set current_version_id = '33000000-0000-0000-0000-000000000001' where id = '32000000-0000-0000-0000-000000000001';
insert into storage.objects (bucket_id, name, metadata) values
('anatomical-sheet-assets', repeat('a',64)||'.svg', '{"size":100}'),
('anatomical-sheet-assets', repeat('b',64)||'.svg', '{"size":100}'),
('anatomical-sheet-assets', repeat('c',64)||'.svg', '{"size":100}');
-- A fourth object whose row is pending (never confirmed): readable by nobody.
insert into public.anatomical_sheet_assets (id, sheet_id, sha256, byte_size, file_name)
values ('32000000-0000-0000-0000-00000000000d', '32000000-0000-0000-0000-000000000001', repeat('d',64), 100, 'd.svg');
insert into storage.objects (bucket_id, name, metadata) values ('anatomical-sheet-assets', repeat('d',64)||'.svg', '{"size":100}');
-- 1. Bucket contract
select is((select public from storage.buckets where id = 'anatomical-sheet-assets'), false, 'bucket is private');
select is(
(select allowed_mime_types from storage.buckets where id = 'anatomical-sheet-assets'),
array['image/svg+xml'], 'bucket accepts SVG only'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and policyname = 'anatomical_sheet_assets_read' and cmd = 'SELECT'),
1::bigint, 'one SELECT policy on storage.objects for the bucket'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and policyname like 'anatomical_sheet_assets%' and cmd <> 'SELECT'),
0::bigint, 'no write policy on the bucket for API roles'
);
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;
-- 2. Alice: sheet with published v1 on asset A, draft v2 on asset B; C is registered but unreferenced
-- Reader (catalog_reader, aal1): the published and deprecated drawings only.
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into pg_temp.ids select 'sheet', public.create_anatomical_sheet('equine', 'Assets', 'assets-test', pg_temp.doc(null));
insert into pg_temp.ids select 'a', public.register_sheet_asset(pg_temp.id('sheet'), repeat('a', 64), 100, 'A.svg');
insert into pg_temp.ids select 'b', public.register_sheet_asset(pg_temp.id('sheet'), repeat('b', 64), 100, 'B.svg');
insert into pg_temp.ids select 'c', public.register_sheet_asset(pg_temp.id('sheet'), repeat('c', 64), 100, 'C.svg');
insert into pg_temp.ids select 'v1', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet');
update public.anatomical_sheet_versions set content = pg_temp.doc(pg_temp.id('a')) where id = pg_temp.id('v1');
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('v1')) $$, 'v1 published on asset A');
insert into pg_temp.ids select 'v2', public.create_sheet_draft(pg_temp.id('sheet'));
update public.anatomical_sheet_versions set content = pg_temp.doc(pg_temp.id('b')) where id = pg_temp.id('v2');
select pg_temp.act('30000000-0000-0000-0000-0000000000d0', gen_random_uuid(), 'aal1');
select results_eq(
$$ select name from storage.objects where bucket_id='anatomical-sheet-assets' order by name $$,
$$ values (repeat('a',64)||'.svg'), (repeat('b',64)||'.svg') $$,
'a catalogue reader reads the published and deprecated drawings, not the draft or pending ones'
);
-- Objects uploaded by the operator (owner session), then confirmed through the
-- service-role RPC for A and B only; C stays metadata without bytes.
reset role;
insert into storage.objects (bucket_id, name) values
('anatomical-sheet-assets', repeat('a', 64) || '.svg'),
('anatomical-sheet-assets', repeat('b', 64) || '.svg'),
('anatomical-sheet-assets', repeat('c', 64) || '.svg'),
('anatomical-sheet-assets', repeat('d', 64) || '.svg');
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'nothing is readable before the upload is confirmed');
-- Editor at aal2: every uploaded drawing of the official sheet (a, b, c), not the pending d.
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select results_eq(
$$ select name from storage.objects where bucket_id='anatomical-sheet-assets' order by name $$,
$$ values (repeat('a',64)||'.svg'), (repeat('b',64)||'.svg'), (repeat('c',64)||'.svg') $$,
'an editor at aal2 reads every confirmed drawing of the official sheet, not the pending one'
);
-- Editor at aal1 (no live aal2 session): the capability branch does not apply,
-- but a capability holder also has catalogue access, so the reader branch still
-- shows the published and deprecated drawings (not the draft's).
select pg_temp.act('30000000-0000-0000-0000-00000000000e', gen_random_uuid(), 'aal1');
select results_eq(
$$ select name from storage.objects where bucket_id='anatomical-sheet-assets' order by name $$,
$$ values (repeat('a',64)||'.svg'), (repeat('b',64)||'.svg') $$,
'the editor at aal1 falls back to the reader branch: published and deprecated only'
);
-- A plain confirmed account with no right: nothing.
select pg_temp.act('30000000-0000-0000-0000-0000000000d1', gen_random_uuid(), 'aal1');
select is((select count(*) from storage.objects where bucket_id='anatomical-sheet-assets'), 0::bigint,
'a confirmed account without any right reads nothing');
-- The INSERT policy only allows a pending key of one''s own sheet: a confirmed
-- key is refused by its WITH CHECK (no UPDATE/DELETE policy exists at all, per
-- the census above; the runtime refusal of those is a storage-API concern, not
-- RLS, so it is not asserted here).
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select throws_ok(
$$ select public.confirm_sheet_asset_upload(pg_temp.id('a')) $$,
'42501', null, 'an editor cannot confirm an upload (service role only)'
$$ insert into storage.objects (bucket_id, name) values ('anatomical-sheet-assets', repeat('a',64)||'.svg') $$,
'42501', null, 'an editor cannot insert a confirmed (non-pending) object key'
);
-- A fifth asset, pending, with no object yet: the editor uploads its key.
reset role;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-000000000001"}', true);
insert into public.anatomical_sheet_assets (id, sheet_id, sha256, byte_size, file_name)
values ('32000000-0000-0000-0000-00000000000e', '32000000-0000-0000-0000-000000000001', repeat('e',64), 100, 'e.svg');
set local role authenticated;
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select lives_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('anatomical-sheet-assets', repeat('e',64)||'.svg', '{"size":100}') $$,
'an editor uploads the pending key of their own sheet'
);
-- confirm with the storage check: a wrong size is refused, the right one works.
reset role;
update storage.objects set metadata = '{"size":999}' where bucket_id='anatomical-sheet-assets' and name = repeat('d',64)||'.svg';
set local role authenticated;
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select throws_ok(
$$ select public.confirm_sheet_asset_upload('32000000-0000-0000-0000-00000000000d') $$,
'P0001', 'size_mismatch', 'confirm refuses a size that does not match the row (F7)'
);
reset role;
update storage.objects set metadata = '{"size":100}' where bucket_id='anatomical-sheet-assets' and name = repeat('d',64)||'.svg';
set local role authenticated;
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select lives_ok(
$$ select public.confirm_sheet_asset_upload('32000000-0000-0000-0000-00000000000d') $$,
'confirm succeeds when the object exists and the size matches'
);
select is((select uploaded_at is not null from public.anatomical_sheet_assets where id='32000000-0000-0000-0000-00000000000d'),
true, 'the row is now confirmed');
-- The operator read RPC: service role only.
select throws_ok(
$$ select public.get_sheet_asset_state('32000000-0000-0000-0000-00000000000a') $$,
'42501', null, 'an editor cannot call the operator read RPC (EXECUTE is service_role only)'
);
set local role service_role;
select lives_ok($$ select public.confirm_sheet_asset_upload(pg_temp.id('a')) $$, 'service role confirms asset A');
select lives_ok($$ select public.confirm_sheet_asset_upload(pg_temp.id('b')) $$, 'service role confirms asset B');
select is(public.confirm_sheet_asset_upload(pg_temp.id('a')), pg_temp.id('a'), 'confirming twice is idempotent');
reset role;
-- 3. Editor reads every object of her sheet (published, draft, unreferenced), not foreign ones
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'anatomical-sheet-assets' order by name $$,
$$ values (repeat('a', 64) || '.svg'), (repeat('b', 64) || '.svg') $$,
'editor reads the confirmed objects of her sheet; unconfirmed and foreign objects stay hidden'
);
-- 4. Bob: private sheet → nothing; claiming the hash on his own sheet opens nothing
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'other user reads nothing from a private sheet');
insert into pg_temp.ids select 'bob-sheet', public.create_anatomical_sheet('equine', 'Claim', 'claim-test', pg_temp.doc(null));
select lives_ok(
$$ select public.register_sheet_asset(pg_temp.id('bob-sheet'), repeat('a', 64), 100, 'A.svg') $$,
'bob registers the known hash on his own sheet'
);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'a self-registered hash without a confirmed upload opens no object');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.set_sheet_visibility(pg_temp.id('sheet'), 'public') $$, 'alice makes the sheet public');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'anatomical-sheet-assets' order by name $$,
$$ values (repeat('a', 64) || '.svg') $$,
'other user reads only the object a published version references (not the draft asset, not the unreferenced one)'
);
select throws_ok(
$$ insert into storage.objects (bucket_id, name) values ('anatomical-sheet-assets', 'x.svg') $$,
'42501', null, 'authenticated cannot upload into the bucket'
);
-- The real image also carries storage.protect_delete (statement trigger), so a
-- direct DELETE is asserted through the policy census, not by running it.
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and cmd in ('INSERT', 'UPDATE', 'DELETE')
and (coalesce(qual, '') like '%anatomical-sheet-assets%' or coalesce(with_check, '') like '%anatomical-sheet-assets%')),
0::bigint, 'no API role has an insert/update/delete policy on the bucket objects'
(select public.get_sheet_asset_state('32000000-0000-0000-0000-00000000000a') ->> 'sha256'),
repeat('a',64), 'the operator read RPC returns the asset state for the service role'
);
-- Name variants never match the strict key
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select ok(
not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', repeat('a', 64))
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', upper(repeat('a', 64)) || '.svg')
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', 'x/' || repeat('a', 64) || '.svg')
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', repeat('a', 64) || '.svg/'),
'object name must be exactly <sha256>.svg'
);
-- A deprecated version still publishes its asset
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('v2')) $$, 'v2 (asset B) published');
select lives_ok($$ select public.deprecate_sheet_version(pg_temp.id('v1'), 'old') $$, 'v1 (asset A) deprecated');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'anatomical-sheet-assets' order by name $$,
$$ values (repeat('a', 64) || '.svg'), (repeat('b', 64) || '.svg') $$,
'published and deprecated references both open the object for users of a public sheet'
);
-- 5. Archived sheet hides objects from others; anon sees nothing
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice archives');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'archived sheet: others read nothing');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.restore_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice restores');
select lives_ok($$ select public.delete_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice soft-deletes the sheet');
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'deleted sheet: even the creator reads nothing');
set local role anon;
select set_config('request.jwt.claims', '', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'anon reads nothing');
select * from finish();
rollback;