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]>
91 lines
4.6 KiB
TypeScript
91 lines
4.6 KiB
TypeScript
import { type Browser, expect, type Page, test } from '@playwright/test'
|
|
import { randomUUID } from 'node:crypto'
|
|
|
|
import { totp } from './totp'
|
|
|
|
/**
|
|
* The `back` link of the 2FA screens (twoFactor.safeBack, security.md). The 2FA package sends the browser to
|
|
* `back` after a valid code; IAPL checks the value first. A prepared link must end on the admin root:
|
|
* no script in the panel, no other site. Run against a built site with the test admin (create-test-admin.ts).
|
|
*
|
|
* Two verify attempts for the test admin here; the smoke spec makes two more (the limit is 5 in 10 minutes).
|
|
*/
|
|
const { E2E_ADMIN_EMAIL: email, E2E_ADMIN_PASSWORD: password, E2E_TOTP_SECRET: secret } = process.env
|
|
|
|
/** Script that would mark the page: runs only if the browser follows `back` as given. */
|
|
const SCRIPT = "javascript:void(document.title='XSS-'+document.domain)"
|
|
|
|
async function passwordOnly(browser: Browser, baseURL: string, who: { email: string; password: string }): Promise<Page> {
|
|
const context = await browser.newContext({ baseURL })
|
|
const page = await context.newPage()
|
|
await page.goto('/admin/login')
|
|
await page.fill('#field-email', who.email)
|
|
await page.fill('#field-password', who.password)
|
|
await page.click('.form-submit button')
|
|
await page.waitForURL(/(verify|setup)-totp/, { waitUntil: 'commit' })
|
|
return page
|
|
}
|
|
|
|
async function enterCode(page: Page, code: string) {
|
|
// six one-digit inputs: type the code into the first, focus moves on by itself
|
|
await page.getByRole('textbox').first().click()
|
|
await page.keyboard.type(code)
|
|
}
|
|
|
|
/** The page ended on the admin root of this site, and the marker script did not run. */
|
|
async function expectAdminRoot(page: Page, baseURL: string) {
|
|
await expect(page).toHaveURL(new RegExp(`^${baseURL.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/admin/?$`), { timeout: 20_000 })
|
|
await expect(page).toHaveTitle(/Dashboard|Pulpit/, { timeout: 20_000 })
|
|
expect(await page.title()).not.toContain('XSS-')
|
|
}
|
|
|
|
test('2FA screens: a prepared back link ends on the admin root', async ({ baseURL, browser }) => {
|
|
test.skip(!email || !password || !secret, 'E2E_* test admin not configured')
|
|
test.setTimeout(120_000)
|
|
const admin = { email: email!, password: password! }
|
|
const other = new URL(baseURL!)
|
|
other.hostname = other.hostname === 'localhost' ? '127.0.0.1' : 'localhost' // another origin, still this machine
|
|
|
|
// verify screen, script in `back`
|
|
const first = await passwordOnly(browser, baseURL!, admin)
|
|
await first.goto(`/admin/verify-totp?back=${encodeURIComponent(SCRIPT)}`)
|
|
await enterCode(first, totp(secret!))
|
|
await expectAdminRoot(first, baseURL!)
|
|
|
|
// two accounts that have not set TOTP up yet, made by the admin who is now fully signed in
|
|
const fresh = [0, 1].map(() => ({ email: `e2e-back-${randomUUID()}@example.com`, password: `T-${randomUUID()}` }))
|
|
const ids: string[] = []
|
|
for (const account of fresh) {
|
|
const created = await first.request.post('/api/users', { data: { ...account, roles: ['editor'] }, headers: { origin: baseURL! } })
|
|
expect(created.status()).toBe(201)
|
|
ids.push((await created.json()).doc.id)
|
|
}
|
|
|
|
try {
|
|
// verify screen, another host written with a backslash
|
|
const second = await passwordOnly(browser, baseURL!, admin)
|
|
await second.goto(`/admin/verify-totp?back=${encodeURIComponent(`/\\${other.host}/admin/login?landed=elsewhere`)}`)
|
|
await enterCode(second, totp(secret!))
|
|
await expectAdminRoot(second, baseURL!)
|
|
await second.context().close()
|
|
|
|
// setup screen, script in `back`
|
|
const third = await passwordOnly(browser, baseURL!, fresh[0])
|
|
await third.goto(`/admin/setup-totp?back=${encodeURIComponent(SCRIPT)}`)
|
|
const newSecret = await third.locator('input[type="hidden"]').evaluateAll((inputs) => inputs.map((i) => (i as HTMLInputElement).value).find((v) => /^[A-Z2-7]{16,}$/.test(v)))
|
|
expect(newSecret).toBeTruthy()
|
|
// twoFactor.confirmByEmail (1.18.0): a FIRST set-up waits for its e-mail link, so the setup screen does not
|
|
// navigate at all — the script in `back` cannot run, the screen stays. (`back` after setup matters only with
|
|
// confirmByEmail: false; test/safeBack.test.ts covers the check itself.)
|
|
await enterCode(third, totp(newSecret!))
|
|
await expect(third.locator('[data-sonner-toast]').first()).toBeVisible({ timeout: 15_000 })
|
|
await expect(third).toHaveURL(/\/admin\/setup-totp/)
|
|
expect(await third.title()).not.toContain('XSS-')
|
|
await third.context().close()
|
|
void fresh[1]
|
|
} finally {
|
|
for (const id of ids) {await first.request.delete(`/api/users/${id}`, { headers: { origin: baseURL! } })}
|
|
await first.context().close()
|
|
}
|
|
})
|