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;
+26 -3
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(125);
select plan(130);
insert into auth.users (id, email) values
('40000000-0000-0000-0000-00000000000a', 'alice-files@test.local'),
@@ -475,7 +475,8 @@ select is(
(select count(*) from private.object_purge_queue where object_name like 'profiles/40000000-0000-0000-0000-00000000000b/%' or object_name like 'quarantine/40000000-0000-0000-0000-00000000000b/%'),
2::bigint, 'the cascade enqueues both keys of the deleted row'
);
-- Ceiling: 2048 rejected rows of 5 MiB owned by Alice fill the 10 GiB.
-- Ceiling: 2048 rejected rows of 5 MiB do NOT count (purged bytes never ratchet
-- the counter); 2048 in-flight rows of 5 MiB fill the 10 GiB.
insert into public.assets (owner_id, kind, bucket_id, object_key, quarantine_key, status, declared_mime, declared_size, rejection_reason, created_at)
select '40000000-0000-0000-0000-00000000000a', 'profile_avatar', 'avatars',
'profiles/40000000-0000-0000-0000-00000000000a/' || g || '/avatar.webp',
@@ -484,8 +485,30 @@ select '40000000-0000-0000-0000-00000000000a', 'profile_avatar', 'avatars',
from (select gen_random_uuid() as g from generate_series(1, 2048)) s;
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', 'the global ceiling refuses a new upload');
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'rejected rows do not count toward the ceiling');
reset role;
insert into public.assets (owner_id, kind, bucket_id, object_key, quarantine_key, status, declared_mime, declared_size, created_at)
select '40000000-0000-0000-0000-00000000000c', 'profile_avatar', 'avatars',
'profiles/40000000-0000-0000-0000-00000000000c/' || g || '/avatar.webp',
'quarantine/40000000-0000-0000-0000-00000000000c/' || g || '/upload',
'uploaded', 'image/jpeg', 5242880, now() - interval '2 hours'
from (select gen_random_uuid() as g from generate_series(1, 2048)) s;
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', 'in-flight bytes fill the global ceiling');
reset role;
-- Deactivated account: confirm is refused like begin.
update public.profiles set active = false where id = '40000000-0000-0000-0000-00000000000c';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok($$ select public.confirm_asset_upload(gen_random_uuid()) $$, '42501', null, 'a deactivated account cannot confirm an upload');
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, '42501', null, 'a deactivated account cannot begin an upload');
reset role;
select ok(not has_function_privilege('service_role', 'public.begin_avatar_upload(text, integer)', 'execute'), 'browser RPCs are not granted to service_role');
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a4'), gen_random_uuid(), 30000, null) $$,
'P0001', 'invalid_state', 'a null sha256 never reaches the output guard before the state guard; the guard itself refuses null (unit of the regex)'
);
select * from finish();
rollback;