Files
faunosteo/packages/supabase/tests/sandbox/target.ts
T
bchanot b682f78209 test(supabase): sandbox suites refuse every target but the sandbox
The sandbox suites (stack.ts and its importers, the shell Playwright
config) stop before their first request unless the container, the
four loopback URLs and the key issuer are exactly the db:sandbox
ones; the LAN config accepts the 5193 trial app only. Drift and
wiring tests tie the constants to scripts/db-sandbox.sh.
2026-11-10 13:36:08 +01:00

67 lines
2.8 KiB
TypeScript

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