2FA setup required: review fixes (SR-1 to SR-9)

SR-1: /api/ipal/admin-icon sends the upload security headers (sandbox policy,
nosniff) with the bytes: an SVG is shown, never run. fetchPublicImage stops
reading at the size limit.
SR-2: requireSetup.exempt exempts only on exactly true; an async function, and a
windowHours that is not a number or false, stop the start.
SR-3: on Postgres / SQLite the missing column makes every login fail: the log
says so, the docs call the migration a required step of the same deploy, and
ipal-doctor reports a SQL project without it.
SR-4: an admin's reset also ends the account's sessions.
SR-5: the reset needs a session that entered its own TOTP code by default.
SR-6: the doctor's import-map rule covers TwoFactorStatusField and TwoFactorAlert.
SR-7: building blocks exported; dashboard notice, status field, setup notice and
the server-side image fetch can be switched off one by one.
SR-8: totpSetupFrom is on the 2FA collection whenever 2FA is on.
SR-9: both ways out of "every administrator is blocked" in security.md.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-24 19:32:34 +01:00
co-authored by Claude Fable 5.2
parent 9d2ab43438
commit 75a6f5209e
34 changed files with 780 additions and 103 deletions
+33
View File
@@ -529,6 +529,39 @@ describe('2FA setup required: an account without TOTP reaches nothing but setup'
expect(ok.status).toBe(200)
const doc = (await payload.findByID({ id: userId, collection: 'users' as never })) as unknown as { totpSetupFrom: string }
expect(Date.now() - new Date(doc.totpSetupFrom).getTime()).toBeLessThan(120_000)
// the reset ends the account's sessions: the token from before is nobody's now, the account logs in again
expect(((await (await api('/users/me', sessions.newcomer)).json()) as { user: unknown }).user).toBeNull()
expect(((await (await setup(sessions.newcomer)).json().catch(() => ({}))) as { ok?: boolean }).ok).not.toBe(true)
sessions.newcomer = { headers: { authorization: `JWT ${(await login('users', '[email protected]')).token}` } }
})
// A reset is also what an administrator presses for an account that may be in someone else's hands. A session of
// that account which had passed 2FA must not survive it: /api/setup-totp asks for no password, so it could
// otherwise enrol the thief's own authenticator.
test('a reset ends every session of the account: a stolen verified session cannot enrol its own authenticator', async () => {
const THIEF_SECRET = 'KRSXG5CTMVRXEZLU'
const victim = await payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['editor'], totpSecret: TOTP_SECRET } as never })
// the victim's fully verified session, as a thief would hold it (token + 2FA cookie)
const signIn = await login('users', '[email protected]')
const verify = await api('/verify-totp', { headers: { authorization: `JWT ${signIn.token}` } }, { body: JSON.stringify({ token: totp(TOTP_SECRET) }), method: 'POST' })
expect(((await verify.json()) as { ok: boolean }).ok).toBe(true)
const stolen: Session = { headers: { authorization: `JWT ${signIn.token}`, cookie: cookiesOf(verify).join('; ') } }
expect((await api('/pages', stolen)).status).toBe(200)
expect((await api('/ipal/two-factor/reset', sessions.verified, { body: JSON.stringify({ userId: victim.id }), method: 'POST' })).status).toBe(200)
expect(((await (await api('/users/me', stolen)).json()) as { user: unknown }).user).toBeNull()
const enrol = await api('/setup-totp', stolen, { body: JSON.stringify({ secret: THIEF_SECRET, token: totp(THIEF_SECRET) }), method: 'POST' })
expect(((await enrol.json().catch(() => ({}))) as { ok?: boolean }).ok).not.toBe(true)
const after = (await payload.findByID({ id: victim.id, collection: 'users' as never, showHiddenFields: true })) as unknown as { sessions?: unknown[]; totpSecret?: null | string }
expect(after.totpSecret ?? null).toBeNull()
expect(after.sessions ?? []).toEqual([])
expect((await api('/form-submissions', stolen)).status).toBe(403)
// the owner logs in again with the password and sets TOTP up; the old session stays dead
const owner: Session = { headers: { authorization: `JWT ${(await login('users', '[email protected]')).token}` } }
expect((await setup(owner)).status).toBe(200)
expect(((await (await api('/users/me', stolen)).json()) as { user: unknown }).user).toBeNull()
})
test('inside the window it sets TOTP up, and then everything its role allows opens', async () => {