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]>
94 lines
4.7 KiB
TypeScript
94 lines
4.7 KiB
TypeScript
/**
|
|
* The same guarantees with the oldest supported 2FA package, @clocklimited/payload-2fa 3.0.0-beta.1 (installed
|
|
* here under the alias `payload-2fa-beta1`). The other HTTP tests run on the version in devDependencies, which is
|
|
* the one client sites use.
|
|
*/
|
|
import type { Payload } from 'payload'
|
|
|
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
import os from 'os'
|
|
import path from 'path'
|
|
import { getPayload } from 'payload'
|
|
import { payloadTotp } from 'payload-2fa-beta1'
|
|
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'
|
|
const PASSWORD = 'a-long-test-password-1'
|
|
let server: TestServer
|
|
let payload: Payload
|
|
|
|
const post = (pathname: string, body: unknown, headers: Record<string, string> = {}) =>
|
|
fetch(`${server.url}/api${pathname}`, { body: JSON.stringify(body), headers: { 'content-type': 'application/json', ...headers }, method: 'POST' })
|
|
const get = (pathname: string, headers: Record<string, string>) => fetch(`${server.url}/api${pathname}`, { headers })
|
|
|
|
beforeAll(async () => {
|
|
const config = buildTestConfig({
|
|
admin: { user: 'users' },
|
|
collections: [
|
|
{ slug: 'customers', access: { create: () => true }, auth: true, fields: [] },
|
|
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
|
|
],
|
|
db: testDatabase('sechttp_b1'),
|
|
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 as never },
|
|
}),
|
|
],
|
|
secret: 'integration-test-secret-0123456789',
|
|
telemetry: false,
|
|
})
|
|
payload = await getPayload({ config })
|
|
server = await startPayloadServer(config)
|
|
await payload.updateGlobal({ slug: 'site-integrations' as never, data: { smtpPassword: 'S3CRET' } as never })
|
|
await payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['admin'], totpSecret: SECRET } as never })
|
|
await payload.create({ collection: 'customers' as never, data: { email: '[email protected]', password: PASSWORD } as never })
|
|
}, 120_000)
|
|
|
|
afterAll(async () => {
|
|
await server?.close()
|
|
await payload?.destroy?.()
|
|
})
|
|
|
|
test('beta.1: password only → refused; password and code → allowed (REST and GraphQL)', async () => {
|
|
const login = await post('/users/login', { email: '[email protected]', password: PASSWORD })
|
|
const cookie = cookiesOf(login).join('; ')
|
|
for (const p of ['/globals/site-integrations', '/form-submissions', '/users', '/globals/site-settings']) {
|
|
expect([p, (await get(p, { cookie })).status]).toEqual([p, 403])
|
|
}
|
|
const before = (await (await post('/graphql', { query: '{ SiteIntegration { smtpPassword } }' }, { cookie })).json()) as { errors?: unknown[] }
|
|
expect(before.errors?.length).toBeGreaterThan(0)
|
|
|
|
const verify = await post('/verify-totp', { token: totp(SECRET) }, { cookie })
|
|
expect(((await verify.json()) as { ok: boolean }).ok).toBe(true)
|
|
const full = [cookie, ...cookiesOf(verify)].join('; ')
|
|
const integrations = await get('/globals/site-integrations', { cookie: full })
|
|
expect(integrations.status).toBe(200)
|
|
expect(((await integrations.json()) as { smtpPassword: string }).smtpPassword).toBe('********') // set, never returned (write-only secrets)
|
|
expect((await get('/form-submissions', { cookie: full })).status).toBe(200)
|
|
})
|
|
|
|
test('beta.1: a customers account cannot use the 2FA endpoints or reach staff data', async () => {
|
|
const login = (await (await post('/customers/login', { email: '[email protected]', password: PASSWORD })).json()) as { token: string }
|
|
const authorization = `JWT ${login.token}`
|
|
const ATTACKER = 'KRSXG5CTMVRXEZLU'
|
|
expect((await post('/setup-totp', { secret: ATTACKER, token: totp(ATTACKER) }, { authorization })).status).toBe(403)
|
|
const admin = await payload.find({ collection: 'users' as never, showHiddenFields: true })
|
|
expect((admin.docs[0] as { totpSecret?: string }).totpSecret).toBe(SECRET)
|
|
expect((await get('/globals/site-integrations', { authorization })).status).toBe(403)
|
|
expect((await get('/form-submissions', { authorization })).status).toBe(403)
|
|
})
|