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;