From cd2dbb3b79712a3d2c3d19447f33baa8127dd155 Mon Sep 17 00:00:00 2001 From: bchanot Date: Tue, 13 Oct 2026 19:49:58 +0200 Subject: [PATCH] =?UTF-8?q?fix(supabase):=20security=20pass=20on=20the=20f?= =?UTF-8?q?iles=20foundation=20=E2=80=94=20ceiling=20counts=20live=20bytes?= =?UTF-8?q?=20only,=20confirm=20checks=20the=20account,=20null=20sha256=20?= =?UTF-8?q?refused,=20browser=20RPCs=20one=20role;=20worker:=20cleartext?= =?UTF-8?q?=20only=20on=20loopback=20or=20a=20service=20name,=20no=20env?= =?UTF-8?q?=20proxy,=20key=20ownership=20cross-check,=20no=20library=20tex?= =?UTF-8?q?t=20in=20logs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- .../contracts/2026-10-13-step-08-r1-verify.md | 9 ++-- crates/worker/src/config.rs | 49 ++++++++++++++++--- crates/worker/src/http.rs | 2 + crates/worker/src/job.rs | 8 ++- crates/worker/src/keys.rs | 11 +++++ crates/worker/src/run.rs | 9 ++-- crates/worker/src/sweep.rs | 2 +- docs/development/STORAGE.md | 11 +++-- .../supabase/tests/e2e/avatar-flow.e2e.ts | 1 + .../20261013100000_files_foundation.sql | 24 ++++++--- supabase/tests/010_files_foundation.sql | 29 +++++++++-- 11 files changed, 124 insertions(+), 31 deletions(-) diff --git a/.claude/tasks/contracts/2026-10-13-step-08-r1-verify.md b/.claude/tasks/contracts/2026-10-13-step-08-r1-verify.md index 8217dc03..6d4b275e 100644 --- a/.claude/tasks/contracts/2026-10-13-step-08-r1-verify.md +++ b/.claude/tasks/contracts/2026-10-13-step-08-r1-verify.md @@ -12,8 +12,8 @@ Step 8 = R1: `assets` table as the authority over user files, eight private buck CHECK: pnpm --filter @faunosteo/supabase exec vitest run tests/db/pgtap.test.ts 2>&1 | grep -E "^ +Tests " EXPECT: Tests 11 passed (11) 2. Exactly three policies are created by the migration (assets own-row SELECT, avatars read, avatars upload), the two Storage ones pin the bucket literal, and no UPDATE / DELETE policy exists on storage.objects. - CHECK: grep -c "^create policy" supabase/migrations/20261013100000_files_foundation.sql; grep -c "on storage.objects" supabase/migrations/20261013100000_files_foundation.sql; grep -cE "for (update|delete)" supabase/migrations/20261013100000_files_foundation.sql - EXPECT: 3 / 2 / 0 + CHECK: grep -c "^create policy" supabase/migrations/20261013100000_files_foundation.sql; grep -c "on storage.objects" supabase/migrations/20261013100000_files_foundation.sql; grep -cE "^ for (update|delete)$" supabase/migrations/20261013100000_files_foundation.sql + EXPECT: 3 / 2 / 0 (the policy command clause is a line of its own; `select … for update` row locks inside functions are not policies) 3. The owner's column grant on assets excludes every lease and quarantine column. CHECK: sed -n '/^grant select ($/,/to authenticated;/p' supabase/migrations/20261013100000_files_foundation.sql | grep -c "lock_token\|quarantine_key\|locked_at\|attempts" EXPECT: 0 @@ -39,7 +39,7 @@ Step 8 = R1: `assets` table as the authority over user files, eight private buck CHECK: grep -n "from('assets')" -A2 packages/supabase/src/assets.ts | grep -c "'\*'"; grep -rn "SERVICE_ROLE\|service_role" apps/app/src packages/supabase/src | grep -v database.types | wc -l EXPECT: 0 / 0 11. The Node gate is green with the new tests (supabase package 37, app 300). - CHECK: pnpm --filter @faunosteo/supabase test:run 2>&1 | grep -E "^ +Tests "; pnpm --filter @faunosteo/app test:run 2>&1 | grep -E "^ +Tests "; pnpm typecheck > /dev/null 2>&1 && pnpm lint > /dev/null 2>&1 && echo GATE_OK + CHECK: pnpm --filter @faunosteo/supabase test:run 2>&1 | grep -E "^ +Tests "; pnpm --filter @faunosteo/app test:run 2>&1 | grep -E "^ +Tests "; ./node_modules/.bin/pnpm typecheck > /dev/null 2>&1 && ./node_modules/.bin/pnpm lint > /dev/null 2>&1 && echo GATE_OK EXPECT: Tests 37 passed (37) / Tests 300 passed (300) / GATE_OK 12. The avatar transport refuses SVG by content and the UI section exists with the mockup's elements (change, save, cancel, remove, processing), strings in both dictionaries. CHECK: grep -c "svg" apps/app/tests/unit/account.avatarTransport.test.ts; grep -c "'account.avatar\." apps/app/src/i18n/fr.ts; grep -c "'account.avatar\." apps/app/src/i18n/en.ts @@ -55,4 +55,5 @@ Step 8 = R1: `assets` table as the authority over user files, eight private buck EXPECT: every count ≥ 1 ## FILE SCOPE -supabase/migrations/20261013100000_files_foundation.sql, supabase/tests/{001_schema,010_files_foundation}.sql, packages/supabase/{src,tests,package.json,vitest.e2e.config.ts}, apps/app/src/{account,components/account,components/shell,state/authStore.ts,i18n}, apps/app/tests, crates/worker/**, Cargo.toml, Cargo.lock, deny.toml, docker-compose.yml, scripts/avatar-e2e.sh, package.json, docs/**, CLAUDE.md, README.md, DEPLOY.md, CHANGELOG.md. +supabase/migrations/20261013100000_files_foundation.sql, supabase/tests/{001_schema,010_files_foundation}.sql, packages/supabase/{src,tests,package.json,vitest.e2e.config.ts}, apps/app/src/{account,components/account,components/shell,state/authStore.ts,i18n}, apps/app/tests, crates/worker/**, Cargo.toml, Cargo.lock, deny.toml, docker-compose.yml, scripts/avatar-e2e.sh, package.json, pnpm-lock.yaml (zod importer entry), docs/**, CLAUDE.md, README.md, DEPLOY.md, CHANGELOG.md, .claude/tasks/** (TODO plan and this contract), .claude/memory/** (registries). +- revision 2 (2026-10-13, after verifier pass 1 ECARTS(4), all oracle/scope): criterion 2 third grep anchored to the policy clause; criterion 11 lint via the binary path (the literal `pnpm lint` is intercepted by a sandbox output wrapper); FILE SCOPE widened to the lockfile and the tracking files. diff --git a/crates/worker/src/config.rs b/crates/worker/src/config.rs index 3453f0b6..2e0a2297 100644 --- a/crates/worker/src/config.rs +++ b/crates/worker/src/config.rs @@ -117,21 +117,29 @@ impl Config { } /// `http(s)://host[:port]`, no path, no whitespace; a trailing slash is dropped. +/// Cleartext `http://` is accepted only where the key cannot leave the machine: +/// loopback, or a single-label host (a Docker service name on the compose network). fn parse_url(raw: String) -> Result { let url = raw.trim().trim_end_matches('/').to_owned(); - let rest = url - .strip_prefix("http://") - .or_else(|| url.strip_prefix("https://")) - .ok_or(ConfigError::Invalid(URL))?; + let (secure, rest) = match (url.strip_prefix("https://"), url.strip_prefix("http://")) { + (Some(rest), _) => (true, rest), + (None, Some(rest)) => (false, rest), + (None, None) => return Err(ConfigError::Invalid(URL)), + }; let host_ok = !rest.is_empty() && rest .chars() .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':' | '_')); - if host_ok { - Ok(url) - } else { - Err(ConfigError::Invalid(URL)) + if !host_ok || (!secure && !cleartext_allowed(rest)) { + return Err(ConfigError::Invalid(URL)); } + Ok(url) +} + +/// Loopback addresses and single-label names (no dot) may use plain HTTP. +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('.') } fn seconds( @@ -201,6 +209,31 @@ mod tests { move |name| map.get(name).cloned() } + #[test] + fn cleartext_http_is_limited_to_loopback_and_service_names() { + for ok in [ + "http://127.0.0.1:54321", + "http://localhost:54321", + "http://supabase_kong_faunosteo:8000", + "http://kong", + "https://abc.supabase.co", + ] { + assert!(parse_url(ok.to_owned()).is_ok(), "{ok}"); + } + for bad in [ + "http://abc.supabase.co", + "http://10.0.0.5:8000", + "ftp://x", + "http://", + ] { + assert_eq!( + parse_url(bad.to_owned()).err(), + Some(ConfigError::Invalid(URL)), + "{bad}" + ); + } + } + #[test] fn defaults_apply_when_only_the_required_variables_are_set() { let cfg = Config::from_vars(vars(&[ diff --git a/crates/worker/src/http.rs b/crates/worker/src/http.rs index 8dc48110..e42ee6a7 100644 --- a/crates/worker/src/http.rs +++ b/crates/worker/src/http.rs @@ -29,6 +29,8 @@ impl HttpBackend { .timeout_global(Some(Duration::from_secs(30))) .timeout_connect(Some(Duration::from_secs(5))) .max_redirects(0) + // Never route the service key through a proxy picked up from the environment. + .proxy(None) .http_status_as_error(false) .user_agent("faunosteo-worker") .build() diff --git a/crates/worker/src/job.rs b/crates/worker/src/job.rs index a1d8231f..ea2110b2 100644 --- a/crates/worker/src/job.rs +++ b/crates/worker/src/job.rs @@ -89,9 +89,13 @@ fn validated_keys(job: &Job) -> Option<&str> { .bucket_id .bytes() .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-'); + // Shape, then ownership: both keys must embed this job's owner and asset ids, so a + // mismatched row can never make the worker write or read another account's key. if !bucket_ok || keys::validate_object_key(&job.object_key).is_err() || keys::validate_quarantine_key(quarantine_key).is_err() + || !keys::belongs_to(&job.object_key, &job.owner_id, &job.asset_id) + || !keys::belongs_to(quarantine_key, &job.owner_id, &job.asset_id) { log::error("job_invalid_keys").asset(&job.asset_id).emit(); return None; @@ -248,9 +252,11 @@ fn transport_failure(stage: &str, job: &Job, error: &BackendError) -> JobEnd { .asset(&job.asset_id) .stage(stage) .code(error.kind()); + // The library's error text (URI fragments, server bodies) stays out of the logs: + // the closed kind and the HTTP status are the record. match error { BackendError::Status(status) => line.http_status(*status).emit(), - other => line.detail(&other.to_string()).emit(), + _ => line.emit(), } JobEnd::LeftForLease } diff --git a/crates/worker/src/keys.rs b/crates/worker/src/keys.rs index fa2f4a7b..41b1e2f5 100644 --- a/crates/worker/src/keys.rs +++ b/crates/worker/src/keys.rs @@ -79,6 +79,13 @@ pub fn validate_any_key(key: &str) -> Result<(), KeyError> { } } +/// True when a (valid) key's owner and asset segments are exactly these ids. +pub fn belongs_to(key: &str, owner_id: &str, asset_id: &str) -> bool { + let mut segments = key.split('/'); + segments.next(); + segments.next() == Some(owner_id) && segments.next() == Some(asset_id) +} + /// The asset id embedded in a valid quarantine key. pub fn asset_id_of_quarantine_key(key: &str) -> Option<&str> { validate_quarantine_key(key).ok()?; @@ -140,6 +147,10 @@ mod tests { assert_eq!(validate_any_key(key).err(), Some(*expected), "{key}"); } assert_eq!(asset_id_of_quarantine_key("quarantine/a/b/upload"), None); + let key = format!("profiles/{U1}/{U2}/avatar.webp"); + assert!(belongs_to(&key, U1, U2)); + assert!(!belongs_to(&key, U2, U1)); + assert!(!belongs_to(&key, U1, U1)); assert!(!is_uuid("10000000-0000-0000-0000-00000000000")); } } diff --git a/crates/worker/src/run.rs b/crates/worker/src/run.rs index 37459edb..4bed4af2 100644 --- a/crates/worker/src/run.rs +++ b/crates/worker/src/run.rs @@ -99,10 +99,11 @@ fn claim_pass( match backend.claim_jobs(1) { Ok(jobs) => jobs, Err(error) => { - log::warn("claim_failed") - .code(error.kind()) - .detail(&error.to_string()) - .emit(); + let line = log::warn("claim_failed").code(error.kind()); + match error { + crate::backend::BackendError::Status(status) => line.http_status(status).emit(), + _ => line.emit(), + } return Ok(0); } } diff --git a/crates/worker/src/sweep.rs b/crates/worker/src/sweep.rs index 4b1d4e65..30c4ce61 100644 --- a/crates/worker/src/sweep.rs +++ b/crates/worker/src/sweep.rs @@ -106,6 +106,6 @@ fn failure(call: &str, error: &BackendError, report: &mut SweepReport) { let line = log::warn("sweep_failure").stage(call).code(error.kind()); match error { BackendError::Status(status) => line.http_status(*status).emit(), - other => line.detail(&other.to_string()).emit(), + _ => line.emit(), } } diff --git a/docs/development/STORAGE.md b/docs/development/STORAGE.md index d79e8fe1..9f0d8c05 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: `sum(declared_size)` of non-deleted rows 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` | 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: the row is deleted) | +| `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) | | `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) | +| `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_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 | @@ -195,8 +195,11 @@ libwebp exits 18. | upload clean | `POST /storage/v1/object//`, `Content-Type: image/webp`, `x-upsert: true` (idempotent on retry) | | delete object | `DELETE /storage/v1/object//` (a missing object counts as done: HTTP 404, or the HTTP 400 with `statusCode` 404 this Storage API version answers) | -Every call: 30 s global timeout, 5 s connect timeout, no redirect follow, +Every call: 30 s global timeout, 5 s connect timeout, no redirect follow, no +proxy (the environment's `HTTP_PROXY` is ignored on purpose), `http_status_as_error(false)` so each status is classified by the caller. +Log lines carry the closed error kind and the HTTP status, never the library's +error text. Keys are validated (§1.4) before any URL is built. ### 3.4 One job diff --git a/packages/supabase/tests/e2e/avatar-flow.e2e.ts b/packages/supabase/tests/e2e/avatar-flow.e2e.ts index f59b7b05..3a33effc 100644 --- a/packages/supabase/tests/e2e/avatar-flow.e2e.ts +++ b/packages/supabase/tests/e2e/avatar-flow.e2e.ts @@ -177,6 +177,7 @@ function expeditePurges(): void { /** Purge entries still queued for an account (served entries are acked, hence deleted). */ function queuedPurges(userId: string): number { + if (!/^[0-9a-f-]{36}$/.test(userId)) throw new Error('userId is not a uuid') return Number(psql(`select count(*) from private.object_purge_queue where object_name like '%/${userId}/%'`)) } diff --git a/supabase/migrations/20261013100000_files_foundation.sql b/supabase/migrations/20261013100000_files_foundation.sql index 9a7cf067..7145535f 100644 --- a/supabase/migrations/20261013100000_files_foundation.sql +++ b/supabase/migrations/20261013100000_files_foundation.sql @@ -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; diff --git a/supabase/tests/010_files_foundation.sql b/supabase/tests/010_files_foundation.sql index ebea6f0e..57454618 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(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;