2FA reset endpoint: only from the site's own origin (twoFactor.sameOrigin)

POST /api/ipal/two-factor/reset reads a text/plain body, which another
origin can send without a CORS preflight; a sibling subdomain is same-site,
so the browser sends the admin's SameSite=Lax cookie, and Payload's csrf
list is empty without serverURL. Such a page could reset any account's
TOTP and end its sessions (reproduced on SQLite, Postgres, MongoDB). Now the
endpoint takes the same origin rule as verify and setup, before it reads
or changes anything; twoFactor.sameOrigin: false leaves it out, and
buildTwoFactorResetEndpoint takes ownOrigin for own wiring.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 20:18:57 +01:00
co-authored by Claude Opus 5.5
parent 91770ec17f
commit 6ca0730c08
9 changed files with 122 additions and 5 deletions
@@ -240,6 +240,14 @@ export type TwoFactorResetOptions = {
* if you return true for it (`req.user._strategy` tells them apart).
*/
access?: (args: { req: PayloadRequest }) => boolean | Promise<boolean>
/**
* Whether the request may come from where the browser says it comes from (sameOrigin.ts). False =
* refused (403) before anything is read or changed. The plugin passes the `twoFactor.sameOrigin` rule
* (on by default; `sameOrigin: false` leaves it out). Without it the endpoint takes a cross-origin
* `text/plain` POST — no CORS preflight — so a page on a sibling subdomain, which the browser sends the
* admin's `SameSite=Lax` cookie to, could reset any account's TOTP and end its sessions.
*/
ownOrigin?: (req: PayloadRequest) => boolean
}
/**
@@ -267,6 +275,7 @@ export function buildTwoFactorResetEndpoint(collection: string, options: TwoFact
? (await options.access({ req })) === true
: (req.user as { _strategy?: unknown })._strategy === 'totp' && userMeetsRole(req, 'admin'))
if (!allowed) {return Response.json({ message: serverText(i18n, 'twoFactorResetForbidden'), ok: false }, { status: 403 })}
if (options.ownOrigin && !options.ownOrigin(req)) {return Response.json({ message: 'Not allowed from this origin.', ok: false }, { status: 403 })}
let body: unknown
try {
body = await req.json?.()
+1 -1
View File
@@ -394,7 +394,7 @@ export const iapl = (options: IaplOptions): Plugin => {
}
}
// An admin resets an account's TOTP (lost phone) / reopens its setup window.
config.endpoints = [...(config.endpoints ?? []), buildTwoFactorResetEndpoint(target, { access: tf.resetAccess })]
config.endpoints = [...(config.endpoints ?? []), buildTwoFactorResetEndpoint(target, { access: tf.resetAccess, ...(guards.ownOrigin ? { ownOrigin: guards.ownOrigin } : {}) })]
// Called by Payload on every request before any access function: guards whatever
// the live config has that was added after iapl (start, dev hot reload).
const sweep = buildGuardStrategy(adoptConfig)