fix(07-02): fail-closed guard on global-setup seed (CR-01, data-loss prevention)

global-setup.ts TRUNCATEs four tables against whatever DB_* points at, with no
production guard — an operator with prod DB_* still exported could wipe lists/
list_items/list_shares/calendar_events. The README promised a DEV_AUTH_BYPASS
guardrail the code never enforced. Adds a fail-closed guard mirroring
apps/api/src/auth/devBypass.ts: hard NODE_ENV==='production' check first, then
require DEV_AUTH_BYPASS==='true' before opening any DB connection. README updated
with the test-process env requirement (run command + CI runner env).

Verified: guard throws without DEV_AUTH_BYPASS; full 58-test suite passes with it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Lucas Berger
2026-06-11 02:23:38 -04:00
co-authored by Claude Opus 4.8
parent 8458dc25eb
commit fcc680e553
2 changed files with 32 additions and 1 deletions
+20
View File
@@ -23,6 +23,26 @@
import mysql from 'mysql2/promise'
export default async function globalSetup(): Promise<void> {
// ── Step 0: Fail-closed environment guard (CR-01 — data-loss prevention) ─────
// This setup TRUNCATEs four tables against whatever DB_* points at. Mirror the
// hard guard in apps/api/src/auth/devBypass.ts so an operator with prod DB_*
// still exported can never wipe production data.
// 1. NODE_ENV === 'production' is the hard FIRST guard (checked before any
// other env var), matching devBypass.ts.
// 2. The harness contract requires DEV_AUTH_BYPASS=true (the same flag the API
// needs to serve Dev User 1) — refuse to seed without it.
if (process.env.NODE_ENV === 'production') {
throw new Error(
'global-setup refused: NODE_ENV=production. The E2E seed TRUNCATEs tables and must never run against production.',
)
}
if (process.env.DEV_AUTH_BYPASS !== 'true') {
throw new Error(
'global-setup refused: DEV_AUTH_BYPASS is not "true". The harness only runs against a dev-bypass stack; ' +
'refusing to TRUNCATE/seed an unconfirmed database. Export DEV_AUTH_BYPASS=true (and point DB_* at the dev DB) to proceed.',
)
}
// ── Step 1: Readiness gate (D-08) ───────────────────────────────────────────
const baseURL = process.env.PLAYWRIGHT_BASE_URL ?? 'http://localhost:5173'
const deadline = Date.now() + 60_000