From 3c141c942b5b67f88497fe66fc11c3ec0852b38e Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 1 Nov 2026 23:13:42 +0100 Subject: [PATCH] fix(supabase): targeted review LOWs on retry and sandbox isolation fail_sheet_verification refuses lease_expired (the claim's record, never a worker report); the sandbox state dir guard resolves the repository path; the avatar suite also refuses keys not issued by the sandbox. pgTAP 021: 90. --- packages/supabase/tests/e2e/avatar-flow.e2e.ts | 10 +++++++++- scripts/db-sandbox.sh | 4 +++- .../20261101100000_sheet_verification_jobs.sql | 4 +++- supabase/tests/021_sheet_verification_jobs.sql | 4 +++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/supabase/tests/e2e/avatar-flow.e2e.ts b/packages/supabase/tests/e2e/avatar-flow.e2e.ts index 5beda495..054a4c36 100644 --- a/packages/supabase/tests/e2e/avatar-flow.e2e.ts +++ b/packages/supabase/tests/e2e/avatar-flow.e2e.ts @@ -33,7 +33,15 @@ const DB_CONTAINER = process.env.AVATAR_E2E_DB_CONTAINER /** The only target accepted: the gateway and database of `pnpm db:sandbox avatar-e2e`. */ const SANDBOX_URL = 'http://127.0.0.1:57800' const SANDBOX_DB = 'faunosteo_dbsandbox_db' -if (URL_ !== SANDBOX_URL || DB_CONTAINER !== SANDBOX_DB || !ANON || !SERVICE) +/** The sandbox signs its keys with issuer `db-sandbox` (scripts/db-sandbox.sh); no other stack's key does. */ +const sandboxKey = (key: string | undefined): boolean => { + try { + return JSON.parse(Buffer.from(key?.split('.')[1] ?? '', 'base64url').toString()).iss === 'db-sandbox' + } catch { + return false + } +} +if (URL_ !== SANDBOX_URL || DB_CONTAINER !== SANDBOX_DB || !sandboxKey(ANON) || !sandboxKey(SERVICE)) throw new Error('avatar e2e: run it through pnpm e2e:avatar, on the sandbox stack only') /** Per run, kept in memory only: both accounts are deleted after the suite. */ const PASSWORD = randomBytes(18).toString('base64url') diff --git a/scripts/db-sandbox.sh b/scripts/db-sandbox.sh index 8497537c..5cb2f9a2 100755 --- a/scripts/db-sandbox.sh +++ b/scripts/db-sandbox.sh @@ -53,8 +53,10 @@ SECRETS="$STATE/secrets.sh" CLI=node_modules/.bin/supabase die() { echo "db-sandbox: $*" >&2; exit 1; } +# Both sides resolved: a repository reached through a symlink is still the repository. +REPO="$(realpath "$PWD")" case "$(realpath -m "$STATE")" in - "$PWD" | "$PWD"/*) die "FAUNOSTEO_SANDBOX_DIR must be outside the repository" ;; + "$REPO" | "$REPO"/*) die "FAUNOSTEO_SANDBOX_DIR must be outside the repository" ;; esac # Every value must be present: an empty password would turn --db-url into an # argument the CLI might read as absent and fall back to the DEV database. diff --git a/supabase/migrations/20261101100000_sheet_verification_jobs.sql b/supabase/migrations/20261101100000_sheet_verification_jobs.sql index 597d3448..d76f1b30 100644 --- a/supabase/migrations/20261101100000_sheet_verification_jobs.sql +++ b/supabase/migrations/20261101100000_sheet_verification_jobs.sql @@ -398,7 +398,9 @@ declare v_definitive boolean; begin perform private.require_service_role(); - if code is null or not (code = any (private.sheet_worker_codes())) then + -- `lease_expired` is the claim's own record of a dead lease, never a worker report. + if code is null or not (code = any (private.sheet_worker_codes())) + or code = 'verification.worker.lease_expired' then raise exception 'invalid_input' using errcode = 'P0001'; end if; select r.* into v_review from public.anatomical_sheet_reviews r where r.id = review_id for update; diff --git a/supabase/tests/021_sheet_verification_jobs.sql b/supabase/tests/021_sheet_verification_jobs.sql index 3f028448..9354e8d6 100644 --- a/supabase/tests/021_sheet_verification_jobs.sql +++ b/supabase/tests/021_sheet_verification_jobs.sql @@ -4,7 +4,7 @@ -- nothing), technical failures up to the attempt limit with their progressive -- delay (`not_before`), dead leases. begin; -select plan(89); +select plan(90); insert into auth.users (id, email, email_confirmed_at) values ('f0000000-0000-0000-0000-000000000001', 'own@vj.local', now()), @@ -209,6 +209,8 @@ set local role service_role; select pg_temp.worker(); select is((select count(*)::int from public.claim_sheet_verifications(1)), 1, 'third round claimed'); select throws_ok(format('select public.fail_sheet_verification(%L, %L, %L)', pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.oops'), 'P0001', 'invalid_input', 'an unknown worker code is refused'); +select throws_ok(format('select public.fail_sheet_verification(%L, %L, %L)', pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.lease_expired'), + 'P0001', 'invalid_input', 'lease_expired is the claim''s record, never a worker report'); select is(public.fail_sheet_verification(pg_temp.id('r3'), gen_random_uuid(), 'verification.worker.timeout'), 'stale', 'a failure with another token is stale'); select is(public.fail_sheet_verification(pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.timeout'), 'retry', 'a timeout below the limit is retried'); select is(pg_temp.status('r3') || '/' || (pg_temp.token('r3') is null)::text, 'verifying/-/verification.worker.timeout/true', 'round stays verifying, code recorded, lease released');