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:
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;
|
||||
|
||||
|
||||
Reference in new issue
Block a user