fix(supabase): revoke PUBLIC EXECUTE on private helpers, audited visibility RPC, input caps, authz-first RPCs, +32 pgTAP checks

This commit is contained in:
bchanot committed 2026-10-08 23:59:39 +02:00
1 parent 6966c54a2e
commit d3fa65af29
15 files changed
+682 -221

No files matched your search

+16 -1
View File
@@ -1,6 +1,6 @@
-- Schema contract: objects exist, RLS is on, API roles start from zero.
begin;
select plan(24);
select plan(26);
-- Schemas
select has_schema('private', 'schema private exists');
@@ -51,5 +51,20 @@ select ok(not has_column_privilege('authenticated', 'public.profiles', 'active',
select ok(not has_schema_privilege('anon', 'private', 'usage'), 'anon has no usage on schema private');
select ok(not has_function_privilege('anon', 'public.grant_platform_role(uuid, public.platform_role)', 'execute'), 'anon cannot execute grant_platform_role');
-- Private helpers: EXECUTE revoked from everyone except the predicates the policies evaluate.
-- (has_function_privilege is true for any role while proacl is null, i.e. the built-in PUBLIC default.)
select is(
(select count(*) from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname in ('private', 'audit') and has_function_privilege('anon', p.oid, 'execute')),
0::bigint, 'anon can execute nothing in private/audit'
);
select is(
(select string_agg(p.proname::text, ',' order by p.proname::text collate "C")
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname in ('private', 'audit') and has_function_privilege('authenticated', p.oid, 'execute')),
'can_edit_sheet,can_use_sheet,has_platform_role,is_platform_admin,is_platform_moderator,is_platform_owner,is_support',
'authenticated executes only the policy predicates in private'
);
select * from finish();
rollback;
+141 -15
View File
@@ -3,7 +3,7 @@
-- denied path (other user, anon, direct writes, official sheets).
-- Counts are scoped to fixture ids: the real stack also carries seed rows.
begin;
select plan(95);
select plan(127);
-- ---------------------------------------------------------------------------
-- Fixtures
@@ -106,7 +106,23 @@ select ok(not has_table_privilege('authenticated', 'public.anatomical_sheet_vers
select ok(not has_column_privilege('authenticated', 'public.anatomical_sheet_versions', 'status', 'update'), 'authenticated cannot update versions.status');
select ok(has_column_privilege('authenticated', 'public.anatomical_sheet_versions', 'content', 'update'), 'authenticated may update versions.content (RLS: own draft)');
select ok(not has_table_privilege('authenticated', 'public.anatomical_sheet_assets', 'insert'), 'authenticated cannot insert assets directly');
select ok(not has_function_privilege('anon', 'public.publish_sheet_version(uuid)', 'execute'), 'anon cannot execute publish_sheet_version');
select ok(
not (has_column_privilege('authenticated', 'public.anatomical_sheets', 'visibility', 'update')
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'creator_user_id', 'update')
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'current_version_id', 'update')
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'based_on_sheet_id', 'update')
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'based_on_version_id', 'update')
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'species_id', 'update')),
'visibility, ownership, provenance, current version and species are not user-updatable'
);
select ok(not has_table_privilege('authenticated', 'public.anatomical_sheet_versions', 'delete'), 'authenticated cannot delete versions directly (RPC only)');
select is(
(select count(*) from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public' and p.prokind = 'f'
and not exists (select 1 from pg_depend d where d.objid = p.oid and d.deptype = 'e')
and has_function_privilege('anon', p.oid, 'execute')),
0::bigint, 'anon can execute no public RPC'
);
-- ---------------------------------------------------------------------------
-- 2. Alice creates a private sheet (draft v1), registers an asset, publishes
@@ -138,6 +154,17 @@ select throws_ok(
'23505', null, 'slug is unique per owner'
);
select throws_ok(
$$ select public.create_anatomical_sheet('equine', 'Bad', 'bad-doc', '{"schema_version":"abc","species_id":"equine"}'::jsonb) $$,
'23514', null, 'a non-numeric schema_version is refused'
);
select throws_ok(
$$ update public.anatomical_sheet_versions
set content = jsonb_build_object('schema_version', 1, 'species_id', 'equine', 'pad', repeat('x', 2200000))
where id = pg_temp.id('v1') $$,
'23514', null, 'a document over 2 MiB is refused'
);
-- Publishing a draft whose main view has no asset is refused with the rule code
select throws_ok(
$$ select public.publish_sheet_version(pg_temp.id('v1')) $$,
@@ -196,6 +223,61 @@ select throws_ok(
'23514', 'document not publishable: annotation.id.duplicate',
'publish refuses duplicate annotation ids'
);
create function pg_temp.publish_issue(p_doc jsonb) returns text language plpgsql as $$
begin
update public.anatomical_sheet_versions set content = p_doc where id = pg_temp.id('v1');
perform public.publish_sheet_version(pg_temp.id('v1'));
return 'published';
exception when check_violation then
return sqlerrm;
end $$;
grant execute on function pg_temp.publish_issue(jsonb) to authenticated;
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{views,0,kind}', '"detail"')),
'document not publishable: view.main.count, view.parent.missing', 'SQL gate: no main view'
);
select is(
pg_temp.publish_issue(pg_temp.sheet_doc(pg_temp.id('asset')) #- '{annotations,2}'),
'document not publishable: view.unreachable', 'SQL gate: detail view without trigger'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{views,1,parent_view_id}', '"nowhere"')),
'document not publishable: view.parent.missing, annotation.opens_view.invalid', 'SQL gate: dangling parent view'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{views,1,id}', '"view-main"')),
'document not publishable: view.unreachable, view.id.duplicate, annotation.opens_view.invalid', 'SQL gate: duplicate view id'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,0,view_id}', '"nowhere"')),
'document not publishable: annotation.view.missing', 'SQL gate: annotation on a missing view'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,0,category_id}', '"nowhere"')),
'document not publishable: annotation.category.missing', 'SQL gate: missing category'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,1,vertebral_region_code}', '"Z"')),
'document not publishable: annotation.region.missing', 'SQL gate: unknown vertebral region'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,2,opens_view_id}', '"view-main"')),
'document not publishable: view.unreachable, annotation.opens_view.invalid', 'SQL gate: trigger opening a non-child view'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{vertebral_config,regions}',
'[{"code":"C","name":{"fr":"a","en":"a"},"count":7},{"code":"C","name":{"fr":"b","en":"b"},"count":7}]')),
'document not publishable: region.code.duplicate', 'SQL gate: duplicate region code'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{finding_zones}',
'[{"id":"hip","name":{"fr":"a","en":"a"},"categories":[]},{"id":"hip","name":{"fr":"b","en":"b"},"categories":[]}]')),
'document not publishable: finding_zone.id.duplicate', 'SQL gate: duplicate finding zone id'
);
select is(
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{views}', '"not-an-array"')),
'document not publishable: document.shape', 'SQL gate: views not an array'
);
update public.anatomical_sheet_versions
set content = pg_temp.sheet_doc(pg_temp.id('asset')) where id = pg_temp.id('v1');
@@ -233,10 +315,9 @@ select is(
(select change_note from public.anatomical_sheet_versions where id = pg_temp.id('v1')),
'asset wired', 'editor UPDATE of a published change note touches no row'
);
delete from public.anatomical_sheet_versions where id = pg_temp.id('v1');
select is(
(select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('v1')),
1::bigint, 'editor DELETE of a published version touches no row'
select throws_ok(
$$ delete from public.anatomical_sheet_versions where id = pg_temp.id('v1') $$,
'42501', null, 'editor has no DELETE privilege on versions (RPC only)'
);
reset role;
select throws_ok(
@@ -331,8 +412,11 @@ select throws_ok(
-- alice makes the sheet public through the identity-column policy
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select name from public.anatomical_sheets where id = pg_temp.id('sheet')), 'Mon cheval', 'bob update on alice sheet changed nothing');
update public.anatomical_sheets set visibility = 'public' where id = pg_temp.id('sheet');
select lives_ok($$ select public.set_sheet_visibility(pg_temp.id('sheet'), 'public') $$, 'alice makes the sheet public (audited RPC)');
insert into pg_temp.ids select 'v3', public.create_sheet_draft(pg_temp.id('sheet'), null, 'work in progress');
insert into pg_temp.ids select 'asset-unused', public.register_sheet_asset(
pg_temp.id('sheet'), repeat('b', 64), 1000, 'Unused.svg'
);
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 1::bigint, 'bob sees the public sheet');
@@ -348,8 +432,25 @@ select is(
update public.anatomical_sheet_versions set content = '{"schema_version":1}'::jsonb where id = pg_temp.id('v3');
select throws_ok(
$$ select public.fork_anatomical_sheet(pg_temp.id('v3'), 'Copie', 'copie-bob') $$,
'55000', null, 'only a published version can be copied'
'42501', null, 'a draft cannot be copied (hidden from non-editors)'
);
update public.anatomical_sheets set name = 'hijack-public' where id = pg_temp.id('sheet');
select is((select name from public.anatomical_sheets where id = pg_temp.id('sheet')), 'Mon cheval', 'bob cannot rename a public sheet he does not edit');
-- Every editor-gated RPC refuses a non-editor, even on a public sheet
select throws_ok($$ select public.publish_sheet_version(pg_temp.id('v3')) $$, '42501', null, 'non-editor cannot publish');
select throws_ok($$ select public.deprecate_sheet_version(pg_temp.id('v2'), 'x') $$, '42501', null, 'non-editor cannot deprecate');
select throws_ok($$ select public.delete_sheet_version(pg_temp.id('v3')) $$, '42501', null, 'non-editor cannot delete a version');
select throws_ok($$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$, '42501', null, 'non-editor cannot archive');
select throws_ok($$ select public.restore_anatomical_sheet(pg_temp.id('sheet')) $$, '42501', null, 'non-editor cannot restore');
select throws_ok($$ select public.delete_anatomical_sheet(pg_temp.id('sheet')) $$, '42501', null, 'non-editor cannot delete a sheet');
select throws_ok($$ select public.set_sheet_visibility(pg_temp.id('sheet'), 'private') $$, '42501', null, 'non-editor cannot change visibility');
select throws_ok($$ select public.register_sheet_asset(pg_temp.id('sheet'), repeat('c', 64), 10, 'x.svg') $$, '42501', null, 'non-editor cannot register an asset');
select throws_ok($$ select public.delete_sheet_asset(pg_temp.id('asset-unused')) $$, '42501', null, 'non-editor cannot delete an asset');
select throws_ok($$ select public.publish_sheet_version(gen_random_uuid()) $$, '42501', null, 'unknown version gets the same answer as a foreign one');
-- A platform admin is not an editor of a personal sheet
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select throws_ok($$ select public.create_sheet_draft(pg_temp.id('sheet')) $$, '42501', null, 'platform owner cannot edit a personal sheet');
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
create table pg_temp.source_snapshot as
select s.updated_at as sheet_updated_at, s.current_version_id, v.content, v.updated_at as version_updated_at,
@@ -374,7 +475,7 @@ select results_eq(
select results_eq(
$$ select sha256, byte_size, id <> pg_temp.id('asset') from public.anatomical_sheet_assets where sheet_id = pg_temp.id('fork') $$,
$$ values (repeat('a', 64), 142400, true) $$,
'asset rows are copied (same content, new id)'
'only the asset referenced by the copied version is copied (same content, new id)'
);
select is(
(select content -> 'views' -> 0 ->> 'svg_asset_id' from public.anatomical_sheet_versions where id = pg_temp.id('fork-v1')),
@@ -434,6 +535,7 @@ select throws_ok(
'55000', null, 'the sheet cannot be deleted while a copy is active'
);
select lives_ok($$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice archives the sheet');
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 1::bigint, 'the editor still reads her archived sheet');
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 0::bigint, 'an archived sheet is no longer usable by others');
select throws_ok(
@@ -447,14 +549,36 @@ select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-0000000
select lives_ok($$ select public.restore_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice restores the sheet');
select lives_ok($$ select public.delete_sheet_version(pg_temp.id('v2')) $$, 'deprecated v2 is deleted once no active copy depends on it');
select is((select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('v2')), 0::bigint, 'v2 row is gone');
select lives_ok($$ delete from public.anatomical_sheet_versions where id = pg_temp.id('v3') $$, 'editor drops her own draft directly');
select lives_ok($$ select public.delete_sheet_version(pg_temp.id('v3')) $$, 'editor drops her own draft through the RPC');
insert into pg_temp.ids select 'v4', public.create_sheet_draft(pg_temp.id('sheet'), null, 'after deletion');
select is(
(select version_number from public.anatomical_sheet_versions where id = pg_temp.id('v4')),
4, 'a deleted version number is never reused'
);
select lives_ok($$ select public.delete_sheet_asset(pg_temp.id('asset-unused')) $$, 'an unreferenced asset can be deleted');
select throws_ok(
$$ select public.delete_sheet_asset(pg_temp.id('asset')) $$,
'55000', null, 'an asset referenced by a version cannot be deleted'
);
-- ---------------------------------------------------------------------------
-- 7. Official sheets: platform admins only
-- 7. Deactivated account
-- ---------------------------------------------------------------------------
reset role;
update public.profiles set active = false where id = '20000000-0000-0000-0000-00000000000b';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select throws_ok(
$$ select public.create_anatomical_sheet('equine', 'Nope', 'nope', pg_temp.sheet_doc(null)) $$,
'42501', null, 'a deactivated account cannot write'
);
reset role;
update public.profiles set active = true where id = '20000000-0000-0000-0000-00000000000b';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
-- ---------------------------------------------------------------------------
-- 8. Official sheets: platform admins only
-- ---------------------------------------------------------------------------
select throws_ok(
$$ select public.create_anatomical_sheet('equine', 'Officielle', 'officielle', pg_temp.sheet_doc(null), '', 'public', true) $$,
@@ -481,7 +605,7 @@ select throws_ok(
);
-- ---------------------------------------------------------------------------
-- 8. Anonymous
-- 9. Anonymous
-- ---------------------------------------------------------------------------
set local role anon;
select set_config('request.jwt.claims', '', true);
@@ -493,15 +617,17 @@ select throws_ok(
);
-- ---------------------------------------------------------------------------
-- 9. Audit trail
-- 10. Audit trail
-- ---------------------------------------------------------------------------
reset role;
select results_eq(
$$ select action from audit.audit_logs
where resource_id in (pg_temp.id('sheet'), pg_temp.id('v1'), pg_temp.id('v2'), pg_temp.id('fork'), pg_temp.id('asset'))
where resource_id in (pg_temp.id('sheet'), pg_temp.id('v1'), pg_temp.id('v2'), pg_temp.id('v3'), pg_temp.id('fork'),
pg_temp.id('asset'), pg_temp.id('asset-unused'))
group by action order by action collate "C" $$,
$$ values ('anatomical_sheet.archived'), ('anatomical_sheet.created'), ('anatomical_sheet.deleted'),
('anatomical_sheet.forked'), ('anatomical_sheet.restored'), ('anatomical_sheet_asset.registered'),
('anatomical_sheet.forked'), ('anatomical_sheet.restored'), ('anatomical_sheet.visibility_changed'),
('anatomical_sheet_asset.deleted'), ('anatomical_sheet_asset.registered'),
('anatomical_sheet_version.created'), ('anatomical_sheet_version.deleted'),
('anatomical_sheet_version.deprecated'), ('anatomical_sheet_version.published') $$,
'every lifecycle step left an audit event'