fix(supabase): second security pass — ceiling counts queued purges, confirm refuses more than declared, upload predicate needs an active account; worker cleartext host check strict

Re-audit findings: the ceiling now also counts terminal rows whose keys still
wait in the purge queue (contract wording kept honest, residual named);
confirm_asset_upload refuses an object larger than the declared size;
can_upload_avatar_object joins profiles.active; pgTAP covers the null
sha256 guard on a live lease, the three service_role revocations, the
queued-purge accounting and the deactivated-account upload (plan 137).
Worker: cleartext http only for localhost, a dotted-quad loopback address or
a non-numeric single-label name (numeric and hex hosts refused, tests added).
This commit is contained in:
bchanot committed 2026-10-13 19:58:54 +02:00
1 parent cd2dbb3b79
commit c386a08b0b
5 files changed
+75 -14

No files matched your search

+43 -5
View File
@@ -2,7 +2,7 @@
-- browser and worker RPCs, the purge queue. Every allowed path has its denial
-- twin; the eight scenarios of the Step 8 brief are named in the descriptions.
begin;
select plan(130);
select plan(137);
insert into auth.users (id, email) values
('40000000-0000-0000-0000-00000000000a', 'alice-files@test.local'),
@@ -497,18 +497,56 @@ set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'P0001', 'storage_full', 'in-flight bytes fill the global ceiling');
reset role;
-- Deactivated account: confirm is refused like begin.
-- Deleted rows still count while their objects wait in the purge queue.
update public.assets set status = 'deleted' where owner_id = '40000000-0000-0000-0000-00000000000c' and status = 'uploaded';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'P0001', 'storage_full', 'objects still queued for purge count toward the ceiling');
reset role;
delete from private.object_purge_queue where object_name like '%/40000000-0000-0000-0000-00000000000c/%';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'served purge entries free the ceiling');
reset role;
-- Carol's pending row (begun above): the object may not exceed the declared size.
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
insert into pg_temp.ids select 'c1', id from public.assets where owner_id = '40000000-0000-0000-0000-00000000000c' and status = 'pending_upload';
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('c1')).quarantine_key, '{"size":4000}');
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('c1')) $$, 'P0001', 'too_large', 'an object larger than the declared size is refused at confirm');
reset role;
update storage.objects set metadata = '{"size":1000}' where name = (pg_temp.row('c1')).quarantine_key;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select lives_ok($$ select public.confirm_asset_upload(pg_temp.id('c1')) $$, 'carol confirms within the declared size');
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tokc1', lock_token from public.claim_avatar_jobs(1);
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('c1'), pg_temp.id('tokc1'), 30000, null) $$,
'P0001', 'invalid_output', 'a null sha256 is refused by the output guard'
);
reset role;
-- Deactivated account: begin, confirm and the upload policy refuse it.
update public.profiles set active = false where id = '40000000-0000-0000-0000-00000000000c';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok($$ select public.confirm_asset_upload(gen_random_uuid()) $$, '42501', null, 'a deactivated account cannot confirm an upload');
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, '42501', null, 'a deactivated account cannot begin an upload');
reset role;
select ok(not has_function_privilege('service_role', 'public.begin_avatar_upload(text, integer)', 'execute'), 'browser RPCs are not granted to service_role');
update public.assets set status = 'pending_upload', locked_at = null, lock_token = null, attempts = 0 where id = pg_temp.id('c1');
delete from storage.objects where name = (pg_temp.row('c1')).quarantine_key;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a4'), gen_random_uuid(), 30000, null) $$,
'P0001', 'invalid_state', 'a null sha256 never reaches the output guard before the state guard; the guard itself refuses null (unit of the regex)'
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('c1')).quarantine_key, '{"size":1000}') $$,
'42501', null, 'a deactivated account cannot upload at its pending quarantine key'
);
reset role;
select ok(not has_function_privilege('service_role', 'public.begin_avatar_upload(text, integer)', 'execute'), 'begin_avatar_upload is not granted to service_role');
select ok(not has_function_privilege('service_role', 'public.confirm_asset_upload(uuid)', 'execute'), 'confirm_asset_upload is not granted to service_role');
select ok(not has_function_privilege('service_role', 'public.remove_avatar()', 'execute'), 'remove_avatar is not granted to service_role');
select * from finish();
rollback;