Files
ipal-kit/reference/e2e/two-factor-back.spec.ts
Radosław SmolińskiandClaude Fable 5.2 b91984fbcd IAPL rename: docs, agent guide, skills, reference site and starter on the new names; changelog draft
- docs, AGENT.md, the 16 skills, PROGRAMISTA and README use the preferred names (scripted, same list of forms);
  past CHANGELOG entries and the proposals in docs/dev are not rewritten
- AGENT.md: 'Names: IAPL and ipal-kit' — old name, new name, state in 1.x; what is not renamed; storageNames
- iapl-upgrade '→ 1.17.0', PROGRAMISTA §6 item 72, CHANGELOG 1.17.0 (draft): nothing required
- env.md, doctor.md (legacy-names, storage-names), stats.md, security.md, project-starter.md, iapl-reference, iapl-setup
- skill scripts read ipal-audit-skip and IPAL_IP_HEADER too; audit rule ids stay what they were
- reference site: iapl(), iapl-doctor.json, iapl-* skills, storageNames: 'iapl'; INDEX.md regenerated

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:36:09 +01:00

93 lines
4.7 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()
await enterCode(third, totp(newSecret!))
await expectAdminRoot(third, baseURL!)
await third.context().close()
// setup screen, an ordinary link back into the panel is still followed
const fourth = await passwordOnly(browser, baseURL!, fresh[1])
await fourth.goto(`/admin/setup-totp?back=${encodeURIComponent('/admin/collections/pages?limit=5')}`)
const fourthSecret = await fourth.locator('input[type="hidden"]').evaluateAll((inputs) => inputs.map((i) => (i as HTMLInputElement).value).find((v) => /^[A-Z2-7]{16,}$/.test(v)))
await enterCode(fourth, totp(fourthSecret!))
await expect(fourth).toHaveURL(/\/admin\/collections\/pages\?limit=5$/, { timeout: 20_000 })
await fourth.context().close()
} finally {
for (const id of ids) {await first.request.delete(`/api/users/${id}`, { headers: { origin: baseURL! } })}
await first.context().close()
}
})