feat(supabase): foundation migrations (audit, profiles, platform roles) + pgTAP suite
- schemas private/audit, set_updated_at, append-only audit.audit_logs, private.log_event - profiles created by auth.users trigger; own-row RLS; column-level UPDATE grant - platform_role enum (owner/admin/moderator/support), profile_roles history table, is_platform_* helpers, grant/revoke RPCs audited; writes only via RPC - config.toml (public schema only, strong passwords, no edge runtime), local seed - 79 pgTAP checks: allowed and denied paths for every policy
This commit is contained in:
1 parent
62c5a3ac86
commit
2d5ff0c33e
10 files changed
+1142
No files matched your search
@@ -0,0 +1,53 @@
|
||||
-- Schema contract: objects exist, RLS is on, API roles start from zero.
|
||||
begin;
|
||||
select plan(22);
|
||||
|
||||
-- Schemas
|
||||
select has_schema('private', 'schema private exists');
|
||||
select has_schema('audit', 'schema audit exists');
|
||||
|
||||
-- Tables
|
||||
select has_table('public', 'profiles', 'table public.profiles exists');
|
||||
select has_table('public', 'profile_roles', 'table public.profile_roles exists');
|
||||
select has_table('audit', 'audit_logs', 'table audit.audit_logs exists');
|
||||
|
||||
-- RLS enabled everywhere (pgTAP has no helper: read pg_class)
|
||||
select ok(
|
||||
(select relrowsecurity from pg_class where oid = 'public.profiles'::regclass),
|
||||
'RLS enabled on profiles'
|
||||
);
|
||||
select ok(
|
||||
(select relrowsecurity from pg_class where oid = 'public.profile_roles'::regclass),
|
||||
'RLS enabled on profile_roles'
|
||||
);
|
||||
select ok(
|
||||
(select relrowsecurity from pg_class where oid = 'audit.audit_logs'::regclass),
|
||||
'RLS enabled on audit_logs'
|
||||
);
|
||||
|
||||
-- Enum
|
||||
select enum_has_labels(
|
||||
'public', 'platform_role',
|
||||
array['owner', 'admin', 'moderator', 'support'],
|
||||
'platform_role has exactly the four platform roles (no professional status)'
|
||||
);
|
||||
|
||||
-- Functions
|
||||
select has_function('private', 'log_event', 'private.log_event exists');
|
||||
select has_function('private', 'is_platform_owner', array['uuid'], 'private.is_platform_owner exists');
|
||||
select has_function('private', 'is_platform_admin', array['uuid'], 'private.is_platform_admin exists');
|
||||
select has_function('private', 'is_platform_moderator', array['uuid'], 'private.is_platform_moderator exists');
|
||||
select has_function('private', 'is_support', array['uuid'], 'private.is_support exists');
|
||||
select has_function('public', 'grant_platform_role', array['uuid', 'platform_role'], 'public.grant_platform_role exists');
|
||||
select has_function('public', 'revoke_platform_role', array['uuid', 'platform_role', 'text'], 'public.revoke_platform_role exists');
|
||||
|
||||
-- Privileges: anon has nothing, authenticated has only what RLS then filters
|
||||
select ok(not has_table_privilege('anon', 'public.profiles', 'select'), 'anon cannot select profiles');
|
||||
select ok(not has_table_privilege('anon', 'public.profile_roles', 'select'), 'anon cannot select profile_roles');
|
||||
select ok(not has_table_privilege('authenticated', 'public.profiles', 'insert'), 'authenticated cannot insert profiles');
|
||||
select ok(not has_table_privilege('authenticated', 'public.profiles', 'delete'), 'authenticated cannot delete profiles');
|
||||
select ok(not has_table_privilege('authenticated', 'public.profile_roles', 'insert'), 'authenticated cannot insert profile_roles');
|
||||
select ok(not has_column_privilege('authenticated', 'public.profiles', 'active', 'update'), 'authenticated cannot update profiles.active');
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
@@ -0,0 +1,131 @@
|
||||
-- profiles: trigger creation, own-row read/update, every denied path.
|
||||
begin;
|
||||
select plan(17);
|
||||
|
||||
-- Fixtures (privileged session = migration owner)
|
||||
insert into auth.users (id, email, raw_user_meta_data) values
|
||||
('00000000-0000-0000-0000-00000000000a', 'alice@test.local', '{"display_name":"Alice","locale":"en"}'),
|
||||
('00000000-0000-0000-0000-00000000000b', 'bob@test.local', '{}'),
|
||||
('00000000-0000-0000-0000-00000000000c', 'carol.long.name@test.local', '{"locale":"xx"}');
|
||||
|
||||
-- 1. Trigger builds the profile
|
||||
select results_eq(
|
||||
$$ select display_name, locale from public.profiles where id = '00000000-0000-0000-0000-00000000000a' $$,
|
||||
$$ values ('Alice'::text, 'en'::text) $$,
|
||||
'profile created from metadata (display_name + locale)'
|
||||
);
|
||||
select results_eq(
|
||||
$$ select display_name, locale from public.profiles where id = '00000000-0000-0000-0000-00000000000b' $$,
|
||||
$$ values ('bob'::text, 'fr'::text) $$,
|
||||
'profile falls back to the email local part and locale fr'
|
||||
);
|
||||
select is(
|
||||
(select locale from public.profiles where id = '00000000-0000-0000-0000-00000000000c'),
|
||||
'fr',
|
||||
'unsupported locale in metadata falls back to fr'
|
||||
);
|
||||
|
||||
-- 2. Alice, authenticated
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
|
||||
select results_eq(
|
||||
$$ select id from public.profiles order by id $$,
|
||||
$$ values ('00000000-0000-0000-0000-00000000000a'::uuid) $$,
|
||||
'alice sees exactly her own profile'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from public.profiles where id = '00000000-0000-0000-0000-00000000000b'),
|
||||
0::bigint,
|
||||
'alice cannot read bob by id (no IDOR)'
|
||||
);
|
||||
|
||||
select lives_ok(
|
||||
$$ update public.profiles set display_name = 'Alice B.', timezone = 'Europe/Brussels' where id = '00000000-0000-0000-0000-00000000000a' $$,
|
||||
'alice updates her own display_name and timezone'
|
||||
);
|
||||
select is(
|
||||
(select display_name from public.profiles where id = '00000000-0000-0000-0000-00000000000a'),
|
||||
'Alice B.',
|
||||
'own update is visible'
|
||||
);
|
||||
|
||||
-- Update on another row: RLS filters it out silently (0 rows)
|
||||
update public.profiles set display_name = 'hacked' where id = '00000000-0000-0000-0000-00000000000b';
|
||||
select throws_ok(
|
||||
$$ update public.profiles set active = false where id = '00000000-0000-0000-0000-00000000000a' $$,
|
||||
'42501',
|
||||
null,
|
||||
'alice cannot deactivate herself (column privilege)'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ update public.profiles set phone_verified_at = now() where id = '00000000-0000-0000-0000-00000000000a' $$,
|
||||
'42501',
|
||||
null,
|
||||
'alice cannot self-verify her phone (column privilege)'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ insert into public.profiles (id, display_name) values (gen_random_uuid(), 'ghost') $$,
|
||||
'42501',
|
||||
null,
|
||||
'alice cannot insert a profile'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ delete from public.profiles where id = '00000000-0000-0000-0000-00000000000a' $$,
|
||||
'42501',
|
||||
null,
|
||||
'alice cannot delete her profile'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ update public.profiles set phone = '0612345678' where id = '00000000-0000-0000-0000-00000000000a' $$,
|
||||
'23514',
|
||||
null,
|
||||
'phone must be E.164'
|
||||
);
|
||||
|
||||
-- 3. Bob sees his row untouched
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
||||
select is(
|
||||
(select display_name from public.profiles where id = '00000000-0000-0000-0000-00000000000b'),
|
||||
'bob',
|
||||
'alice''s cross-row update did not touch bob'
|
||||
);
|
||||
|
||||
-- 4. Phone change resets verification
|
||||
reset role;
|
||||
update public.profiles
|
||||
set phone = '+33612345678', phone_verified_at = now()
|
||||
where id = '00000000-0000-0000-0000-00000000000b';
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
||||
select lives_ok(
|
||||
$$ update public.profiles set phone = '+33698765432' where id = '00000000-0000-0000-0000-00000000000b' $$,
|
||||
'bob changes his phone'
|
||||
);
|
||||
select is(
|
||||
(select phone_verified_at from public.profiles where id = '00000000-0000-0000-0000-00000000000b'),
|
||||
null,
|
||||
'changing the phone clears phone_verified_at'
|
||||
);
|
||||
|
||||
-- 5. Anonymous: no access at all
|
||||
set local role anon;
|
||||
select set_config('request.jwt.claims', '', true);
|
||||
select throws_ok(
|
||||
$$ select * from public.profiles $$,
|
||||
'42501',
|
||||
null,
|
||||
'anon cannot select profiles'
|
||||
);
|
||||
|
||||
-- 6. updated_at trigger overrides whatever the writer sends
|
||||
reset role;
|
||||
update public.profiles set updated_at = '2000-01-01' where id = '00000000-0000-0000-0000-00000000000a';
|
||||
select is(
|
||||
(select updated_at from public.profiles where id = '00000000-0000-0000-0000-00000000000a'),
|
||||
now(),
|
||||
'updated_at is stamped by the trigger, not by the writer'
|
||||
);
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
@@ -0,0 +1,147 @@
|
||||
-- Platform roles: bootstrap, delegation rules, every denied path, visibility.
|
||||
begin;
|
||||
select plan(28);
|
||||
|
||||
insert into auth.users (id, email) values
|
||||
('00000000-0000-0000-0000-000000000001', 'root@test.local'),
|
||||
('00000000-0000-0000-0000-00000000000a', 'alice@test.local'),
|
||||
('00000000-0000-0000-0000-00000000000b', 'bob@test.local'),
|
||||
('00000000-0000-0000-0000-00000000000c', 'carol@test.local');
|
||||
|
||||
-- 1. Nobody has a role; bootstrap the first owner from a privileged session
|
||||
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') $$,
|
||||
'system bootstraps the first owner (actor null)'
|
||||
);
|
||||
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');
|
||||
select ok(private.is_platform_moderator('00000000-0000-0000-0000-000000000001'), 'owner counts as moderator');
|
||||
select ok(not private.is_support('00000000-0000-0000-0000-000000000001'), 'owner is not support');
|
||||
|
||||
-- 2. Owner grants admin to alice through the RPC
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
|
||||
select lives_ok(
|
||||
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin') $$,
|
||||
'owner grants admin'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin') $$,
|
||||
'23505',
|
||||
null,
|
||||
'granting an active role twice is refused'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ select public.grant_platform_role('00000000-0000-0000-0000-000000000001', 'admin') $$,
|
||||
'42501',
|
||||
null,
|
||||
'owner cannot change their own roles'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ select public.grant_platform_role(gen_random_uuid(), 'support') $$,
|
||||
'P0002',
|
||||
null,
|
||||
'unknown target is refused'
|
||||
);
|
||||
|
||||
-- 3. Alice (admin) may delegate moderator/support, nothing above
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
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',
|
||||
null,
|
||||
'admin cannot grant admin'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000b', 'owner') $$,
|
||||
'42501',
|
||||
null,
|
||||
'admin cannot grant owner'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'owner') $$,
|
||||
'42501',
|
||||
null,
|
||||
'admin cannot promote herself to owner'
|
||||
);
|
||||
|
||||
-- 4. 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'
|
||||
);
|
||||
|
||||
-- 5. Bob (moderator) cannot delegate; sees only his own grants; carol sees nothing
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
||||
select throws_ok(
|
||||
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000c', 'support') $$,
|
||||
'42501',
|
||||
null,
|
||||
'moderator 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'
|
||||
);
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
|
||||
select is((select count(*) from public.profile_roles), 0::bigint, 'plain user sees no role rows');
|
||||
select is((select count(*) from public.profiles), 1::bigint, 'plain user still sees one profile (own)');
|
||||
|
||||
-- 6. Admin sees every grant and every profile (staff read)
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select is((select count(*) from public.profile_roles), 3::bigint, 'admin sees all three grant rows');
|
||||
select is((select count(*) from public.profiles), 4::bigint, 'admin reads every profile');
|
||||
|
||||
-- 7. 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'
|
||||
);
|
||||
|
||||
-- 8. Revocation and deactivation
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select lives_ok(
|
||||
$$ select public.revoke_platform_role('00000000-0000-0000-0000-00000000000b', 'moderator', 'test') $$,
|
||||
'admin revokes moderator'
|
||||
);
|
||||
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');
|
||||
|
||||
-- 9. Last-owner guard on the system path
|
||||
select throws_ok(
|
||||
$$ select private.withdraw_platform_role(null, '00000000-0000-0000-0000-000000000001', 'owner', 'oops') $$,
|
||||
'42501',
|
||||
null,
|
||||
'the last owner cannot be revoked'
|
||||
);
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
@@ -0,0 +1,99 @@
|
||||
-- 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) values
|
||||
('00000000-0000-0000-0000-000000000001', 'root@test.local'),
|
||||
('00000000-0000-0000-0000-00000000000a', 'alice@test.local'),
|
||||
('00000000-0000-0000-0000-00000000000b', 'bob@test.local');
|
||||
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 $$,
|
||||
$$ 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"}', 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 is not null 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),
|
||||
3::bigint,
|
||||
'every 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), 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;
|
||||
Reference in new issue
Block a user