Files
faunosteo/supabase/tests/015_catalog_access.sql
T
bchanot 51225cacf6 fix(app): grant expiry ends the chosen day in Paris; species order 0..9999 (V1-2.5 L2)
Owner checkpoint 2026-11-02: expiry 23:59:59.999 Europe/Paris (picker, validation, display), tests under non-Paris zones and DST days; M7 redefines create_species and update_inactive_species with the range check; pgTAP 015 and 022; « Relecteur ou relectrice »; object_conflict EN; setup key selectable, no Copy.
2026-11-02 21:29:33 +01:00

262 lines
19 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(44);
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');
-- Expiry instants (owner 2026-11-02, Europe/Paris end of day): the stored
-- instant is the same whatever the session TimeZone; the DST days are future
-- ones the RPC accepts. One fresh account per grant (one active row per kind).
reset role;
insert into auth.users (id, email, email_confirmed_at) values
('d0000000-0000-0000-0000-0000000000b1', 'tz1@t.local', now()),
('d0000000-0000-0000-0000-0000000000b2', 'tz2@t.local', now()),
('d0000000-0000-0000-0000-0000000000b3', 'tz3@t.local', now()),
('d0000000-0000-0000-0000-0000000000b4', 'tz4@t.local', now()),
('d0000000-0000-0000-0000-0000000000b5', 'st1@t.local', now()),
('d0000000-0000-0000-0000-0000000000b6', 'st2@t.local', now()),
('d0000000-0000-0000-0000-0000000000b7', 'st3@t.local', now());
set local role authenticated;
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
set local timezone to 'UTC';
select public.grant_catalog_right('d0000000-0000-0000-0000-0000000000b1', 'catalog_reader', 'tz', null, '2027-03-28T23:59:59.999+02:00');
select public.grant_catalog_right('d0000000-0000-0000-0000-0000000000b2', 'catalog_reader', 'tz', null, '2027-10-31T23:59:59.999+01:00');
set local timezone to 'America/New_York';
select public.grant_catalog_right('d0000000-0000-0000-0000-0000000000b3', 'catalog_reader', 'tz', null, '2027-03-28T23:59:59.999+02:00');
select public.grant_catalog_right('d0000000-0000-0000-0000-0000000000b4', 'catalog_reader', 'tz', null, '2027-10-31T23:59:59.999+01:00');
reset role;
select is((select string_agg(to_char(expires_at at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS'), ',' order by user_id)
from public.anatomical_catalog_grants where user_id in (
'd0000000-0000-0000-0000-0000000000b1', 'd0000000-0000-0000-0000-0000000000b2')),
'2027-03-28T21:59:59.999,2027-10-31T22:59:59.999', 'the 2027 DST days store the same instants under UTC');
select is((select string_agg(to_char(expires_at at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS'), ',' order by user_id)
from public.anatomical_catalog_grants where user_id in (
'd0000000-0000-0000-0000-0000000000b3', 'd0000000-0000-0000-0000-0000000000b4')),
'2027-03-28T21:59:59.999,2027-10-31T22:59:59.999', 'the 2027 DST days store the same instants under America/New_York');
select is((select count(*)::int from public.anatomical_catalog_grants where user_id in (
'd0000000-0000-0000-0000-0000000000b1', 'd0000000-0000-0000-0000-0000000000b2')
and expires_at in ('2027-03-28T21:59:59.999Z'::timestamptz, '2027-10-31T22:59:59.999Z'::timestamptz)), 2,
'the stored instants equal the Paris end of day (summer +02:00, winter +01:00)');
set local timezone to 'UTC';
-- The list state at each boundary (direct inserts with an earlier granted_at).
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, granted_by, granted_at, expires_at) values
('d0000000-0000-0000-0000-0000000000b5', 'catalog_reader', 'x', 'd0000000-0000-0000-0000-000000000001', now() - interval '2 days', now() - interval '1 minute'),
('d0000000-0000-0000-0000-0000000000b6', 'catalog_reader', 'x', 'd0000000-0000-0000-0000-000000000001', now() - interval '2 days', now() + interval '1 minute'),
('d0000000-0000-0000-0000-0000000000b7', 'catalog_reader', 'x', 'd0000000-0000-0000-0000-000000000001', now() - interval '2 days', now() + interval '30 days 1 minute');
set local role authenticated;
select pg_temp.act('d0000000-0000-0000-0000-000000000001', 'd1000000-0000-0000-0000-000000000001', 'aal2');
create function pg_temp.grant_state(p_user uuid) returns text language sql stable as $$
select (e ->> 'state') || '/' || (e ->> 'effective')
from jsonb_array_elements(public.list_catalog_grants()) e where (e ->> 'user_id')::uuid = p_user;
$$;
grant execute on function pg_temp.grant_state(uuid) to authenticated;
select is(pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b5'), 'expired/false', 'expiry 1 minute ago: expired, not effective');
select is(pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b6'), 'expiring/true', 'expiry in 1 minute: expiring, effective');
select is(pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b7'), 'active/true', 'expiry in 30 days and 1 minute: active, effective');
select is(pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b3') like 'expiring/%' or pg_temp.grant_state('d0000000-0000-0000-0000-0000000000b3') like 'active/%', true, 'a future DST-day grant is listed live');
-- 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;