fix(supabase): ceiling measured on bytes present in the bucket plus pending claims without bytes; confirm fails closed on an unsized object; cleartext host must start with a letter

Third audit (BLOCK 1): counting terminal rows while queued let begin spam
pin the ceiling with declared sizes. The ceiling now sums storage.objects
sizes of the bucket (whatever the row's state) plus the declared size of
pending_upload rows whose quarantine object has not landed: a claim dies
with its row, a present object counts whatever its row says, no full scan of
the assets history. confirm_asset_upload treats an object without a size as
upload_missing. pgTAP (plan 138): rejected rows without bytes weigh nothing,
pending claims fill, superseded claims vanish, present bytes fill, unsized
object refused. Worker: a single-label cleartext host must start with an
ASCII letter (0X… hex forms refused, test added); STORAGE.md names the
native-run search-domain residual.
This commit is contained in:
bchanot committed 2026-10-13 20:07:22 +02:00
1 parent 369478576e
commit 273914d825
5 files changed
+48 -35

No files matched your search

@@ -201,8 +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 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.
-- Global ceiling on the bytes present in the avatars bucket (10 GiB to start),
-- measured on storage.objects, plus the declared size of uploads still expected.
create or replace function private.storage_ceiling()
returns bigint
language sql
@@ -297,6 +297,7 @@ declare
v_id uuid := gen_random_uuid();
v_recent integer;
v_used bigint;
v_expected bigint;
v_object_key text;
v_quarantine_key text;
begin
@@ -319,17 +320,20 @@ begin
if v_recent >= 10 then
raise exception using message = 'rate_limited', errcode = 'P0001';
end if;
-- Only rows whose bytes can exist count: in-flight rows by the declared
-- transport size, clean rows by their output; rejected and deleted rows are
-- 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')
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
-- Bytes that exist (every object of the bucket, whatever its row's state)
-- plus the declared size of pending uploads whose bytes have not landed yet.
-- A claim never outlives the row it belongs to, and a row with no bytes
-- weighs nothing once it moved on, so no account can pin the ceiling.
select coalesce(sum((o.metadata ->> 'size')::bigint), 0) into v_used
from storage.objects o where o.bucket_id = 'avatars';
select coalesce(sum(a.declared_size), 0) into v_expected
from public.assets a
where a.status = 'pending_upload'
and not exists (
select 1 from storage.objects o
where o.bucket_id = a.bucket_id and o.name = a.quarantine_key
);
if v_used + v_expected + declared_size > private.storage_ceiling() then
raise exception using message = 'storage_full', errcode = 'P0001';
end if;
@@ -379,10 +383,11 @@ begin
if v_row.status <> 'pending_upload' then
raise exception using message = 'invalid_state', errcode = 'P0001';
end if;
select true, coalesce((o.metadata ->> 'size')::bigint, 0) into v_found, v_size
select true, (o.metadata ->> 'size')::bigint into v_found, v_size
from storage.objects o
where o.bucket_id = v_row.bucket_id and o.name = v_row.quarantine_key;
if v_found is distinct from true then
-- No object, or an object the Storage API has not sized: nothing to confirm (fail closed).
if v_found is distinct from true or v_size is null then
raise exception using message = 'upload_missing', errcode = 'P0001';
end if;
-- The object may not exceed the cap nor what the caller declared (the