diff --git a/apps/app/e2e/playwright.lan.config.ts b/apps/app/e2e/playwright.lan.config.ts index e91293c4..9d687f9f 100644 --- a/apps/app/e2e/playwright.lan.config.ts +++ b/apps/app/e2e/playwright.lan.config.ts @@ -9,6 +9,12 @@ import { defineConfig } from '@playwright/test' import type { GuardOptions } from './support/browser' const ORIGIN = process.env.TRIAL_ORIGIN ?? 'http://192.168.1.100:5193' +// Port check only: 5193 is the app of pnpm db:sandbox trial; the db:trial app (5194) is never +// a test target. +if (new URL(ORIGIN).port !== '5193') + throw new Error( + 'Refused: TRIAL_ORIGIN must be the app of pnpm db:sandbox trial (port 5193)', + ) export default defineConfig({ testDir: './lan', diff --git a/apps/app/e2e/playwright.shell.config.ts b/apps/app/e2e/playwright.shell.config.ts index 6bd70fa4..02b7d008 100644 --- a/apps/app/e2e/playwright.shell.config.ts +++ b/apps/app/e2e/playwright.shell.config.ts @@ -7,11 +7,14 @@ * session tokens and the typed passwords. */ import { defineConfig } from '@playwright/test' +import { assertSandboxTarget } from '../../../packages/supabase/tests/sandbox/target' import type { GuardOptions } from './support/browser' import { PROJECTS } from './support/browser' const ORIGIN = 'http://127.0.0.1:5192' const FAKE_SUPABASE_ORIGIN = 'http://sandbox.faunosteo.invalid' +// The sandbox's exact values or nothing starts (the db:trial stack, DEV, look-alikes are refused). +assertSandboxTarget(process.env) const anonKey = process.env.SANDBOX_ANON_KEY if (!anonKey) throw new Error( diff --git a/packages/supabase/tests/sandbox-target.test.ts b/packages/supabase/tests/sandbox-target.test.ts new file mode 100644 index 00000000..5aee8fa8 --- /dev/null +++ b/packages/supabase/tests/sandbox-target.test.ts @@ -0,0 +1,127 @@ +import { readFileSync } from 'node:fs' +import { resolve } from 'node:path' +import { describe, expect, it } from 'vitest' +import { + assertSandboxTarget, + jwtIssuer, + sandboxTargetProblems, +} from './sandbox/target' + +const b64 = (value: unknown) => + Buffer.from(JSON.stringify(value)).toString('base64url') +const key = (claims: Record) => + `${b64({ alg: 'HS256', typ: 'JWT' })}.${b64(claims)}.signature` + +/** What scripts/db-sandbox.sh exports through with_sandbox_env. */ +const SANDBOX: Record = { + SANDBOX_DB_CONTAINER: 'faunosteo_dbsandbox_db', + SANDBOX_AUTH_URL: 'http://127.0.0.1:57999', + SANDBOX_REST_URL: 'http://127.0.0.1:57300', + SANDBOX_STORAGE_URL: 'http://127.0.0.1:57500', + SANDBOX_MAIL_URL: 'http://127.0.0.1:57025', + SANDBOX_ANON_KEY: key({ role: 'anon', iss: 'db-sandbox' }), + SANDBOX_SERVICE_KEY: key({ role: 'service_role', iss: 'db-sandbox' }), +} +const TRIAL_SERVICE_KEY = key({ role: 'service_role', iss: 'faunosteo-trial' }) + +describe('sandbox target guard', () => { + it('accepts the environment db-sandbox.sh writes', () => { + expect(sandboxTargetProblems(SANDBOX)).toEqual([]) + expect(() => assertSandboxTarget(SANDBOX)).not.toThrow() + }) + + // One refusal per value: the db:trial stack's own value in each variable. + it.each([ + ['SANDBOX_DB_CONTAINER', 'faunosteo_trial_db'], + ['SANDBOX_AUTH_URL', 'http://127.0.0.1:58999'], + ['SANDBOX_REST_URL', 'http://127.0.0.1:58300'], + ['SANDBOX_STORAGE_URL', 'http://127.0.0.1:58500'], + ['SANDBOX_MAIL_URL', 'http://127.0.0.1:58025'], + ['SANDBOX_ANON_KEY', key({ role: 'anon', iss: 'faunosteo-trial' })], + ['SANDBOX_SERVICE_KEY', TRIAL_SERVICE_KEY], + ])('refuses the trial value of %s', (name, value) => { + const env = { ...SANDBOX, [name]: value } + const problems = sandboxTargetProblems(env) + expect(problems).toHaveLength(1) + expect(problems[0]).toContain(name) + expect(() => assertSandboxTarget(env)).toThrow(/db:sandbox stack only/) + }) + + it('refuses a missing value and a key that is not a JWT', () => { + expect( + sandboxTargetProblems({ ...SANDBOX, SANDBOX_REST_URL: undefined }), + ).toHaveLength(1) + expect( + sandboxTargetProblems({ ...SANDBOX, SANDBOX_ANON_KEY: 'not-a-jwt' }), + ).toHaveLength(1) + }) + + it('names variables, never the values it refused', () => { + const refused: Record = { + SANDBOX_DB_CONTAINER: 'faunosteo_trial_db', + SANDBOX_AUTH_URL: 'http://127.0.0.1:58999', + SANDBOX_REST_URL: 'http://127.0.0.1:58300', + SANDBOX_STORAGE_URL: 'http://127.0.0.1:58500', + SANDBOX_MAIL_URL: 'http://127.0.0.1:58025', + SANDBOX_ANON_KEY: key({ role: 'anon', iss: 'faunosteo-trial' }), + SANDBOX_SERVICE_KEY: TRIAL_SERVICE_KEY, + } + for (const [name, value] of Object.entries(refused)) { + let message = '' + try { + assertSandboxTarget({ ...SANDBOX, [name]: value }) + } catch (error) { + message = (error as Error).message + } + expect(message).toMatch(new RegExp(name)) + expect(message).not.toContain(value) + } + }) +}) + +describe('jwtIssuer', () => { + const payload = (value: string) => + `h.${Buffer.from(value).toString('base64url')}.s` + it.each([ + ['array payload', payload('[1]')], + ['null payload', payload('null')], + ['primitive payload', payload('7')], + ['missing iss', payload('{"role":"anon"}')], + ['non-string iss', payload('{"iss":1}')], + ['two-part token', 'header.payload'], + ['empty string', ''], + ['non-JSON payload', payload('not json')], + ])('returns null for %s', (_label, token) => { + expect(jwtIssuer(token)).toBeNull() + }) + + it('reads a padded base64 payload', () => { + const padded = Buffer.from('{"iss":"db-sandbox"}').toString('base64') + expect(padded.endsWith('=')).toBe(true) + expect(jwtIssuer(`h.${padded}.s`)).toBe('db-sandbox') + }) +}) + +describe('sandbox target wiring', () => { + const repo = resolve(__dirname, '../../..') + const read = (path: string) => readFileSync(resolve(repo, path), 'utf8') + + it('still matches the values scripts/db-sandbox.sh writes', () => { + const script = read('scripts/db-sandbox.sh') + for (const text of [ + 'DB=faunosteo_dbsandbox_db', + 'PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025', + 'iss: "db-sandbox"', + 'http://127.0.0.1:$PORT_', + ]) + expect(script).toContain(text) + }) + + it('keeps the guard called by the stack and the shell config', () => { + for (const path of [ + 'packages/supabase/tests/sandbox/stack.ts', + 'apps/app/e2e/playwright.shell.config.ts', + ]) + expect(read(path)).toContain('assertSandboxTarget(process.env)') + }) +}) diff --git a/packages/supabase/tests/sandbox/stack.ts b/packages/supabase/tests/sandbox/stack.ts index cebab80a..08cfb3e2 100644 --- a/packages/supabase/tests/sandbox/stack.ts +++ b/packages/supabase/tests/sandbox/stack.ts @@ -3,10 +3,14 @@ * real GoTrue, PostgREST and PostgreSQL sessions, never the DEV stack. The * script passes the endpoints and the generated keys through the environment; * the database password reaches `docker exec` through the environment too, - * never through a command argument. + * never through a command argument. Any other target (the db:trial stack, DEV, + * a look-alike) stops the suite here, before its first request (./target.ts). */ import { execFile } from 'node:child_process' import { randomBytes, randomUUID } from 'node:crypto' +import { assertSandboxTarget } from './target' + +assertSandboxTarget(process.env) const need = (name: string): string => { const value = process.env[name] diff --git a/packages/supabase/tests/sandbox/target.ts b/packages/supabase/tests/sandbox/target.ts new file mode 100644 index 00000000..eecd0484 --- /dev/null +++ b/packages/supabase/tests/sandbox/target.ts @@ -0,0 +1,66 @@ +/** + * The one stack the sandbox suites may reach: the throwaway stack of `pnpm db:sandbox` + * (scripts/db-sandbox.sh), whose runner passes exactly these values. Anything else, such as the + * persistent trial stack of `pnpm db:trial` (containers faunosteo_trial_*, ports 58xxx, keys + * signed for the issuer faunosteo-trial), DEV (54321) or a look-alike address, stops a suite + * before its first request (docs/development/TRIAL_STACK.md ยง3.6). Exact string equality, as + * avatar-flow.e2e.ts and sheet-assets-e2e.sh do. Messages name variables, never their values. + * The key issuer is read without verifying the signature: an accident guard, not authentication. + */ + +/** The exact values scripts/db-sandbox.sh writes (`with_sandbox_env`, `new_secrets`). */ +export const SANDBOX_TARGET = { + container: 'faunosteo_dbsandbox_db', + issuer: 'db-sandbox', + urls: { + SANDBOX_AUTH_URL: 'http://127.0.0.1:57999', + SANDBOX_REST_URL: 'http://127.0.0.1:57300', + SANDBOX_STORAGE_URL: 'http://127.0.0.1:57500', + SANDBOX_MAIL_URL: 'http://127.0.0.1:57025', + }, +} as const + +type Env = Readonly> + +const KEY_VARIABLES = ['SANDBOX_ANON_KEY', 'SANDBOX_SERVICE_KEY'] as const + +/** The `iss` claim of a JWT, or null when the value is not a readable JWT payload. */ +export function jwtIssuer(token: string | undefined): string | null { + const payload = token?.split('.')[1] + if (!payload) return null + try { + const claims: unknown = JSON.parse( + Buffer.from(payload, 'base64url').toString('utf8'), + ) + if (typeof claims !== 'object' || claims === null || Array.isArray(claims)) + return null + const iss = (claims as { iss?: unknown }).iss + return typeof iss === 'string' ? iss : null + } catch { + return null + } +} + +/** Every way `env` differs from the sandbox runner's environment; empty when it is the same. */ +export function sandboxTargetProblems(env: Env): string[] { + const problems: string[] = [] + if (env.SANDBOX_DB_CONTAINER !== SANDBOX_TARGET.container) + problems.push(`SANDBOX_DB_CONTAINER is not ${SANDBOX_TARGET.container}`) + for (const [name, expected] of Object.entries(SANDBOX_TARGET.urls)) + if (env[name] !== expected) problems.push(`${name} is not ${expected}`) + for (const name of KEY_VARIABLES) + if (jwtIssuer(env[name]) !== SANDBOX_TARGET.issuer) + problems.push( + `${name} is not signed for the issuer ${SANDBOX_TARGET.issuer}`, + ) + return problems +} + +/** Throws before any request when the environment is not the sandbox runner's. */ +export function assertSandboxTarget(env: Env = process.env): void { + const problems = sandboxTargetProblems(env) + if (problems.length > 0) + throw new Error( + `Refused: this suite runs against the db:sandbox stack only (pnpm db:sandbox ...). ${problems.join('; ')}.`, + ) +}