chore(supabase): ceiling comments aligned, prefix patterns built once, cross-account residuals named in STORAGE.md

This commit is contained in:
bchanot committed 2026-10-13 20:20:19 +02:00
1 parent 6a9657ce1e
commit 40ae453f84
2 files changed
+9 -5

No files matched your search

+1 -1
View File
@@ -94,7 +94,7 @@ granted to the one role that calls it. Business refusals raise SQLSTATE
| RPC | Effect | Refusals (message) |
|---|---|---|
| `begin_avatar_upload(declared_mime text, declared_size integer) returns jsonb` | locks the caller's `profiles` row (`for update`); checks the type and the size; rate: at most 10 rows created by the caller in the last hour, every status counted; per-account budget: the caller's bytes present under `profiles/<uid>/` and `quarantine/<uid>/` plus one cap below `private.account_byte_budget()` (64 MiB) → `rate_limited`; global ceiling: the bytes present in the bucket (sum of sized `storage.objects`, whatever each row's state) plus one 5 MiB cap per `pending_upload` row whose bytes have not landed, below `private.storage_ceiling()` (10 GiB): a claim dies with its row, so begin spam pins nothing; residuals: about 160 colluding confirmed accounts at their budget fill the ceiling, and a burst of accounts fills the filesystem quota, the hard bound; supersedes the caller's `pending_upload` / `uploaded` rows (→ `deleted`); inserts the row; returns `{"asset_id", "bucket_id", "quarantine_key"}` | `unsupported_type`, `too_large`, `rate_limited`, `storage_full` |
| `begin_avatar_upload(declared_mime text, declared_size integer) returns jsonb` | locks the caller's `profiles` row (`for update`); checks the type and the size; rate: at most 10 rows created by the caller in the last hour, every status counted; per-account budget: the caller's bytes present under `profiles/<uid>/` and `quarantine/<uid>/` plus one cap below `private.account_byte_budget()` (64 MiB) → `rate_limited`; global ceiling: the bytes present in the bucket (sum of sized `storage.objects`, whatever each row's state) plus one 5 MiB cap per `pending_upload` row whose bytes have not landed, below `private.storage_ceiling()` (10 GiB): a claim dies with its row, so begin spam pins nothing; residuals: about 100 to 160 colluding confirmed accounts at their budget fill the ceiling (bytes still streaming for a superseded row weigh 0 until they land, bounded by rate × cap), about 2 000 accounts each keeping one bytes-less pending claim alive do the same, and a burst of accounts fills the filesystem quota, the hard bound; supersedes the caller's `pending_upload` / `uploaded` rows (→ `deleted`); inserts the row; returns `{"asset_id", "bucket_id", "quarantine_key"}` | `unsupported_type`, `too_large`, `rate_limited`, `storage_full` |
| `confirm_asset_upload(asset_id uuid) returns void` | active account; own `pending_upload` row with a `storage.objects` row at `(bucket_id, quarantine_key)` whose `metadata->>'size'` is set and within the cap → `uploaded` | `not_found` (unknown or not own), `invalid_state` (not pending), `upload_missing` (no object yet, or an object without a size: fail closed), `too_large` (object over the cap or over the declared size: the row stays pending and expires after 24 h) |
| `remove_avatar() returns integer` | every `profile_avatar` row of the caller in `pending_upload` / `uploaded` / `clean` → `deleted`; returns the count; logs `asset.avatar_removed` | none (0 rows is fine) |
@@ -204,7 +204,7 @@ create trigger assets_enqueue_purge_on_delete
for each row execute function private.enqueue_asset_objects();
-- 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.
-- measured on storage.objects, plus one 5 MiB cap per upload still expected.
create or replace function private.storage_ceiling()
returns bigint
language sql
@@ -312,6 +312,8 @@ declare
v_used bigint;
v_expected bigint;
v_mine bigint;
v_final_prefix text;
v_quarantine_prefix text;
v_object_key text;
v_quarantine_key text;
begin
@@ -337,10 +339,12 @@ begin
-- 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.
v_final_prefix := 'profiles/' || v_actor::text || '/%';
v_quarantine_prefix := 'quarantine/' || v_actor::text || '/%';
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 || '/%');
and (o.name like v_final_prefix or o.name like v_quarantine_prefix);
if v_mine + 5242880 > private.account_byte_budget() then
raise exception using message = 'rate_limited', errcode = 'P0001';
end if;
@@ -378,7 +382,7 @@ end;
$$;
comment on function public.begin_avatar_upload(text, integer) is
'Reserves an avatar upload: rate (10 per hour), global ceiling, type and size caps, supersedes any in-flight row of the caller, returns {asset_id, bucket_id, quarantine_key}.';
'Reserves an avatar upload: rate (10 per hour), per-account byte budget, global ceiling, type and size caps, supersedes any in-flight row of the caller, returns {asset_id, bucket_id, quarantine_key}.';
create or replace function public.confirm_asset_upload(asset_id uuid)
returns void
@@ -417,7 +421,7 @@ begin
raise exception using message = 'upload_missing', errcode = 'P0001';
end if;
-- The object may not exceed the cap nor what the caller declared (the
-- ceiling counted the declared size).
-- transport blob the browser announced).
if v_size > 5242880 or v_size > v_row.declared_size then
raise exception using message = 'too_large', errcode = 'P0001';
end if;