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.
174 lines
11 KiB
PL/PgSQL
174 lines
11 KiB
PL/PgSQL
-- 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(15);
|
|
|
|
-- 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)');
|
|
|
|
-- 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);
|
|
|
|
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','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);
|
|
$$;
|
|
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}');
|
|
|
|
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;
|
|
|
|
-- Reader (catalog_reader, aal1): the published and deprecated drawings only.
|
|
set local role authenticated;
|
|
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'
|
|
);
|
|
|
|
-- 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(
|
|
$$ 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 set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
|
select is(
|
|
(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'
|
|
);
|
|
|
|
select * from finish();
|
|
rollback;
|