diff --git a/crates/worker/src/config.rs b/crates/worker/src/config.rs index 2e0a2297..492461a2 100644 --- a/crates/worker/src/config.rs +++ b/crates/worker/src/config.rs @@ -136,10 +136,21 @@ fn parse_url(raw: String) -> Result { Ok(url) } -/// Loopback addresses and single-label names (no dot) may use plain HTTP. +/// `localhost`, a dotted-quad loopback address, or a single-label name that is not a +/// number (a Docker service name on the compose network) may use plain HTTP. Numeric +/// forms (`167772165`, `0x0a000005`) resolve to arbitrary addresses and are refused. fn cleartext_allowed(host_port: &str) -> bool { let host = host_port.rsplit_once(':').map_or(host_port, |(h, _)| h); - host == "localhost" || host.starts_with("127.") || host == "[::1]" || !host.contains('.') + if host == "localhost" { + return true; + } + if let Ok(address) = host.parse::() { + return address.is_loopback(); + } + !host.is_empty() + && !host.contains('.') + && host.chars().any(|c| c.is_ascii_alphabetic()) + && !host.starts_with("0x") } fn seconds( @@ -223,6 +234,10 @@ mod tests { for bad in [ "http://abc.supabase.co", "http://10.0.0.5:8000", + "http://127.evil.example", + "http://167772165:8000", + "http://0x0a000005", + "http://127.0.0.1.example", "ftp://x", "http://", ] { diff --git a/docs/development/STEP8_FILES_ANALYSIS.md b/docs/development/STEP8_FILES_ANALYSIS.md index 681748ea..f20845fe 100644 --- a/docs/development/STEP8_FILES_ANALYSIS.md +++ b/docs/development/STEP8_FILES_ANALYSIS.md @@ -174,7 +174,7 @@ new predicates. | RPC | Role | Effect | |---|---|---| -| `begin_avatar_upload(declared_mime, declared_size)` | authenticated | locks the caller's `profiles` row (`for update`, serialises a double click); rate: ≤ 10 rows of the caller created in the last hour, all statuses counted → `rate_limited`; global ceiling: sum of `declared_size` of non-deleted rows < `private.storage_ceiling()` → `storage_full`; caps (type in the list, size ≤ 5 MiB); supersedes any `pending_upload` / `uploaded` row of the caller (→ `deleted`, enqueued for purge); inserts the row with `object_key = profiles///avatar.webp`, `quarantine_key = quarantine///upload`; returns `{asset_id, bucket_id, quarantine_key}` | +| `begin_avatar_upload(declared_mime, declared_size)` | authenticated | locks the caller's `profiles` row (`for update`, serialises a double click); rate: ≤ 10 rows of the caller created in the last hour, all statuses counted → `rate_limited`; global ceiling: bytes that can exist (live rows, plus terminal rows still queued for purge; as built, STORAGE.md §2.1) < `private.storage_ceiling()` → `storage_full`; caps (type in the list, size ≤ 5 MiB); supersedes any `pending_upload` / `uploaded` row of the caller (→ `deleted`, enqueued for purge); inserts the row with `object_key = profiles///avatar.webp`, `quarantine_key = quarantine///upload`; returns `{asset_id, bucket_id, quarantine_key}` | | `confirm_asset_upload(asset_id)` | authenticated | own row, `pending_upload`, a `storage.objects` row exists at `(bucket_id, quarantine_key)` with `metadata->>'size'` ≤ cap → `uploaded`. Kept as an explicit client signal: the worker never has to guess from Storage API internals whether a PUT finished | | `remove_avatar()` | authenticated | every `profile_avatar` row of the caller not already `deleted` / `rejected` (clean and in flight) → `deleted`; logs `asset.avatar_removed` | | `claim_avatar_jobs(max_jobs)` | service_role | `uploaded` rows with `locked_at is null or locked_at < now() - 10 min` and `attempts < 3`, `for update skip locked`, oldest first → sets `locked_at`, `attempts + 1`, returns the rows; a row at its 3rd expired lease becomes `rejected (processing_error)` instead. VOLATILE (a STABLE function runs read-only through PostgREST) | diff --git a/docs/development/STORAGE.md b/docs/development/STORAGE.md index 9f0d8c05..cfc24ecf 100644 --- a/docs/development/STORAGE.md +++ b/docs/development/STORAGE.md @@ -94,8 +94,8 @@ 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; global ceiling: the bytes that can exist (`declared_size` of in-flight rows, `byte_size` of clean rows; rejected and deleted rows never count) below `private.storage_ceiling()` (10 GiB); 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 within the cap → `uploaded` | `not_found` (unknown or not own), `invalid_state` (not pending), `upload_missing` (no object yet), `too_large` (object over the cap, a guard the bucket cap already enforces: the row stays pending and expires after 24 h) | +| `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; global ceiling: the bytes that can exist (`declared_size` of in-flight rows, `byte_size` of clean rows, and terminal rows while their keys still wait in the purge queue) below `private.storage_ceiling()` (10 GiB); residual: a burst of accounts within the ceiling 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 within the cap → `uploaded` | `not_found` (unknown or not own), `invalid_state` (not pending), `upload_missing` (no object yet), `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) | `auth.uid() is null` → `42501` on all three. @@ -170,7 +170,7 @@ metadata chunk), hashes it and uploads it. | Variable | Required | Default | Meaning | |---|---|---|---| -| `SUPABASE_URL` | yes | | origin of the stack (`http://supabase_kong_faunosteo:8000` from the Supabase compose network, `http://127.0.0.1:54321` natively). Cleartext `http://` is accepted only for loopback or a single-label host (a Docker service name); any other host must be `https://`, the service key travels in the headers | +| `SUPABASE_URL` | yes | | origin of the stack (`http://supabase_kong_faunosteo:8000` from the Supabase compose network, `http://127.0.0.1:54321` natively). Cleartext `http://` is accepted only for `localhost`, a dotted-quad loopback address, or a single-label host that is not a number (a Docker service name on the compose network, where a `search` domain cannot apply); any other host must be `https://`, the service key travels in the headers | | `SUPABASE_SERVICE_ROLE_KEY` | yes | | the service key; never printed; the JWT `exp` claim, when the key is a JWT, is logged at start and warned about 30 days ahead | | `WORKER_ID` | no | `worker-` | tag of every log line | | `WORKER_POLL_SECONDS` | no | `2` | idle interval between claims | diff --git a/supabase/migrations/20261013100000_files_foundation.sql b/supabase/migrations/20261013100000_files_foundation.sql index 7145535f..433aabbf 100644 --- a/supabase/migrations/20261013100000_files_foundation.sql +++ b/supabase/migrations/20261013100000_files_foundation.sql @@ -201,7 +201,8 @@ create trigger assets_enqueue_purge_on_delete after delete on public.assets for each row execute function private.enqueue_asset_objects(); --- Global ceiling on declared sizes of non-deleted rows (10 GiB to start). +-- Global ceiling on the bytes that can exist in the bucket (10 GiB to start): +-- live rows, plus terminal rows whose objects still wait in the purge queue. create or replace function private.storage_ceiling() returns bigint language sql @@ -242,6 +243,7 @@ set search_path = '' as $$ select exists ( select 1 from public.assets a + join public.profiles p on p.id = a.owner_id and p.active where a.owner_id = p_user_id and a.kind = 'profile_avatar' and a.status = 'pending_upload' @@ -322,7 +324,11 @@ begin -- 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'); + where a.status in ('pending_upload', 'uploaded', 'clean') + or exists ( + select 1 from private.object_purge_queue q + where q.bucket_id = a.bucket_id and q.object_name in (a.object_key, a.quarantine_key) + ); if v_used + declared_size > private.storage_ceiling() then raise exception using message = 'storage_full', errcode = 'P0001'; end if; @@ -379,7 +385,9 @@ begin if v_found is distinct from true then raise exception using message = 'upload_missing', errcode = 'P0001'; end if; - if v_size > 5242880 then + -- The object may not exceed the cap nor what the caller declared (the + -- ceiling counted the declared size). + if v_size > 5242880 or v_size > v_row.declared_size then raise exception using message = 'too_large', errcode = 'P0001'; end if; update public.assets a set status = 'uploaded' where a.id = v_row.id; diff --git a/supabase/tests/010_files_foundation.sql b/supabase/tests/010_files_foundation.sql index 57454618..b4706704 100644 --- a/supabase/tests/010_files_foundation.sql +++ b/supabase/tests/010_files_foundation.sql @@ -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(130); +select plan(137); insert into auth.users (id, email) values ('40000000-0000-0000-0000-00000000000a', 'alice-files@test.local'), @@ -497,18 +497,56 @@ 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. +-- Deleted rows still count while their objects wait in the purge queue. +update public.assets set status = 'deleted' where owner_id = '40000000-0000-0000-0000-00000000000c' and status = 'uploaded'; +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', 'objects still queued for purge count toward the ceiling'); +reset role; +delete from private.object_purge_queue where object_name like '%/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 lives_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'served purge entries free the ceiling'); +reset role; +-- Carol's pending row (begun above): the object may not exceed the declared size. +set local role authenticated; +select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true); +insert into pg_temp.ids select 'c1', id from public.assets where owner_id = '40000000-0000-0000-0000-00000000000c' and status = 'pending_upload'; +insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('c1')).quarantine_key, '{"size":4000}'); +select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('c1')) $$, 'P0001', 'too_large', 'an object larger than the declared size is refused at confirm'); +reset role; +update storage.objects set metadata = '{"size":1000}' where name = (pg_temp.row('c1')).quarantine_key; +set local role authenticated; +select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true); +select lives_ok($$ select public.confirm_asset_upload(pg_temp.id('c1')) $$, 'carol confirms within the declared size'); +reset role; +set local role service_role; +select set_config('request.jwt.claims', '{"role":"service_role"}', true); +insert into pg_temp.ids select 'tokc1', lock_token from public.claim_avatar_jobs(1); +select throws_ok( + $$ select public.complete_avatar_job(pg_temp.id('c1'), pg_temp.id('tokc1'), 30000, null) $$, + 'P0001', 'invalid_output', 'a null sha256 is refused by the output guard' +); +reset role; +-- Deactivated account: begin, confirm and the upload policy refuse it. 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'); +update public.assets set status = 'pending_upload', locked_at = null, lock_token = null, attempts = 0 where id = pg_temp.id('c1'); +delete from storage.objects where name = (pg_temp.row('c1')).quarantine_key; +set local role authenticated; +select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true); 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)' + $$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('c1')).quarantine_key, '{"size":1000}') $$, + '42501', null, 'a deactivated account cannot upload at its pending quarantine key' ); +reset role; +select ok(not has_function_privilege('service_role', 'public.begin_avatar_upload(text, integer)', 'execute'), 'begin_avatar_upload is not granted to service_role'); +select ok(not has_function_privilege('service_role', 'public.confirm_asset_upload(uuid)', 'execute'), 'confirm_asset_upload is not granted to service_role'); +select ok(not has_function_privilege('service_role', 'public.remove_avatar()', 'execute'), 'remove_avatar is not granted to service_role'); select * from finish(); rollback;