/** * Release smoke for security audits: deploys an app with one planted bug to a test * platform and checks that the deploy scan reports it. Maintainer notes and cleanup * steps: security-smoke-test.md. * * SECURITY_SMOKE_API_URL=https://api.staging.void.cloud \ * SECURITY_SMOKE_CONFIRM=https://api.staging.void.cloud \ * VOID_TOKEN=... node platform/scripts/security-smoke-test.ts */ import { spawn } from 'node:child_process'; import { cpSync, rmSync } from 'node:fs'; import { join } from 'node:path'; import { createInterface } from 'node:readline'; import { setTimeout as delay } from 'node:timers/promises'; import { fileURLToPath } from 'node:url'; import { Agent } from 'undici'; import { createIsolatedPlayground } from '../../test/integration-helpers.ts'; import { createApi, readTarget, redact, runSecuritySmoke, SmokeError, } from './security-smoke-flow.ts'; const workspaceRoot = fileURLToPath(new URL('../..', import.meta.url)); /** A copy of playground/spa with no local env or crons, plus the planted route. */ export function prepareSecuritySmokeFixture() { const fixture = createIsolatedPlayground(workspaceRoot, 'playground/spa'); try { rmSync(join(fixture.root, '.env'), { force: true }); rmSync(join(fixture.root, 'crons'), { recursive: true, force: true }); cpSync(join(workspaceRoot, 'platform/scripts/fixtures/security-smoke'), fixture.root, { recursive: true, }); return fixture; } catch (error) { fixture.cleanup(); throw error; } } /** Runs the built CLI with tokens in env only; each output line is redacted. */ function runCli( args: Array, cwd: string, env: NodeJS.ProcessEnv, secrets: Array, signal: AbortSignal, ) { const cli = join(workspaceRoot, 'packages/void/dist/cli/cli.mjs'); const child = spawn(process.execPath, [cli, ...args], { cwd, env, signal, stdio: ['ignore', 'pipe', 'pipe'], }); for (const [input, output] of [ [child.stdout, process.stdout], [child.stderr, process.stderr], ] as const) { createInterface({ input }).on('line', (line) => output.write(`${redact(line, secrets)}\n`)); } return new Promise((resolve) => { child.on('error', () => resolve(1)); child.on('close', (code) => resolve(code ?? 1)); }); } if (import.meta.main) { let target: ReturnType; try { target = readTarget(process.env); } catch (error) { console.error(error instanceof SmokeError ? error.message : 'Invalid smoke settings.'); process.exit(1); } const { CF_ACCESS_CLIENT_ID: accessId, CF_ACCESS_CLIENT_SECRET: accessSecret } = process.env; const secrets = [target.token, accessId, accessSecret].filter((value) => value !== undefined); const log = (line: string) => console.log(redact(line, secrets)); const slug = `security-smoke-${Date.now().toString(36)}`; // First signal: stop the run and let cleanup finish. Second: quit and say what may be left. const stop = new AbortController(); const onSignal = () => { if (!stop.signal.aborted) { console.error('Stopping: cleaning up first. Send the signal again to quit at once.'); stop.abort(); return; } console.error( `Quit before cleanup. Manual cleanup may be needed on ${target.origin}: void project delete ${slug} --force; void platform security policy disabled (only if this run turned it on)`, ); process.exit(1); }; process.on('SIGINT', onSignal); process.on('SIGTERM', onSignal); const noHeaderWait = new Agent({ headersTimeout: 0, bodyTimeout: 0 }); let fixture: ReturnType | undefined; try { process.exitCode = await runSecuritySmoke({ api: createApi({ origin: target.origin, token: target.token, headers: accessId && accessSecret ? { 'CF-Access-Client-Id': accessId, 'CF-Access-Client-Secret': accessSecret } : {}, // Leaving Off runs a probe that can answer after fetch's default 300-second wait for // headers. Each call still ends by its own timeout signal. fetch: (url, init) => globalThis.fetch(url, Object.assign({}, init, { dispatcher: noHeaderWait })), now: Date.now, }), origin: target.origin, slug, signal: stop.signal, async deploy(slug) { fixture = prepareSecuritySmokeFixture(); const env = { ...process.env, VOID_TOKEN: target.token, VOID_API_URL: target.origin, VOID_ACCESS_ORIGIN: target.origin, }; const connect = await runCli( ['connect', target.origin, '--no-login'], fixture.root, env, secrets, stop.signal, ); return connect === 0 ? runCli(['deploy', '--project', slug], fixture.root, env, secrets, stop.signal) : connect; }, sleep: (ms) => delay(ms, undefined, { signal: stop.signal }), now: Date.now, log, }); } finally { fixture?.cleanup(); await noHeaderWait.close(); } }