import { ATTACK_PROBES, createProbeContext } from '@intecion/ipal-kit/doctor' import { expect, test } from '@playwright/test' import { editorTestAccount, totp } from './totp' /** * Attack probes on the BUILT site (security audit 2026-12; docs/security.md → "Security suites"): every probe of * the plugin's ATTACK_PROBES, the live-safe ones and those only for a local build (8 MB bodies), then what needs a * signed-in session: CSRF on the API and the session cookie's flags. `pnpm e2e:built` runs it after a webpack and a * Turbopack build. A probe tied to an open audit finding is expected to fail (test.fail) until the fix lands. * * The secrets the bundle check looks for are this site's own values from reference/.env. */ const env = process.env const dbPassword = (() => { try { return decodeURIComponent(new URL(env.DATABASE_URI ?? '').password) } catch { return '' } })() const secrets = [env.PAYLOAD_SECRET, env.CRON_SECRET, env.IAPL_COOKIE_SCAN_TOKEN, env.IAPL_DOCTOR_TOKEN, env.R2_SECRET_ACCESS_KEY, env.E2E_ADMIN_PASSWORD, env.E2E_TOTP_SECRET, dbPassword].filter((s): s is string => !!s && s.length >= 8) test.describe('attack probes', () => { for (const probe of ATTACK_PROBES) { test(`${probe.class}: ${probe.title}`, async ({ baseURL }) => { if (probe.finding) {test.fail(true, `audit finding ${probe.finding} is open (security-audit-2026-12-findings.md)`)} const { result } = { result: await probe.run(createProbeContext(baseURL!, { page: '/pl', secrets })) } test.skip(!!result.skipped, result.skipped) expect(result.ok, result.detail).toBe(true) }) } }) test.describe('attack probes with a session', () => { test('CSRF: the editor\'s cookie does not work from another origin (REST write, GraphQL mutation)', async ({ baseURL, playwright }) => { const { E2E_ADMIN_EMAIL: adminEmail, E2E_ADMIN_PASSWORD: password, E2E_TOTP_SECRET: secret } = env test.skip(!adminEmail || !password || !secret, 'E2E_* test admin not configured') const api = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL! } }) expect((await api.post('/api/users/login', { data: { email: editorTestAccount(adminEmail!), password } })).status()).toBe(200) expect((await (await api.post('/api/verify-totp', { data: { token: totp(secret!) } })).json()).ok).toBe(true) // the same session, same origin: the write is allowed (proves the cookie works), with a body that changes nothing const own = await api.get('/api/users/me') expect(((await own.json()) as { user?: unknown }).user).toBeTruthy() const foreign = { origin: 'https://evil.example', referer: 'https://evil.example/', 'sec-fetch-site': 'cross-site' } const me = await api.get('/api/users/me', { headers: foreign }) expect([me.status(), ((await me.json()) as { user?: unknown }).user ?? null], 'a cross-site request must not be signed in').toEqual([200, null]) const write = await api.post('/api/globals/header', { data: {}, headers: foreign }) expect([401, 403]).toContain(write.status()) const gql = await api.post('/api/graphql', { data: { query: 'mutation { updateHeader(data: {}) { id } }' }, headers: foreign }) expect(gql.status()).toBeLessThan(500) expect(await gql.text()).not.toMatch(/"updateHeader"\s*:\s*\{/) await api.dispose() }) test('session cookie: HttpOnly and SameSite, never readable by script', async ({ baseURL, playwright }) => { const { E2E_ADMIN_EMAIL: adminEmail, E2E_ADMIN_PASSWORD: password } = env test.skip(!adminEmail || !password, 'E2E_* test admin not configured') const api = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL! } }) const res = await api.post('/api/users/login', { data: { email: editorTestAccount(adminEmail!), password } }) expect(res.status()).toBe(200) const cookie = res.headersArray().filter((h) => h.name.toLowerCase() === 'set-cookie').map((h) => h.value).find((v) => /-token=/.test(v)) expect(cookie, 'the login sets the session cookie').toBeTruthy() expect(cookie).toMatch(/;\s*HttpOnly/i) expect(cookie).toMatch(/;\s*SameSite=(?:Lax|Strict)/i) await api.dispose() }) })