2FA: the first set-up is confirmed by e-mail (twoFactor.confirmByEmail, 1.18.0)

Security audit 2026-12, F0: inside an account's setup window its password alone
enrolled an authenticator of the caller's choosing; the owner was locked out.

- /setup-totp for an account without a factor: IAPL checks the code, keeps the
  secret pending (payload-kv), mails a single-use 30-min link to the account and
  ties it to the enrolling browser; the package's handler (and its -totp cookie)
  is never used for a first enrolment
- GET /api/iapl/two-factor/confirm = a page with one button; POST confirms
  (own origin, same browser), ends the account's sessions, signs nobody in
- notice mails on every enrolment, change, removal / reset (any path)
- mail not working: link to the server log, admin confirms on the account page
  (POST /api/iapl/two-factor/confirm-pending), doctor rule 2fa-enrolment-mail
- options and opt-out (confirmByEmail: false), building blocks exported
- tests: HTTP suite on SQLite, MongoDB, Postgres; built-site spec in e2e:built;
  closed doors and judged routes; docs, AGENT.md, skills, changelog

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 13:37:39 +01:00
co-authored by Claude Opus 5.5
parent 84717804ae
commit c9d1f4b340
41 changed files with 1513 additions and 42 deletions
+23 -7
View File
@@ -1,10 +1,12 @@
import { type APIRequestContext, expect, type Page, test } from '@playwright/test'
import { waitForEnrolmentLink } from './enrolment'
import { startSmtpSink } from './smtp-sink'
import { secondTestAdmin, totp } from './totp'
/**
* "2FA setup required" in a real browser: an account that has not set two-step verification up sees only the setup
* screen and gets nothing from the API; after the code it is in. Covers the first admin of a fresh site (only when
* screen and gets nothing from the API; after the code and the e-mailed link (twoFactor.confirmByEmail) it is in. Covers the first admin of a fresh site (only when
* the database has no users yet — run this file before e2e/create-test-admin.ts for that), a new editor, and the
* administrator's reset on the account page.
* E2E_BASE_URL=http://localhost:3217 pnpm exec playwright test e2e/two-factor-setup.spec.ts
@@ -43,7 +45,7 @@ async function adminApi(playwright: { request: { newContext: (o: object) => Prom
return api
}
test('first admin of a fresh site: created, sent to 2FA setup, in after the code', async ({ baseURL, page }) => {
test('first admin of a fresh site: created, sent to 2FA setup; with no mail yet the set-up waits for the link in the server log', async ({ baseURL, page }) => {
// With users in the database this address leads to the login screen instead.
await page.goto('/admin/create-first-user')
const firstUserForm = await page.locator('#field-confirm-password').waitFor({ timeout: 10_000 }).then(() => true, () => false)
@@ -65,9 +67,10 @@ test('first admin of a fresh site: created, sent to 2FA setup, in after the code
}
const secret = await page.locator('input[name="secret"]').inputValue()
await typeCode(page, secret)
await expect(page).toHaveTitle(/Dashboard|Pulpit/, { timeout: 20_000 })
expect((await page.request.get('/api/globals/site-integrations', { headers: { origin: baseURL! } })).status()).toBe(200)
admin = { ...OWNER, secret }
// twoFactor.confirmByEmail: a fresh site has no mail yet, so the set-up waits and its link is in the server log
// (the person who set the site up opens it there); nothing opens before that
await expect(page.locator('[data-sonner-toast]').first()).toContainText(/administrator|log/i, { timeout: 15_000 })
expect((await page.request.get('/api/globals/site-integrations', { headers: { origin: baseURL! } })).status()).toBe(403)
})
test('a new editor: setup screen with the deadline, nothing else, in after the code', async ({ baseURL, page, playwright }) => {
@@ -95,9 +98,22 @@ test('a new editor: setup screen with the deadline, nothing else, in after the c
await page.waitForURL(/setup-totp/)
const secret = await page.locator('input[name="secret"]').inputValue()
// twoFactor.confirmByEmail: the code starts the set-up, the e-mailed link (opened in this browser) finishes it
const sink = await startSmtpSink(Number(process.env.E2E_SMTP_PORT ?? 2526))
try {
await typeCode(page, secret)
const link = await waitForEnrolmentLink(sink.messages, EDITOR.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/)
} finally {
await sink.close()
}
// signed out by the confirmation: password and code, then the data opens
await signIn(page, EDITOR)
await page.waitForURL(/verify-totp/, { waitUntil: 'commit' })
await typeCode(page, secret)
// back where it wanted to go, and the data opens
await page.waitForURL(/\/admin\/collections\/pages/, { timeout: 20_000 })
await expect(page).toHaveTitle(/Dashboard|Pulpit/, { timeout: 20_000 })
expect((await page.request.get('/api/pages', { headers: { origin: baseURL! } })).status()).toBe(200)
})