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:
1 parent
9c13bae4bc
commit
77e5fa4cf5
33 files changed
+4613
-1617
No files matched your search
@@ -0,0 +1,204 @@
|
||||
-- V1-2.1 M4: AR-1 B (MFA on every platform role change) and catalogue rights.
|
||||
-- Contract docs/development/V1-2_ANATOMICAL_SHEETS.md §5.2, §5.7, §15.
|
||||
-- The aal2 session check reads auth.sessions; here those rows are fabricated,
|
||||
-- as the shim allows. Real GoTrue MFA is proven in tests/sandbox/mfa.sandbox.ts,
|
||||
-- the F18 race in tests/sandbox (two sessions).
|
||||
begin;
|
||||
select plan(36);
|
||||
|
||||
insert into auth.users (id, email, email_confirmed_at) values
|
||||
('d0000000-0000-0000-0000-000000000001', 'own@t.local', now()), -- owner
|
||||
('d0000000-0000-0000-0000-00000000000a', 'adm@t.local', now()), -- admin
|
||||
('d0000000-0000-0000-0000-00000000000b', 'mod@t.local', now()), -- moderator
|
||||
('d0000000-0000-0000-0000-00000000000c', 'noface@t.local', now()), -- no factor
|
||||
('d0000000-0000-0000-0000-00000000000e', 'editor@t.local', now()), -- capability holder
|
||||
('d0000000-0000-0000-0000-0000000000aa', 'att@t.local', now()), -- fresh grant target
|
||||
('d0000000-0000-0000-0000-00000000000f', 'rev@t.local', now()); -- reviewer (non-admin)
|
||||
|
||||
insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at)
|
||||
select gen_random_uuid(), id, 'totp', 'verified', now(), now()
|
||||
from auth.users where email in ('own@t.local', 'adm@t.local', 'mod@t.local', 'editor@t.local', 'att@t.local', 'rev@t.local');
|
||||
|
||||
insert into auth.sessions (id, user_id, aal) values
|
||||
('d1000000-0000-0000-0000-000000000001', 'd0000000-0000-0000-0000-000000000001', 'aal2'),
|
||||
('d2000000-0000-0000-0000-000000000001', 'd0000000-0000-0000-0000-000000000001', 'aal1'),
|
||||
('d1000000-0000-0000-0000-00000000000a', 'd0000000-0000-0000-0000-00000000000a', 'aal2'),
|
||||
('d1000000-0000-0000-0000-00000000000b', 'd0000000-0000-0000-0000-00000000000b', 'aal2'),
|
||||
('d1000000-0000-0000-0000-00000000000e', 'd0000000-0000-0000-0000-00000000000e', 'aal2');
|
||||
|
||||
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);
|
||||
$$;
|
||||
|
||||
-- Bootstrap owner (operator path), admin, moderator.
|
||||
select set_config('request.jwt.claims', '', true);
|
||||
select private.assign_platform_role(null, 'd0000000-0000-0000-0000-000000000001', 'owner');
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
select public.grant_platform_role('d0000000-0000-0000-0000-00000000000a', 'admin');
|
||||
select public.grant_platform_role('d0000000-0000-0000-0000-00000000000b', 'moderator');
|
||||
|
||||
-- === AR-1 B matrix ========================================================
|
||||
-- aal1, every role, refused with mfa_required before any role check.
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd2000000-0000-0000-0000-000000000001', 'aal1');
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000c','support') $$,
|
||||
'42501', 'mfa_required', 'aal1 owner refused (mfa_required)');
|
||||
-- a token whose aal is missing / null / wrongly cased is not aal2
|
||||
select set_config('request.jwt.claims',
|
||||
'{"sub":"d0000000-0000-0000-0000-000000000001","role":"authenticated","session_id":"d1000000-0000-0000-0000-000000000001"}', true);
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000c','support') $$,
|
||||
'42501', 'mfa_required', 'absent aal claim refused');
|
||||
select set_config('request.jwt.claims',
|
||||
'{"sub":"d0000000-0000-0000-0000-000000000001","role":"authenticated","aal":"AAL2","session_id":"d1000000-0000-0000-0000-000000000001"}', true);
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000c','support') $$,
|
||||
'42501', 'mfa_required', 'wrongly cased aal refused');
|
||||
-- aal2 but the session_id names no live aal2 session row
|
||||
select set_config('request.jwt.claims',
|
||||
'{"sub":"d0000000-0000-0000-0000-000000000001","role":"authenticated","aal":"aal2","session_id":"d9000000-0000-0000-0000-000000000009"}', true);
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000c','support') $$,
|
||||
'42501', 'mfa_required', 'aal2 claim with no live session refused');
|
||||
|
||||
-- aal2 owner grants any role.
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
select lives_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000e','support') $$, 'owner aal2 grants support');
|
||||
select lives_ok($$ select public.revoke_platform_role('d0000000-0000-0000-0000-00000000000e','support','done') $$, 'owner aal2 revokes support');
|
||||
|
||||
-- aal2 admin: moderator/support only.
|
||||
select pg_temp.act('d0000000-0000-0000-0000-00000000000a', 'd1000000-0000-0000-0000-00000000000a', 'aal2');
|
||||
select lives_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000e','support') $$, 'admin aal2 grants support');
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000e','admin') $$,
|
||||
'42501', 'forbidden', 'admin cannot grant admin');
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000e','owner') $$,
|
||||
'42501', 'forbidden', 'admin cannot grant owner');
|
||||
|
||||
-- aal2 moderator: nothing.
|
||||
select pg_temp.act('d0000000-0000-0000-0000-00000000000b', 'd1000000-0000-0000-0000-00000000000b', 'aal2');
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000c','support') $$,
|
||||
'42501', 'forbidden', 'moderator at aal2 cannot grant (aal2 is not sufficient)');
|
||||
|
||||
-- deactivated owner refused.
|
||||
reset role;
|
||||
update public.profiles set active = false where id = 'd0000000-0000-0000-0000-000000000001';
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000c','support') $$,
|
||||
'42501', 'forbidden', 'a deactivated owner is not a manager');
|
||||
reset role; update public.profiles set active = true where id = 'd0000000-0000-0000-0000-000000000001'; set local role authenticated;
|
||||
|
||||
-- grant to a target with no verified factor refused.
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-00000000000c','support') $$,
|
||||
'P0001', 'target_factor_missing', 'grant to a factorless target refused');
|
||||
|
||||
-- The regression chain: an aal1 owner session grants owner to a fresh account
|
||||
-- that never received anything; no row, no audit row.
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd2000000-0000-0000-0000-000000000001', 'aal1');
|
||||
select throws_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-0000000000aa','owner') $$,
|
||||
'42501', 'mfa_required', 'the escalation chain is blocked at step 3');
|
||||
reset role;
|
||||
select is((select count(*) from public.profile_roles where user_id='d0000000-0000-0000-0000-0000000000aa'),
|
||||
0::bigint, 'no role row written for the attacker');
|
||||
select is((select count(*) from audit.audit_logs where target_user_id='d0000000-0000-0000-0000-0000000000aa'),
|
||||
0::bigint, 'no audit row for the refused grant');
|
||||
|
||||
-- The private write functions are not API-callable (EXECUTE revoked), and their
|
||||
-- actor rule is exercised as the table owner: a non-null actor at aal1, and a
|
||||
-- null actor from any session (API role set), are refused.
|
||||
select set_config('request.jwt.claims',
|
||||
'{"sub":"d0000000-0000-0000-0000-000000000001","role":"authenticated","aal":"aal1","session_id":"d2000000-0000-0000-0000-000000000001"}', true);
|
||||
select throws_ok($$ select private.assign_platform_role('d0000000-0000-0000-0000-000000000001','d0000000-0000-0000-0000-0000000000aa','support') $$,
|
||||
'42501', 'mfa_required', 'private writer: non-null actor at aal1 refused');
|
||||
select throws_ok($$ select private.assign_platform_role(null,'d0000000-0000-0000-0000-0000000000aa','support') $$,
|
||||
'42501', 'operator_path_only', 'private writer: null actor from an API session refused');
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select throws_ok($$ select private.assign_platform_role(null,'d0000000-0000-0000-0000-0000000000aa','support') $$,
|
||||
'42501', 'operator_path_only', 'private writer: null actor as service_role refused');
|
||||
|
||||
-- Role read from the table every call: a revoked role is refused at once.
|
||||
select set_config('request.jwt.claims', '', true);
|
||||
select private.assign_platform_role(null, 'd0000000-0000-0000-0000-00000000000e', 'admin');
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-00000000000e', 'd1000000-0000-0000-0000-00000000000e', 'aal2');
|
||||
select lives_ok($$ select public.grant_platform_role('d0000000-0000-0000-0000-0000000000aa','support') $$,
|
||||
'the new admin (aal2) grants support to a factored account');
|
||||
reset role;
|
||||
update public.profile_roles set revoked_at = now() where user_id='d0000000-0000-0000-0000-00000000000e' and role='admin' and revoked_at is null;
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-00000000000e', 'd1000000-0000-0000-0000-00000000000e', 'aal2');
|
||||
select throws_ok($$ select public.revoke_platform_role('d0000000-0000-0000-0000-0000000000aa','support','x') $$,
|
||||
'42501', 'forbidden', 'the revoked admin is refused at the next call, same token');
|
||||
|
||||
-- An account holding a factor but no role is no admin.
|
||||
select ok(not private.is_platform_admin('d0000000-0000-0000-0000-00000000000c'), 'a factor alone grants no role');
|
||||
|
||||
-- === Catalogue rights =====================================================
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
-- owner grants editor (needs an admin target, a note, a verified factor)
|
||||
select lives_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000a','sheet_editor','needs to author','DVM checked') $$,
|
||||
'owner grants sheet_editor to an admin with a note');
|
||||
select throws_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000a','sheet_editor','again','x') $$,
|
||||
'P0001', 'grant.active', 'one active grant per account and kind');
|
||||
-- editor/publisher need a platform admin target
|
||||
select throws_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000f','sheet_editor','no role','x') $$,
|
||||
'P0001', 'grant.ineligible', 'sheet_editor needs a platform admin target');
|
||||
-- a capability needs a verification note
|
||||
select throws_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000f','sheet_reviewer','partner') $$,
|
||||
'P0001', 'invalid_input', 'a capability needs a verification note');
|
||||
-- reviewer may be a non-admin with a factor
|
||||
select lives_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000f','sheet_reviewer','partner vet','DVM verified', now()+interval '90 days') $$,
|
||||
'reviewer granted to a non-admin confirmed account with a factor');
|
||||
-- catalog_reader needs no note and no factor
|
||||
select lives_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000c','catalog_reader','beta') $$,
|
||||
'catalog_reader needs no note or factor');
|
||||
-- the owner cannot grant itself sheet_reviewer
|
||||
select throws_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-000000000001','sheet_reviewer','self','x') $$,
|
||||
'P0001', 'grant.self_review', 'the owner cannot review its own drafts');
|
||||
|
||||
-- grant_active reflects eligibility, expiry and revocation (checked as the
|
||||
-- table owner: grant_active is internal, not granted to any API role).
|
||||
reset role;
|
||||
select ok(private.grant_active('d0000000-0000-0000-0000-00000000000f','sheet_reviewer'), 'an active grant is active');
|
||||
-- An expired grant is inactive (inserted directly with a past expiry: the RPC
|
||||
-- refuses a past expiry and the guard forbids editing a live row).
|
||||
insert into auth.users (id, email, email_confirmed_at)
|
||||
values ('d0000000-0000-0000-0000-0000000000ef', 'expired@t.local', now());
|
||||
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, granted_by, granted_at, expires_at)
|
||||
values ('d0000000-0000-0000-0000-0000000000ef', 'catalog_reader', 'x', 'd0000000-0000-0000-0000-000000000001', now() - interval '2 days', now() - interval '1 day');
|
||||
select ok(not private.grant_active('d0000000-0000-0000-0000-0000000000ef','catalog_reader'), 'an expired grant is inactive');
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
-- A revoked grant is inactive at the next read.
|
||||
select lives_ok($$ select public.revoke_catalog_right(
|
||||
(select id from public.anatomical_catalog_grants where user_id='d0000000-0000-0000-0000-00000000000c' and grant_kind='catalog_reader'),
|
||||
'no longer a beta tester') $$, 'owner revokes the reader grant');
|
||||
reset role;
|
||||
select ok(not private.grant_active('d0000000-0000-0000-0000-00000000000c','catalog_reader'), 'a revoked grant is inactive');
|
||||
set local role authenticated;
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
|
||||
-- only the owner (aal2) lists grants
|
||||
select ok((select jsonb_typeof(public.list_catalog_grants()) = 'array'), 'owner lists grants');
|
||||
select pg_temp.act('d0000000-0000-0000-0000-00000000000a', 'd1000000-0000-0000-0000-00000000000a', 'aal2');
|
||||
select throws_ok($$ select public.list_catalog_grants() $$, '42501', 'rights.owner', 'an admin cannot list grants');
|
||||
-- an aal1 owner cannot touch rights
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd2000000-0000-0000-0000-000000000001', 'aal1');
|
||||
select throws_ok($$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000c','catalog_reader','x') $$,
|
||||
'42501', 'rights.mfa', 'rights need a live aal2 session');
|
||||
|
||||
-- direct writes to the grants table are impossible
|
||||
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
|
||||
select throws_ok(
|
||||
$$ insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, granted_by)
|
||||
values ('d0000000-0000-0000-0000-00000000000c','catalog_reader','x','d0000000-0000-0000-0000-000000000001') $$,
|
||||
'42501', null, 'no direct insert into the grants table');
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
Reference in new issue
Block a user