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
+4 -4
View File
@@ -21,7 +21,7 @@ import {
twoFactorSetupState,
watchPayloadConfig,
} from '../src/modules/security/totp/secondFactor.js'
import { buildTwoFactorResetEndpoint, resolveRequireSetup, strictExempt } from '../src/modules/security/totp/setupRequired.js'
import { buildTwoFactorResetEndpoint, resolveRequireSetup, stampSetupFrom, strictExempt } from '../src/modules/security/totp/setupRequired.js'
import { buildVerifyLimit, clearVerifyLimits, verifyLimitKey } from '../src/modules/security/totp/verifyLimit.js'
import { iapl } from '../src/plugin.js'
import { withTotpParts } from './helpers.js'
@@ -480,7 +480,7 @@ describe('iapl and the 2FA plugin', () => {
...withTotpParts(c, o.collection),
endpoints: [...(c.endpoints ?? []), ...['/verify-totp', '/setup-totp', '/remove-totp'].map((path) => ({ handler, method: 'post' as const, path }))],
})
const build = (twoFactor: Record<string, unknown> = {}) => iapl({ i18n, twoFactor: { issuer: 'A', plugin: withEndpoints as never, ...twoFactor } as never })({ collections: [], endpoints: [], secret: 'x' } as unknown as Config)
const build = (twoFactor: Record<string, unknown> = {}) => iapl({ i18n, twoFactor: { confirmByEmail: false, issuer: 'A', plugin: withEndpoints as never, ...twoFactor } as never })({ collections: [], endpoints: [], secret: 'x' } as unknown as Config)
const call = async (config: Config, path: string, headers: Record<string, string>, user: Record<string, unknown>) =>
(await config.endpoints!.find((e) => e.path === path)!.handler({ headers: new Headers(headers), i18n: undefined, payload: { config, logger: { warn } }, url: 'https://site.example/api' + path, user: { id: 5, collection: 'users', ...user } } as never)).status
const elsewhere = { host: 'site.example', origin: 'https://blog.site.example' }
@@ -538,7 +538,7 @@ describe('iapl and the 2FA plugin', () => {
expect(guard(on)).toMatchObject({ collection: 'users', requireSetup: true, setupWindowMs: 72 * 3_600_000 })
const users = on.collections!.find((c) => c.slug === 'users')!
expect(users.fields.some((f) => 'name' in f && f.name === 'totpSetupFrom')).toBe(true)
expect(users.hooks?.beforeChange?.length).toBe(1)
expect(users.hooks?.beforeChange?.filter((h) => h === stampSetupFrom)).toHaveLength(1)
expect(on.endpoints!.some((e) => e.path === '/ipal/two-factor/reset')).toBe(true)
expect(JSON.stringify(on.admin?.components?.beforeDashboard)).toContain('TwoFactorAlert')
@@ -551,7 +551,7 @@ describe('iapl and the 2FA plugin', () => {
const offUsers = off.collections!.find((c) => c.slug === 'users')!
expect(offUsers.fields.some((f) => 'name' in f && f.name === 'totpSetupFrom')).toBe(true)
expect(offUsers.fields.some((f) => 'name' in f && f.name === 'totpSetupStatus')).toBe(false)
expect(offUsers.hooks?.beforeChange?.length ?? 0).toBe(0) // no stamping while the mode is off
expect(offUsers.hooks?.beforeChange?.includes(stampSetupFrom) ?? false).toBe(false) // no stamping while the mode is off
expect(JSON.stringify(off.admin?.components?.beforeDashboard ?? [])).not.toContain('TwoFactorAlert')
const guardOff = await iapl({ i18n, twoFactor: { guard: false, issuer: 'A', plugin: fakeTotp as never } })(base())
expect(guardOff.collections!.find((c) => c.slug === 'users')!.fields.some((f) => 'name' in f && f.name === 'totpSetupFrom')).toBe(true)