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]>
93 lines
4.0 KiB
TypeScript
93 lines
4.0 KiB
TypeScript
/**
|
|
* A fresh site with "2FA setup required": the first admin is created on Payload's "Create first user" screen
|
|
* (POST /api/users/first-register), reaches nothing until it has set TOTP up, and everything afterwards.
|
|
* Real HTTP, see securityHttp.test.ts.
|
|
*/
|
|
import type { Payload } from 'payload'
|
|
|
|
import { payloadTotp } from '@clocklimited/payload-2fa'
|
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
import os from 'os'
|
|
import path from 'path'
|
|
import { getPayload } from 'payload'
|
|
import { afterAll, beforeAll, expect, test, vi } from 'vitest'
|
|
|
|
import { iapl } from '../src/plugin.js'
|
|
import { buildTestConfig } from './helpers.js'
|
|
import { testDatabase } from './http/db.js'
|
|
import { cookiesOf, startPayloadServer, type TestServer } from './http/server.js'
|
|
import { totp } from './http/totp.js'
|
|
|
|
vi.mock('server-only', () => ({}))
|
|
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
|
|
|
|
const SECRET = 'JBSWY3DPEHPK3PXP'
|
|
let server: TestServer
|
|
let payload: Payload
|
|
|
|
beforeAll(async () => {
|
|
const config = buildTestConfig({
|
|
collections: [{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } }],
|
|
db: testDatabase('sechttp_first'),
|
|
editor: lexicalEditor(),
|
|
plugins: [
|
|
iapl({
|
|
access: { authCollection: 'users' },
|
|
forms: {},
|
|
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
|
|
// the e-mail step of a first enrolment has its own suite (enrolmentConfirmHttp.test.ts); here the code alone turns it on
|
|
twoFactor: { confirmByEmail: false, issuer: 'Example', plugin: payloadTotp },
|
|
}),
|
|
],
|
|
secret: 'integration-test-secret-0123456789',
|
|
telemetry: false,
|
|
})
|
|
payload = await getPayload({ config })
|
|
server = await startPayloadServer(config)
|
|
}, 120_000)
|
|
|
|
afterAll(async () => {
|
|
await server?.close()
|
|
await payload?.destroy?.()
|
|
})
|
|
|
|
const json = { 'content-type': 'application/json' }
|
|
|
|
test('the first admin: created, locked until TOTP is set up, then in', async () => {
|
|
const register = await fetch(`${server.url}/api/users/first-register`, {
|
|
body: JSON.stringify({ email: '[email protected]', password: 'a-long-test-password-1' }),
|
|
headers: json,
|
|
method: 'POST',
|
|
})
|
|
expect(register.status).toBe(200)
|
|
const body = (await register.json()) as { token?: string; user: { roles: string[] } }
|
|
expect(body.user.roles).toEqual(['admin'])
|
|
const cookie = cookiesOf(register).join('; ')
|
|
expect(cookie).toContain('payload-token')
|
|
|
|
for (const p of ['/globals/site-integrations', '/users', '/form-submissions', '/globals/site-settings']) {
|
|
expect([p, (await fetch(`${server.url}/api${p}`, { headers: { cookie } })).status]).toEqual([p, 403])
|
|
}
|
|
|
|
const setup = await fetch(`${server.url}/api/setup-totp`, { body: JSON.stringify({ secret: SECRET, token: totp(SECRET) }), headers: { ...json, cookie }, method: 'POST' })
|
|
expect(((await setup.json()) as { ok: boolean }).ok).toBe(true)
|
|
const full = [cookie, ...cookiesOf(setup)].join('; ')
|
|
for (const p of ['/globals/site-integrations', '/users', '/form-submissions']) {
|
|
expect([p, (await fetch(`${server.url}/api${p}`, { headers: { cookie: full } })).status]).toEqual([p, 200])
|
|
}
|
|
// the admin creates an editor: it gets its own setup window
|
|
const created = await fetch(`${server.url}/api/users`, {
|
|
body: JSON.stringify({ email: '[email protected]', password: 'a-long-test-password-2', roles: ['editor'] }),
|
|
headers: { ...json, cookie: full },
|
|
method: 'POST',
|
|
})
|
|
expect(created.status).toBe(201)
|
|
const editor = (await payload.find({ collection: 'users' as never, where: { email: { equals: '[email protected]' } } })).docs[0] as unknown as { totpSetupFrom?: string }
|
|
expect(editor.totpSetupFrom).toBeTruthy()
|
|
})
|
|
|
|
test('seeds and scripts are not affected: the Local API still reads and writes', async () => {
|
|
await payload.updateGlobal({ slug: 'site-settings' as never, data: { siteName: 'Seeded' } as never })
|
|
expect(((await payload.findGlobal({ slug: 'site-settings' as never })) as { siteName: string }).siteName).toBe('Seeded')
|
|
})
|