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

@@ -201,7 +201,8 @@ create trigger assets_enqueue_purge_on_delete
after delete on public.assets
for each row execute function private.enqueue_asset_objects();
-- Global ceiling on declared sizes of non-deleted rows (10 GiB to start).
-- Global ceiling on the bytes that can exist in the bucket (10 GiB to start):
-- live rows, plus terminal rows whose objects still wait in the purge queue.
create or replace function private.storage_ceiling()
returns bigint
language sql
@@ -242,6 +243,7 @@ set search_path = ''
as $$
select exists (
select 1 from public.assets a
join public.profiles p on p.id = a.owner_id and p.active
where a.owner_id = p_user_id
and a.kind = 'profile_avatar'
and a.status = 'pending_upload'
@@ -322,7 +324,11 @@ begin
-- purged and never ratchet the counter.
select coalesce(sum(case when a.status = 'clean' then coalesce(a.byte_size, a.declared_size) else a.declared_size end), 0)
into v_used from public.assets a
where a.status in ('pending_upload', 'uploaded', 'clean');
where a.status in ('pending_upload', 'uploaded', 'clean')
or exists (
select 1 from private.object_purge_queue q
where q.bucket_id = a.bucket_id and q.object_name in (a.object_key, a.quarantine_key)
);
if v_used + declared_size > private.storage_ceiling() then
raise exception using message = 'storage_full', errcode = 'P0001';
end if;
@@ -379,7 +385,9 @@ begin
if v_found is distinct from true then
raise exception using message = 'upload_missing', errcode = 'P0001';
end if;
if v_size > 5242880 then
-- The object may not exceed the cap nor what the caller declared (the
-- ceiling counted the declared size).
if v_size > 5242880 or v_size > v_row.declared_size then
raise exception using message = 'too_large', errcode = 'P0001';
end if;
update public.assets a set status = 'uploaded' where a.id = v_row.id;
+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;