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.
163 lines
7.9 KiB
PL/PgSQL
163 lines
7.9 KiB
PL/PgSQL
-- Platform roles: bootstrap, AR-1 B (live aal2 session required), delegation
|
|
-- rules, every denied path, visibility. Since V1-2.1 (M4) every role change
|
|
-- needs an aal2 session that still exists in auth.sessions and, for a grant,
|
|
-- a verified TOTP factor on the target. Full AR-1 matrix: 015.
|
|
begin;
|
|
select plan(25);
|
|
|
|
insert into auth.users (id, email, email_confirmed_at) values
|
|
('00000000-0000-0000-0000-000000000001', 'root@test.local', now()),
|
|
('00000000-0000-0000-0000-00000000000a', 'alice@test.local', now()),
|
|
('00000000-0000-0000-0000-00000000000b', 'bob@test.local', now()),
|
|
('00000000-0000-0000-0000-00000000000c', 'carol@test.local', now());
|
|
|
|
-- A verified TOTP factor for every account that will receive a role, except
|
|
-- carol, who gets one only after the factor-missing refusal is proven.
|
|
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 ('root@test.local', 'alice@test.local', 'bob@test.local');
|
|
|
|
-- aal2 sessions for the callers (root, alice); one aal1 session for root.
|
|
insert into auth.sessions (id, user_id, aal, not_after) values
|
|
('a0000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000001', 'aal2', null),
|
|
('a0000000-0000-0000-0000-00000000000a', '00000000-0000-0000-0000-00000000000a', 'aal2', null),
|
|
('a0000000-0000-0000-0000-00000000000b', '00000000-0000-0000-0000-00000000000b', 'aal2', null),
|
|
('b0000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000001', 'aal1', null);
|
|
|
|
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);
|
|
$$;
|
|
|
|
-- 1. Bootstrap the first owner from the operator path (no JWT at all).
|
|
select set_config('request.jwt.claims', '', true);
|
|
select ok(not private.is_platform_owner('00000000-0000-0000-0000-000000000001'), 'fresh account has no owner role');
|
|
select lives_ok(
|
|
$$ select private.assign_platform_role(null, '00000000-0000-0000-0000-000000000001', 'owner') $$,
|
|
'operator bootstraps the first owner (actor null, no JWT)'
|
|
);
|
|
select ok(private.is_platform_owner('00000000-0000-0000-0000-000000000001'), 'root is owner');
|
|
select ok(private.is_platform_admin('00000000-0000-0000-0000-000000000001'), 'owner counts as admin');
|
|
|
|
-- 2. AR-1 B: an aal1 owner session is refused before any role check.
|
|
set local role authenticated;
|
|
select pg_temp.act('00000000-0000-0000-0000-000000000001', 'b0000000-0000-0000-0000-000000000001', 'aal1');
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin') $$,
|
|
'42501', 'mfa_required', 'an aal1 owner session cannot grant a role'
|
|
);
|
|
|
|
-- 3. Owner at aal2 grants admin to alice (who holds a verified factor).
|
|
select pg_temp.act('00000000-0000-0000-0000-000000000001', 'a0000000-0000-0000-0000-000000000001', 'aal2');
|
|
select lives_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin') $$,
|
|
'owner at aal2 grants admin'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin') $$,
|
|
'P0001', 'role_active', 'granting an active role twice is refused'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-000000000001', 'admin') $$,
|
|
'42501', 'self_target', 'owner cannot change their own roles'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role(gen_random_uuid(), 'support') $$,
|
|
'P0001', 'target_invalid', 'unknown target is refused'
|
|
);
|
|
|
|
-- 4. A grant to a target with no verified factor is refused (AR-1 B).
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000c', 'support') $$,
|
|
'P0001', 'target_factor_missing', 'a grant to an account with no verified factor is refused'
|
|
);
|
|
reset role;
|
|
insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at)
|
|
values (gen_random_uuid(), '00000000-0000-0000-0000-00000000000c', 'totp', 'verified', now(), now());
|
|
set local role authenticated;
|
|
select pg_temp.act('00000000-0000-0000-0000-000000000001', 'a0000000-0000-0000-0000-000000000001', 'aal2');
|
|
|
|
-- 5. Alice (admin) at aal2 may delegate moderator/support, nothing above.
|
|
select pg_temp.act('00000000-0000-0000-0000-00000000000a', 'a0000000-0000-0000-0000-00000000000a', 'aal2');
|
|
select lives_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000b', 'moderator') $$,
|
|
'admin grants moderator'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000b', 'admin') $$,
|
|
'42501', 'forbidden', 'admin cannot grant admin'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000b', 'owner') $$,
|
|
'42501', 'forbidden', 'admin cannot grant owner'
|
|
);
|
|
|
|
-- 6. Direct writes are impossible for the API role.
|
|
select throws_ok(
|
|
$$ insert into public.profile_roles (user_id, role) values ('00000000-0000-0000-0000-00000000000a', 'owner') $$,
|
|
'42501', null, 'admin cannot insert a role row directly'
|
|
);
|
|
select throws_ok(
|
|
$$ update public.profile_roles set role = 'owner' where user_id = '00000000-0000-0000-0000-00000000000a' $$,
|
|
'42501', null, 'admin cannot update role rows directly'
|
|
);
|
|
select throws_ok(
|
|
$$ delete from public.profile_roles $$,
|
|
'42501', null, 'admin cannot delete role rows directly'
|
|
);
|
|
|
|
-- 7. service_role cannot write the table nor execute the RPCs (F17).
|
|
set local role service_role;
|
|
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
|
select ok(not has_function_privilege('service_role', 'public.grant_platform_role(uuid, public.platform_role)', 'execute'),
|
|
'service_role cannot execute grant_platform_role');
|
|
select throws_ok(
|
|
$$ insert into public.profile_roles (user_id, role) values ('00000000-0000-0000-0000-00000000000c', 'owner') $$,
|
|
'42501', null, 'service_role cannot insert a role row directly'
|
|
);
|
|
|
|
-- 8. Bob (moderator) at aal2 sees only his own grant; carol sees none.
|
|
set local role authenticated;
|
|
select pg_temp.act('00000000-0000-0000-0000-00000000000b', 'a0000000-0000-0000-0000-00000000000b', 'aal2');
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000c', 'support') $$,
|
|
'42501', 'forbidden', 'moderator at aal2 cannot grant any role'
|
|
);
|
|
select results_eq(
|
|
$$ select user_id, role::text from public.profile_roles $$,
|
|
$$ values ('00000000-0000-0000-0000-00000000000b'::uuid, 'moderator'::text) $$,
|
|
'moderator sees only his own grant rows'
|
|
);
|
|
|
|
-- 9. Anonymous
|
|
set local role anon;
|
|
select set_config('request.jwt.claims', '', true);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000c', 'support') $$,
|
|
'42501', null, 'anon cannot call grant_platform_role'
|
|
);
|
|
|
|
-- 10. Revocation at aal2 and deactivation.
|
|
set local role authenticated;
|
|
select pg_temp.act('00000000-0000-0000-0000-00000000000a', 'a0000000-0000-0000-0000-00000000000a', 'aal2');
|
|
select lives_ok(
|
|
$$ select public.revoke_platform_role('00000000-0000-0000-0000-00000000000b', 'moderator', 'test') $$,
|
|
'admin revokes moderator at aal2'
|
|
);
|
|
select ok(not private.is_platform_moderator('00000000-0000-0000-0000-00000000000b'), 'revoked role no longer counts');
|
|
reset role;
|
|
update public.profiles set active = false where id = '00000000-0000-0000-0000-00000000000a';
|
|
select ok(not private.is_platform_admin('00000000-0000-0000-0000-00000000000a'), 'deactivated account loses its roles');
|
|
|
|
-- 11. Last-owner guard on the system path (operator path: no JWT at all).
|
|
update public.profile_roles set revoked_at = now()
|
|
where role = 'owner' and revoked_at is null and user_id <> '00000000-0000-0000-0000-000000000001';
|
|
select set_config('request.jwt.claims', '', true);
|
|
select throws_ok(
|
|
$$ select private.withdraw_platform_role(null, '00000000-0000-0000-0000-000000000001', 'owner', 'oops') $$,
|
|
'P0001', 'last_owner', 'the last owner cannot be revoked'
|
|
);
|
|
|
|
select * from finish();
|
|
rollback;
|