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

+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);