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]>
63 lines
3.6 KiB
TypeScript
63 lines
3.6 KiB
TypeScript
/**
|
|
* twoFactor.confirmByEmail building blocks: the switch is read strictly, the code check agrees with the 2FA package's
|
|
* own (RFC 6238 vectors; one period either side, like otpauth's window 1), addresses are masked, and the notice hooks see every transition. The HTTP flow is in
|
|
* enrolmentConfirmHttp.test.ts.
|
|
*/
|
|
import { describe, expect, test } from 'vitest'
|
|
|
|
import { buildTotpNoticeHooks, maskAddress, resolveConfirmByEmail, verifyTotpCode } from '../src/modules/security/totp/enrolmentConfirm.js'
|
|
|
|
describe('confirmByEmail options', () => {
|
|
test('on by default; false turns it off; nonsense stops the start', () => {
|
|
expect(resolveConfirmByEmail(undefined)).toMatchObject({ adminConfirm: true, linkMinutes: 30, logLinkWhenMailFails: true, maxMailsPerHour: 5, notices: true, sameBrowser: true })
|
|
expect(resolveConfirmByEmail(false)).toBeNull()
|
|
expect(resolveConfirmByEmail({ linkMinutes: 10, sameBrowser: false })).toMatchObject({ linkMinutes: 10, sameBrowser: false })
|
|
for (const bad of ['false', 0, [], { sameBrowser: 'false' }, { linkMinutes: 0 }, { linkMinutes: 2000 }, { maxMailsPerHour: 1.5 }]) {
|
|
expect(() => resolveConfirmByEmail(bad), JSON.stringify(bad)).toThrow(/confirmByEmail/)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('the code check', () => {
|
|
const secret = 'JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP'
|
|
test('RFC 6238 test vectors (SHA1, SHA256, SHA512; 8 digits) and one period either side, not two', () => {
|
|
// RFC 6238 Appendix B: seeds "1234567890…" (20 / 32 / 64 bytes) in base32
|
|
const sha1 = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'
|
|
const sha256 = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA'
|
|
const sha512 = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA'
|
|
const at = (s: number) => s * 1000
|
|
expect(verifyTotpCode(sha1, '94287082', { digits: 8 }, at(59))).toBe(true)
|
|
expect(verifyTotpCode(sha256, '46119246', { algorithm: 'SHA256', digits: 8 }, at(59))).toBe(true)
|
|
expect(verifyTotpCode(sha512, '90693936', { algorithm: 'SHA512', digits: 8 }, at(59))).toBe(true)
|
|
expect(verifyTotpCode(sha1, '07081804', { digits: 8 }, at(1111111109))).toBe(true)
|
|
// the same code 30 s later still passes (window 1), 90 s later not
|
|
expect(verifyTotpCode(sha1, '07081804', { digits: 8 }, at(1111111109 + 30))).toBe(true)
|
|
expect(verifyTotpCode(sha1, '07081804', { digits: 8 }, at(1111111109 + 90))).toBe(false)
|
|
expect(verifyTotpCode(sha1, '1234567', { digits: 8 }, at(59))).toBe(false)
|
|
expect(verifyTotpCode('not base32!', '123456', {}, at(59))).toBe(false)
|
|
expect(verifyTotpCode('ABCD', '123456', {}, at(59))).toBe(false) // too short a key
|
|
void secret
|
|
})
|
|
test('addresses are masked on the screen', () => {
|
|
expect(maskAddress('[email protected]')).toBe('j…@firma.pl')
|
|
expect(maskAddress('nope')).toBe('…')
|
|
})
|
|
})
|
|
|
|
describe('notice hooks', () => {
|
|
const { beforeChange } = buildTotpNoticeHooks('users', { notices: true })
|
|
const change = (before: unknown, after: unknown, operation = 'update') => {
|
|
const context: Record<string, unknown> = {}
|
|
beforeChange({ context, data: { totpSecret: after }, operation, originalDoc: { totpSecret: before } } as never)
|
|
return context.iaplTotpChange ?? null
|
|
}
|
|
test('on, off, changed; nothing for a write that does not touch the secret, or a create', () => {
|
|
expect(change(null, 'A')).toBe('on')
|
|
expect(change('A', null)).toBe('off')
|
|
expect(change('A', 'B')).toBe('changed')
|
|
expect(change('A', 'A')).toBeNull()
|
|
expect(change(null, null)).toBeNull()
|
|
expect(change(null, 'A', 'create')).toBeNull()
|
|
})
|
|
})
|