@clocklimited/payload-2fa sends the browser to the `back` query value after a valid code without checking it (GHSA-rffw-fq79-g3ch upstream, not fixed in the fork): a prepared link ran script in the panel or left the site. SafeTotpView now stands in front of the package's setup and verify views and passes on a checked value: a path on the site's origin inside the admin route, else the admin root. The package's views stay in the import map through admin.dependencies, so ipal-kit still never imports the package. twoFactor.safeBack: false turns it off, a function replaces the rule; safeAdminBack, wrapTotpViews and SafeTotpView are exported. Projects with 2FA must regenerate the admin import map. Co-Authored-By: Claude Fable 5.2 <[email protected]>
93 lines
4.7 KiB
TypeScript
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; ipal-kit 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()
|
|
}
|
|
})
|