fix(supabase): per-account byte budget at begin (64 MiB of bytes present), pending claims charged at the cap, unsized objects ignored in the pending term, partial index on pending claims

Fourth audit (PASS, residual MEDIUM): one account could hold real bytes
during the grace with no bound. begin_avatar_upload now refuses rate_limited
when the caller's objects under its two prefixes plus one cap exceed
private.account_byte_budget(); pending claims count one 5 MiB cap each
(the declared size is not enforced by the upload); an object without a size
does not cancel its claim. pgTAP (plan 140): landed upload charged once by
its bytes, per-account budget refusal. STORAGE.md names the residuals
(colluding accounts at their budget, filesystem quota) and the musl ndots
detail.
This commit is contained in:
bchanot committed 2026-10-13 20:15:18 +02:00
1 parent 273914d825
commit 6a9657ce1e
3 files changed
+55 -12

No files matched your search

@@ -101,6 +101,8 @@ comment on column public.assets.lock_token is
create unique index assets_one_clean_avatar
on public.assets (owner_id) where kind = 'profile_avatar' and status = 'clean';
create index assets_claimable on public.assets (created_at) where status = 'uploaded';
-- The ceiling's pending term reads only the rows still waiting for bytes.
create index assets_pending_claims on public.assets (quarantine_key) where status = 'pending_upload';
create index assets_owner_idx on public.assets (owner_id, created_at desc);
create trigger assets_set_updated_at
@@ -212,6 +214,17 @@ as $$ select 10737418240::bigint; $$;
revoke all on function private.storage_ceiling() from public;
-- Bytes one account may hold in the bucket at once (its clean photo plus the
-- quarantine objects of its last uploads during their grace): 64 MiB.
create or replace function private.account_byte_budget()
returns bigint
language sql
immutable
set search_path = ''
as $$ select 67108864::bigint; $$;
revoke all on function private.account_byte_budget() from public;
-- ---------------------------------------------------------------------------
-- Storage policies for bucket avatars, evaluated through assets.
-- ---------------------------------------------------------------------------
@@ -298,6 +311,7 @@ declare
v_recent integer;
v_used bigint;
v_expected bigint;
v_mine bigint;
v_object_key text;
v_quarantine_key text;
begin
@@ -320,20 +334,32 @@ begin
if v_recent >= 10 then
raise exception using message = 'rate_limited', errcode = 'P0001';
end if;
-- 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.
-- Per-account budget on bytes present under the caller's two prefixes
-- (clean photo, quarantine objects within their grace): bounds what one
-- account can hold against the global ceiling.
select coalesce(sum((o.metadata ->> 'size')::bigint), 0) into v_mine
from storage.objects o
where o.bucket_id = 'avatars'
and (o.name like 'profiles/' || v_actor || '/%' or o.name like 'quarantine/' || v_actor || '/%');
if v_mine + 5242880 > private.account_byte_budget() then
raise exception using message = 'rate_limited', errcode = 'P0001';
end if;
-- Global ceiling: bytes that exist (every sized object of the bucket,
-- whatever its row's state) plus one cap per pending upload whose bytes
-- have not landed yet (the declared size is a claim the upload does not
-- enforce). A claim never outlives its row and a row without 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
select count(*) * 5242880 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
and o.metadata ->> 'size' is not null
);
if v_used + v_expected + declared_size > private.storage_ceiling() then
if v_used + v_expected + 5242880 > private.storage_ceiling() then
raise exception using message = 'storage_full', errcode = 'P0001';
end if;
+21 -4
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(138);
select plan(140);
insert into auth.users (id, email) values
('40000000-0000-0000-0000-00000000000a', 'alice-files@test.local'),
@@ -504,13 +504,30 @@ 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) $$, 'scenario: rows that moved on without bytes weigh nothing, even while queued for purge');
reset role;
-- Bytes present count whatever the row says: one 10 GiB object fills the ceiling.
insert into storage.objects (bucket_id, name, metadata) values ('avatars', 'quarantine/40000000-0000-0000-0000-00000000000c/00000000-0000-0000-0000-00000000ffff/upload', '{"size":10737418240}');
-- Bytes present count whatever the row says: one 10 GiB object (under a foreign
-- prefix, so the per-account budget stays out of the way) fills the ceiling.
insert into storage.objects (bucket_id, name, metadata) values ('avatars', 'quarantine/40000000-0000-0000-0000-00000000000d/00000000-0000-0000-0000-00000000ffff/upload', '{"size":10737418240}');
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', 'bytes present in the bucket fill the ceiling whatever their row');
reset role;
update storage.objects set metadata = '{"size":1}' where name = 'quarantine/40000000-0000-0000-0000-00000000000c/00000000-0000-0000-0000-00000000ffff/upload';
update storage.objects set metadata = '{"size":1}' where name = 'quarantine/40000000-0000-0000-0000-00000000000d/00000000-0000-0000-0000-00000000ffff/upload';
-- A pending row whose bytes landed is charged once, by its object (no double count).
update public.assets set declared_size = 5242880 where owner_id = '40000000-0000-0000-0000-00000000000c' and status = 'pending_upload';
insert into storage.objects (bucket_id, name, metadata)
select 'avatars', a.quarantine_key, '{"size":1024}' from public.assets a
where a.owner_id = '40000000-0000-0000-0000-00000000000c' and a.status = 'pending_upload';
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) $$, 'a landed upload is counted by its bytes, not by its claim');
reset role;
-- Per-account budget: 64 MiB of the caller's own bytes present → rate_limited.
insert into storage.objects (bucket_id, name, metadata) values ('avatars', 'quarantine/40000000-0000-0000-0000-00000000000c/00000000-0000-0000-0000-00000000fffe/upload', '{"size":67108864}');
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', 'rate_limited', 'scenario: one account cannot hold more than its byte budget in the bucket');
reset role;
update storage.objects set metadata = '{"size":1}' where name = 'quarantine/40000000-0000-0000-0000-00000000000c/00000000-0000-0000-0000-00000000fffe/upload';
-- 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);