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

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