/** * 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('; ')}.`, ) }