fix(supabase): security pass on the files foundation — ceiling counts live bytes only, confirm checks the account, null sha256 refused, browser RPCs one role; worker: cleartext only on loopback or a service name, no env proxy, key ownership cross-check, no library text in logs

Security audit (PASS, 2 MEDIUM + LOW): the global ceiling summed every
non-deleted row, so rejected uploads ratcheted it toward storage_full with no
disk used → in-flight rows by declared size, clean rows by byte_size, nothing
else (pgTAP: rejected rows do not count, in-flight rows do); confirm refuses a
deactivated account like begin; complete_avatar_job refuses a null sha256;
begin / confirm / remove granted to authenticated only. Worker: SUPABASE_URL
over http:// accepted only for loopback or a single-label Docker service
name; ureq proxy(None); the job's keys must embed its owner and asset ids;
backend_failure / claim_failed / sweep_failure lines carry the closed kind and
the HTTP status, never the library's error text. e2e: uuid shape checked
before the psql query. Contract revision 2 (oracle fixes).
This commit is contained in:
bchanot committed 2026-10-13 19:49:58 +02:00
1 parent 62eb4e5d82
commit cd2dbb3b79
11 files changed
+124 -31

No files matched your search

@@ -317,8 +317,12 @@ begin
if v_recent >= 10 then
raise exception using message = 'rate_limited', errcode = 'P0001';
end if;
select coalesce(sum(a.declared_size), 0) into v_used from public.assets a
where a.status <> 'deleted';
-- 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');
if v_used + declared_size > private.storage_ceiling() then
raise exception using message = 'storage_full', errcode = 'P0001';
end if;
@@ -349,6 +353,7 @@ as $$
#variable_conflict use_variable
declare
v_actor uuid := auth.uid();
v_active boolean;
v_row public.assets;
v_size bigint;
v_found boolean;
@@ -356,6 +361,10 @@ begin
if v_actor is null then
raise exception 'authentication required' using errcode = '42501';
end if;
select p.active into v_active from public.profiles p where p.id = v_actor;
if v_active is distinct from true then
raise exception 'account inactive or unknown' using errcode = '42501';
end if;
select a.* into v_row from public.assets a
where a.id = asset_id and a.owner_id = v_actor for update;
if v_row.id is null then
@@ -413,9 +422,12 @@ comment on function public.remove_avatar() is
revoke all on function public.begin_avatar_upload(text, integer) from public, anon;
revoke all on function public.confirm_asset_upload(uuid) from public, anon;
revoke all on function public.remove_avatar() from public, anon;
grant execute on function public.begin_avatar_upload(text, integer) to authenticated, service_role;
grant execute on function public.confirm_asset_upload(uuid) to authenticated, service_role;
grant execute on function public.remove_avatar() to authenticated, service_role;
revoke all on function public.begin_avatar_upload(text, integer) from service_role;
revoke all on function public.confirm_asset_upload(uuid) from service_role;
revoke all on function public.remove_avatar() from service_role;
grant execute on function public.begin_avatar_upload(text, integer) to authenticated;
grant execute on function public.confirm_asset_upload(uuid) to authenticated;
grant execute on function public.remove_avatar() to authenticated;
-- ---------------------------------------------------------------------------
-- Worker RPCs (service_role only; EXECUTE is the guard, as for
@@ -493,7 +505,7 @@ begin
if v_row.status <> 'uploaded' or lock_token is null or v_row.lock_token is distinct from lock_token then
raise exception using message = 'invalid_state', errcode = 'P0001';
end if;
if byte_size is null or byte_size < 1 or byte_size > 524288 or sha256 !~ '^[0-9a-f]{64}$' then
if byte_size is null or byte_size < 1 or byte_size > 524288 or sha256 is null or sha256 !~ '^[0-9a-f]{64}$' then
raise exception using message = 'invalid_output', errcode = 'P0001';
end if;