import { type APIRequestContext, expect, type Page, test } from '@playwright/test' import { waitForEnrolmentLink } from './enrolment' import { startSmtpSink } from './smtp-sink' import { secondTestAdmin, totp } from './totp' /** * twoFactor.confirmByEmail on the BUILT site (pnpm e2e:built, webpack and Turbopack; security audit 2026-12, F0). * Before 1.18.0 the password of an account inside its setup window was enough to enrol a factor of one's own — the * owner was then locked out. Now the first set-up waits for a link sent to the account's e-mail (read here from the * mail sink), opened in the browser that scanned the code. */ test.describe.configure({ mode: 'serial' }) const stamp = Date.now() const VICTIM = { email: `e2e-enrol-${stamp}@example.com`, password: `enrol-${stamp}-Hk29dLq` } const ATTACKER_SECRET = 'JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP' let sink: Awaited> let victimId: string | undefined type Ctx = { request: { newContext: (o: object) => Promise } } const ctx = (playwright: Ctx, baseURL: string) => playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL } }) async function adminApi(playwright: Ctx, baseURL: string) { const api = await ctx(playwright, baseURL) expect((await api.post('/api/users/login', { data: { email: secondTestAdmin(process.env.E2E_ADMIN_EMAIL!), password: process.env.E2E_ADMIN_PASSWORD } })).status()).toBe(200) expect((await (await api.post('/api/verify-totp', { data: { token: totp(process.env.E2E_TOTP_SECRET!) } })).json()).ok).toBe(true) return api } async function typeCode(page: Page, secret: string) { await page.getByRole('textbox').first().click() await page.keyboard.type(totp(secret)) } test.beforeAll(async () => { sink = await startSmtpSink(Number(process.env.E2E_SMTP_PORT ?? 2526)) }) test.afterAll(async ({ baseURL, playwright }) => { await sink?.close() if (victimId && process.env.E2E_ADMIN_EMAIL) { const api = await adminApi(playwright, baseURL!) await api.delete(`/api/users/${victimId}`) await api.dispose() } }) test('2FA enrolment: the password alone no longer takes an account inside its setup window', async ({ baseURL, playwright }) => { test.skip(!process.env.E2E_ADMIN_EMAIL || !process.env.E2E_ADMIN_PASSWORD || !process.env.E2E_TOTP_SECRET, 'E2E_* test admin not configured') const admin = await adminApi(playwright, baseURL!) const created = await admin.post('/api/users', { data: { ...VICTIM, roles: ['editor'] } }) expect(created.status()).toBe(201) victimId = String((await created.json()).doc.id) await admin.dispose() const attacker = await ctx(playwright, baseURL!) expect((await attacker.post('/api/users/login', { data: VICTIM })).status()).toBe(200) const setup = await attacker.post('/api/setup-totp', { data: { secret: ATTACKER_SECRET, token: totp(ATTACKER_SECRET) } }) expect(setup.status()).toBe(202) expect(await setup.json()).toMatchObject({ ok: false, pending: true }) // not signed in with a factor, nothing opens expect(((await (await attacker.get('/api/users/me')).json()) as { user?: { _strategy?: string } }).user?._strategy).not.toBe('totp') expect((await attacker.post('/api/globals/site-settings', { data: {} })).status()).toBe(403) // the link went to the OWNER's mailbox; opened in the owner's own browser it is refused const link = await waitForEnrolmentLink(sink.messages, VICTIM.email) expect(link.origin).toBe(new URL(baseURL!).origin) const owner = await ctx(playwright, baseURL!) const page = await owner.get(link.pathname + link.search) expect(page.status()).toBe(200) expect(await page.text()).toContain('
{ test.skip(!process.env.E2E_ADMIN_EMAIL, 'E2E_* test admin not configured') const person = { email: `e2e-enrol-ui-${stamp}@example.com`, password: `enrolui-${stamp}-Pz71mWx` } const admin = await adminApi(playwright, baseURL!) const created = await admin.post('/api/users', { data: { ...person, roles: ['editor'] } }) expect(created.status()).toBe(201) const id = String((await created.json()).doc.id) const context = await browser.newContext({ baseURL }) const page = await context.newPage() try { await page.goto('/admin/login') await page.fill('#field-email', person.email) await page.fill('#field-password', person.password) await page.click('.form-submit button') await page.waitForURL(/setup-totp/) const secret = await page.locator('input[name="secret"]').inputValue() await typeCode(page, secret) // the package's form shows the answer; the screen stays, and after a reload names where the link went await expect(page.locator('[data-sonner-toast]').first()).toContainText(/e…@example\.com/, { timeout: 15_000 }) await page.reload() await expect(page.locator('[data-iapl="two-factor-pending"]')).toContainText(/e…@example\.com/) // the link, in this browser: a page with one button, then done const link = await waitForEnrolmentLink(sink.messages, person.email) await page.goto(link.toString()) await page.getByRole('button').click() await expect(page.locator('body')).toContainText(/Two-step verification is on|Weryfikacja dwuetapowa jest włączona/) // signed out: password, then the code await page.goto('/admin/login') await page.fill('#field-email', person.email) await page.fill('#field-password', person.password) await page.click('.form-submit button') await page.waitForURL(/verify-totp/, { waitUntil: 'commit' }) await typeCode(page, secret) await expect(page).toHaveTitle(/Dashboard|Pulpit/, { timeout: 20_000 }) // notices: the confirmation mail, then "turned on" expect(sink.messages.filter((m) => m.to.some((to) => to.includes(person.email))).length).toBeGreaterThanOrEqual(2) } finally { await context.close() await admin.delete(`/api/users/${id}`) await admin.dispose() } })