Files
faunosteo/supabase/tests/015_catalog_access.sql
T
bchanot b1de87b5d0 fix(supabase): V1-2.1 owner self-review grant and the text size measure
M6 option A: grant_catalog_right no longer refuses an owner granting
itself sheet_reviewer; authors stay refused at acknowledgment, approval
and publication. The document cap is the UTF-8 bytes of the JSON text:
check_document_structure drops pg_column_size and M5 replaces the base
table CHECK by octet_length(content::text). pgTAP 015 and 017 cover the
owner-reviewer journey and cap - 1, cap, cap + 1, multi-byte, dense.
2026-10-28 19:32:57 +01:00

211 lines
14 KiB
PL/PgSQL

-- 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 and the F18 race (two sessions) are
-- proven in packages/supabase/tests/sandbox/sheets.sandbox.ts, the seed owner's
-- factors on an upgraded stack in seed-owner-mfa.sandbox.ts.
begin;
select plan(37);
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');
-- a capability never goes to an account without a verified factor (§5.2)
select throws_ok(
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000c','sheet_reviewer','partner','DVM verified') $$,
'P0001', 'grant.ineligible', 'a capability is refused to an account without a verified factor');
-- the owner (aal2, verified factor) may grant itself sheet_reviewer (M6 option A);
-- the separation of duties is per round (017)
select lives_ok(
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-000000000001','sheet_reviewer','self','x') $$,
'the owner grants itself sheet_reviewer');
-- 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;