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
+75
View File
@@ -791,6 +791,79 @@ for p in globals/site-integrations form-submissions users; do
done
```
### Set-up confirmed by e-mail (`twoFactor.confirmByEmail`, 1.18.0)
**Why.** Inside an account's setup window the password alone used to be enough to enrol an authenticator — so
whoever learned the password (phishing, a reused password) could enrol THEIR OWN app first: they were in with full
rights, and the owner was locked out. The window opens for every existing account at the first start after an
upgrade to 1.16.6 or later, for every new account, and after every reset (security audit 2026-12, finding F0).
**What happens now (on by default):**
1. The person scans the code and types it on the setup screen (`POST /api/setup-totp`). IAPL checks the code like
the 2FA package does, but **does not turn the factor on**: it keeps the secret aside (payload-kv), sends a link
to the **account's own e-mail address**, and gives this browser a random cookie (`<cookiePrefix>-iapl-enrol`,
HttpOnly). The screen says where the link went (`p…@example.com`); after a reload a notice shows it too.
2. Opening the link (`GET /api/iapl/two-factor/confirm?u=…&t=…`) shows a page with one button — a mail scanner that
opens links changes nothing. The button posts it back.
3. The POST checks: the link's token (stored as a hash; single use; **30 minutes**), that no newer set-up replaced it,
the site's own origin, and — `sameBrowser`, on by default — the cookie of the browser that scanned the code. Then
the factor is written, **every session of the account ends**, and nobody is signed in: the person signs in again
with the password and the code.
- Until then the account stays in its setup state: nothing but the setup screen, as before. The window keeps running.
- A new set-up (scanning again) replaces a waiting one; its link stops working. At most **5 confirmation mails per
account and hour** (`429` after that).
- The link is made from Payload's `serverURL` (`NEXT_PUBLIC_SERVER_URL`), never from the request's `Host`; without
`serverURL` no link is made (logged).
- Why the same browser: someone with the password who starts a set-up gets the link sent to the OWNER, who may open
it thinking it is theirs. With `sameBrowser` that click is refused ("open this link in the browser where you scanned
the code"); the mail also says "if it was not you, do not open the link: someone knows your password".
**Notices.** The account's address gets a mail whenever its factor is turned on (with who confirmed it, when an
administrator did), changed, or removed / reset (with the administrator's address) — whatever path wrote it: the
setup screen, the package's remove and admin endpoints, IAPL's reset, the Local API. Mails never carry the secret.
They go in the language of the panel's fallback language (`i18n.fallbackLanguage`), as kind `account` (the link) or
`security-alert` (notices) through the site's mail (the mail service or SMTP, [email.md](./email.md)).
**When mail does not work** (no sending method chosen, the mail service still pending, SMTP incomplete, or no mail
transport at all) nobody is locked out, and the doctor says it loudly:
- the person's screen says the e-mail could not be sent and to ask an administrator;
- the **link is written to the server log** (one `[iapl] 2FA: … Link (valid 30 min, single use): …` line; Coolify →
Logs) — for the team that runs the site. With `sameBrowser` it still works only in the browser that scanned the
code (the person at the keyboard opens what the team reads out to them). A fresh site's first administrator, who
has no mail yet, finishes this way;
- an **administrator** who is fully signed in (password and code) opens the account page and chooses **"Confirm the
waiting set-up"** (`POST /api/iapl/two-factor/confirm-pending { userId }`; same rule as the reset: `resetAccess`,
own origin) — only after checking with the person, outside the panel, that they started it;
- `iapl-doctor --deep` (or `--url` with `IAPL_DOCTOR_TOKEN`): rule `2fa-enrolment-mail` — **error** while mail does
not work, and the number of set-ups waiting.
**What to do on a site:** make sure e-mail works BEFORE people set up two-step verification (Site integrations →
E-mail sending → "Send a test e-mail"); after an upgrade, tell the staff that the set-up now ends with a link in their
mailbox. Nothing to wire: the endpoints and hooks come with `twoFactor`, the import map does not change.
**Options** — `twoFactor.confirmByEmail`:
| Option | Default | Role |
|---|---|---|
| (the whole) | on | `false` = the 2FA package's behaviour: the code alone turns the factor on (no link, no notices) |
| `linkMinutes` | `30` | how long a link works (1–1440) |
| `sameBrowser` | `true` | the link works only in the browser that scanned the code; `false` = any browser (weaker: the owner can confirm an attacker's set-up by clicking) |
| `adminConfirm` | `true` | an administrator can confirm a waiting set-up from the account page |
| `logLinkWhenMailFails` | `true` | write the link to the server log when the mail cannot be sent |
| `notices` | `true` | notice mails on enrolment, change, removal / reset |
| `maxMailsPerHour` | `5` | confirmation mails per account and hour |
A value that is not understood stops the start (like every switch since 1.17.1). **Building blocks** (exported):
`startEnrolment`, `buildEnrolmentEndpoints`, `buildTotpNoticeHooks`, `sendTwoFactorNotice`, `verifyTotpCode`,
`readPendingEnrolment`, `pendingEnrolmentInfo`, `dropPendingEnrolment`, `countPendingEnrolments`,
`pendingEnrolmentKey`, `mailConfigured`, `resolveConfirmByEmail`, `ENROL_CONFIRM_PATH`, `ENROL_ADMIN_CONFIRM_PATH`.
**Verify:** a new editor signs in, scans the code → the screen says "we sent a link to e…@…" and
`curl -s -o /dev/null -w '%{http_code}' -b <its cookies> <site>/api/globals/site-integrations` is still `403`; the
mail arrives; opened in another browser it answers "open this link in the browser where you scanned the code"; in the
same browser → "Two-step verification is on", then sign-in with password and code works and the mailbox has the
"turned on" notice. The built reference site runs this in `e2e/two-factor-enrolment.spec.ts` (`pnpm e2e:built`).
### Import map (required)
The 2FA screens and the redirect to them are admin components. After adding `twoFactor`, and after every
@@ -1296,6 +1369,8 @@ every answer: `accessAllowed(option, answer)` (exported). The second factor is c
`safeAdminBack`, `wrapTotpViews`, `copyViews`, `changedViewKeys`, `SAFE_TOTP_VIEW`, `SafeTotpView` are exported.
The stale import-map notice that comes with it: `twoFactor.importMapCheck: false`; its parts
(`findImportMapFile`, `missingImportMapEntries`) are exported too.
- **Set-up confirmed by e-mail:** `twoFactor.confirmByEmail: false` (off), or `{ linkMinutes, sameBrowser, adminConfirm,
logLinkWhenMailFails, notices, maxMailsPerHour }`; the building blocks above.
- **Setup required:** `twoFactor.requireSetup: false` (off), `{ windowHours }` (another window, `false` / `0` = none),
`{ exempt }` (a service account), `{ dashboardNotice: false }`, `{ statusField: false }`, `{ setupNotice: false }`,
`twoFactor.resetAccess` (who reopens); exported: `twoFactorSetupState`, `twoFactorSetupDeadline`,