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)') }) })