Files
faunosteo/supabase/tests/004_audit.sql
T
bchanot 77e5fa4cf5 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.
2026-10-28 15:52:49 +01:00

114 lines
4.5 KiB
PL/PgSQL

-- audit.audit_logs: sensitive operations leave a row, rows are immutable,
-- only owners/admins read them.
begin;
select plan(12);
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());
-- AR-1 B: the owner needs a live aal2 session, the target a verified factor.
insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at)
values (gen_random_uuid(), '00000000-0000-0000-0000-00000000000a', 'totp', 'verified', now(), now());
insert into auth.sessions (id, user_id, aal) values
('a0000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000001', 'aal2');
select set_config('request.jwt.claims', '', true);
select private.assign_platform_role(null, '00000000-0000-0000-0000-000000000001', 'owner');
-- 1. Bootstrap is audited as a system action
select results_eq(
$$ select actor_user_id, action, resource_type, target_user_id, metadata ->> 'role'
from audit.audit_logs where target_user_id = '00000000-0000-0000-0000-000000000001' $$,
$$ values (null::uuid, 'platform_role.granted'::text, 'profile_role'::text,
'00000000-0000-0000-0000-000000000001'::uuid, 'owner'::text) $$,
'bootstrap grant logged with null actor'
);
-- 2. RPC grant and revoke are audited with the actor
set local role authenticated;
select set_config('request.jwt.claims',
'{"sub":"00000000-0000-0000-0000-000000000001","role":"authenticated","aal":"aal2","session_id":"a0000000-0000-0000-0000-000000000001"}', true);
select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin');
select public.revoke_platform_role('00000000-0000-0000-0000-00000000000a', 'admin', 'rotation');
reset role;
select results_eq(
$$ select action, actor_user_id, target_user_id, metadata ->> 'reason'
from audit.audit_logs where actor_user_id = '00000000-0000-0000-0000-000000000001'
order by occurred_at, action $$,
$$ values ('platform_role.granted'::text, '00000000-0000-0000-0000-000000000001'::uuid,
'00000000-0000-0000-0000-00000000000a'::uuid, null::text),
('platform_role.revoked'::text, '00000000-0000-0000-0000-000000000001'::uuid,
'00000000-0000-0000-0000-00000000000a'::uuid, 'rotation'::text) $$,
'grant and revoke logged with actor, target and metadata'
);
select is(
(select count(*) from audit.audit_logs a
join public.profile_roles r on r.id = a.resource_id
where a.target_user_id in ('00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-00000000000a')),
3::bigint,
'every fixture audit row points at its profile_roles row'
);
-- 3. Append-only, even for the privileged owner session
select throws_ok(
$$ update audit.audit_logs set action = 'platform_role.tampered' $$,
'42501',
null,
'audit rows cannot be updated'
);
select throws_ok(
$$ delete from audit.audit_logs $$,
'42501',
null,
'audit rows cannot be deleted'
);
-- 4. log_event validates its inputs
select throws_ok(
$$ select private.log_event(null, 'Bad Action', 'profile') $$,
'23514',
null,
'action must match <domain>.<verb>'
);
select throws_ok(
$$ select private.log_event(null, 'export.requested', 'profile', null, null, null, '[]'::jsonb) $$,
'23514',
null,
'metadata must be a JSON object'
);
-- 5. Read access: owner yes, plain user no, anon no, nobody writes through the API
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is(
(select count(*) from audit.audit_logs
where target_user_id in ('00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-00000000000a')),
3::bigint,
'owner reads the audit log'
);
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from audit.audit_logs), 0::bigint, 'plain user reads nothing');
select throws_ok(
$$ insert into audit.audit_logs (action, resource_type) values ('x.y', 'z') $$,
'42501',
null,
'authenticated cannot insert audit rows'
);
select throws_ok(
$$ select private.log_event(null, 'x.y', 'z') $$,
'42501',
null,
'authenticated cannot call log_event'
);
set local role anon;
select throws_ok(
$$ select * from audit.audit_logs $$,
'42501',
null,
'anon cannot read the audit log'
);
select * from finish();
rollback;