fix(supabase): e2e:avatar runs on the sandbox only, DEV runner removed

scripts/avatar-e2e.sh read the DEV keys through `supabase status`, started
the worker on the DEV network and let the suite write and purge on DEV.
Port the V1-2 sandbox runner (`db-sandbox.sh avatar-e2e`: loopback gateway,
worker in a 600 env file) and the guarded suite (fixed URL, container and
key issuer, per-run accounts); refusals and the positive run proven under
strace in .claude/audits/2026-11-02-avatar-e2e-sandbox-only.md.
This commit is contained in:
bchanot committed 2026-11-02 10:45:03 +01:00
1 parent 6287e5bb9f
commit 3a4614bc86
17 files changed
+589 -117

No files matched your search

+61 -32
View File
@@ -1,14 +1,17 @@
/**
* Avatar flow against the real local stack and the worker container
* Avatar flow against a real stack and the worker container
* (docs/development/STORAGE.md §4, §3; STEP8_FILES_ANALYSIS IV.2 "integration"
* and "real-stack e2e"). Run through `pnpm e2e:avatar` (scripts/avatar-e2e.sh),
* which starts the worker with a 5 s sweep and a 0 s quarantine grace.
* and "real-stack e2e"). Run through `pnpm e2e:avatar` (= `pnpm db:sandbox
* avatar-e2e`): the throwaway stack only, the worker started with a 5 s sweep and
* a 0 s quarantine grace.
*
* Environment: SUPABASE_URL, SUPABASE_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY
* (the service key is used for inspection only: listing objects and shifting
* the purge clock through psql in the database container).
* the purge clock through psql) and AVATAR_E2E_DB_CONTAINER. The URL and the
* container must be the sandbox's own: any other value, or a missing one, fails
* the run before any request. Both accounts are made per run.
*/
import { createHash } from 'node:crypto'
import { createHash, randomBytes } from 'node:crypto'
import { execFileSync } from 'node:child_process'
import { deflateSync } from 'node:zlib'
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
@@ -26,15 +29,28 @@ import {
const URL_ = process.env.SUPABASE_URL
const ANON = process.env.SUPABASE_ANON_KEY
const SERVICE = process.env.SUPABASE_SERVICE_ROLE_KEY
const DB_CONTAINER = process.env.AVATAR_E2E_DB_CONTAINER ?? 'supabase_db_faunosteo'
const PASSWORD = 'LocalDev-only-password-1'
const BOB = 'bob@faunosteo.local'
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'
/** 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')
const WORKER_WAIT_MS = 60_000
type Client = SupabaseClient<Database>
function anonClient(): Client {
return createClient<Database>(URL_ ?? '', ANON ?? '', {
return createClient<Database>(SANDBOX_URL, ANON ?? '', {
auth: { persistSession: false, autoRefreshToken: false },
})
}
@@ -47,7 +63,7 @@ async function signedIn(email: string): Promise<Client> {
}
function serviceClient(): Client {
return createClient<Database>(URL_ ?? '', SERVICE ?? '', {
return createClient<Database>(SANDBOX_URL, SERVICE ?? '', {
auth: { persistSession: false, autoRefreshToken: false },
})
}
@@ -166,19 +182,24 @@ async function objectExists(service: Client, bucket: string, key: string): Promi
function psql(sql: string): string {
return execFileSync('docker', [
'exec', DB_CONTAINER, 'psql', '-U', 'postgres', '-d', 'postgres', '-Atc', sql,
'exec', SANDBOX_DB, 'psql', '-U', 'postgres', '-d', 'postgres', '-Atc', sql,
]).toString().trim()
}
/** Moves every due date of the purge queue to now (the grace is 48 h / 1 h in production). */
function expeditePurges(): void {
psql('update private.object_purge_queue set not_before = now() where not_before > now()')
function accountPattern(userId: string): string {
if (!/^[0-9a-f-]{36}$/.test(userId)) throw new Error('userId is not a uuid')
return `'%/${userId}/%'`
}
/** Moves an account's due dates of the purge queue to now (the grace is 48 h / 1 h in production). */
function expeditePurges(userId: string): void {
psql(`update private.object_purge_queue set not_before = now()
where not_before > now() and object_name like ${accountPattern(userId)}`)
}
/** 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}/%'`))
return Number(psql(`select count(*) from private.object_purge_queue where object_name like ${accountPattern(userId)}`))
}
async function downloadFails(client: Client, key: string): Promise<boolean> {
@@ -192,31 +213,39 @@ function sha256(bytes: Uint8Array): string {
// --- suite ------------------------------------------------------------------
const configured = Boolean(URL_ && ANON && SERVICE)
/** A confirmed throwaway account; resolves with its id. */
async function newAccount(service: Client, tag: string, name: string): Promise<{ id: string; email: string }> {
const email = `e2e-${tag}-${Date.now()}@faunosteo.local`
const created = await service.auth.admin.createUser({
email,
password: PASSWORD,
email_confirm: true,
user_metadata: { display_name: name, locale: 'fr' },
})
if (created.error || !created.data.user) throw new Error(`createUser: ${created.error?.message}`)
return { id: created.data.user.id, email }
}
describe.skipIf(!configured)('avatar flow on the real stack', () => {
describe('avatar flow on the sandbox stack', () => {
const service = serviceClient()
let alice: Client
let aliceId: string
let bob: Client
let bobId: string
beforeAll(async () => {
// A fresh account per run keeps the seeded accounts clean and the per-hour rate out of reach.
const email = `e2e-avatar-${Date.now()}@faunosteo.local`
const created = await service.auth.admin.createUser({
email,
password: PASSWORD,
email_confirm: true,
user_metadata: { display_name: 'E2E Avatar', locale: 'fr' },
})
if (created.error || !created.data.user) throw new Error(`createUser: ${created.error?.message}`)
aliceId = created.data.user.id
alice = await signedIn(email)
bob = await signedIn(BOB)
// Fresh accounts per run keep the seeded accounts out of the suite and the per-hour rate out of reach.
const a = await newAccount(service, 'avatar', 'E2E Avatar')
aliceId = a.id
alice = await signedIn(a.email)
const b = await newAccount(service, 'other', 'E2E Other')
bobId = b.id
bob = await signedIn(b.email)
})
afterAll(async () => {
if (aliceId) await service.auth.admin.deleteUser(aliceId)
if (bobId) await service.auth.admin.deleteUser(bobId)
})
it('cleans a valid upload, and only the owner can download the result', async () => {
@@ -260,7 +289,7 @@ describe.skipIf(!configured)('avatar flow on the real stack', () => {
expect(state.clean?.id).toBe(row.id)
expect(await downloadFails(alice, previous!.object_key)).toBe(true)
expect(await objectExists(service, 'avatars', previous!.object_key)).toBe(true)
expeditePurges()
expeditePurges(aliceId)
await waitFor(
async () => ((await objectExists(service, 'avatars', previous!.object_key)) ? null : true),
30_000,
@@ -316,7 +345,7 @@ describe.skipIf(!configured)('avatar flow on the real stack', () => {
expect(await downloadFails(alice, row.object_key)).toBe(true)
const state = await fetchOwnAvatarState(alice, aliceId)
expect(state.clean).toBeNull()
expeditePurges()
expeditePurges(aliceId)
await waitFor(
async () => {
const listed = await service.storage.from('avatars').list(`profiles/${aliceId}`, { limit: 100 })