2FA screens: the back link is checked on the server (twoFactor.safeBack)

@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]>
This commit is contained in:
Radosław Smoliński
2026-11-24 15:29:06 +01:00
co-authored by Claude Fable 5.2
parent 39309c5c1c
commit 93e75426ab
26 changed files with 678 additions and 11 deletions
+42 -2
View File
@@ -526,8 +526,9 @@ ipal-kit upgrade, run:
pnpm generate:importmap
```
`src/app/(payload)/admin/importMap.js` must list `@intecion/ipal-kit/admin#TotpRedirectProvider` and
`@clocklimited/payload-2fa/rsc#TOTPSetup` / `#TOTPVerify` / `#TOTPField`. Commit the file.
`src/app/(payload)/admin/importMap.js` must list `@intecion/ipal-kit/admin#TotpRedirectProvider`,
`@intecion/ipal-kit/admin#SafeTotpView` and `@clocklimited/payload-2fa/rsc#TOTPSetup` / `#TOTPVerify` /
`#TOTPField`. Commit the file.
### Texts of the 2FA screens (`twoFactor.texts`)
@@ -564,6 +565,43 @@ No project change is needed apart from `generate:importmap`.
The provider only steers the UI. Data access is still checked by the package's access wrapper.
### The `back` link of the 2FA screens (`twoFactor.safeBack`)
The setup and verify screens take a `back` query value (`/admin/verify-totp?back=%2Fadmin%2Fcollections%2Fpages`)
and the 2FA package sends the browser there once the code is accepted, without looking at it. A link with
`back=javascript:…` therefore ran script in the panel, as the user who had just passed the second factor, and a
link with an outside address sent the user off the site straight after a real login step. The original package
fixed this in `payload-totp` 3.0.5 (GHSA-rffw-fq79-g3ch); `@clocklimited/payload-2fa` (beta.1 to beta.7) has not.
The panel's CSP does not stop it: the admin needs inline script.
ipal-kit closes it without changing the package. `SafeTotpView` (`@intecion/ipal-kit/admin`) is registered in
place of the package's four views (`setup-totp`, `verify-totp`, each with and without a leading slash). It checks
`back` on the server and renders the package's view with the checked value. The package's views stay in the
import map through `admin.dependencies`, so ipal-kit still never imports the package.
The rule (`safeAdminBack`, the same as upstream's): `back` is resolved the way a browser resolves it and kept
only when it stays on the site's origin, over HTTP(S), without credentials, inside the admin route
(`routes.admin`, also a custom one). Everything else gives the admin root: `javascript:` in any case or with
leading spaces, `data:`, `https://other.example`, `//other.example`, `/\other.example`, `\\other.example`,
`https:other.example`, a path outside the panel, a repeated `back`, no `back` at all. The value passed on is
always a path, never an absolute URL. An absolute `back` on the site's own origin is accepted only when the
config has `serverURL`.
- **On by default** with `twoFactor`. After upgrading run `pnpm payload generate:importmap` and commit
`importMap.js`; without it the two screens fail with `[ipal] The admin import map has no "…"`.
- **Your own rule:** `twoFactor: { …, safeBack: (back, { adminRoute, origin }) => string }`. It gets the raw value
(`undefined` when absent or repeated) and returns where to go. Return a path on your own origin.
- **Off:** `twoFactor: { …, safeBack: false }` — the package's views exactly as it registers them. Only when a
later version of the 2FA package checks `back` itself, or something in front of the site does.
- **Building blocks:** `safeAdminBack(back, { adminRoute, origin })` and `wrapTotpViews(views, added, rule)` from
`@intecion/ipal-kit`; `SafeTotpView` from `@intecion/ipal-kit/admin` for your own 2FA view registration (give it
the serverProp `ipalTotpView: '<import-map path of the view to render>'` and list that path in
`admin.dependencies`).
**Check:** log in with the password only, open
`/admin/verify-totp?back=https%3A%2F%2Fexample.com%2F`, enter the code: you must land on `/admin`, not on
example.com. With `back=%2Fadmin%2Fcollections%2Fpages` you must land on that list.
### Turning it off (DISCOURAGED)
```ts
@@ -661,6 +699,8 @@ size while reading, content signature, container and macro checks, an optional `
## Turn off, override, extend
- **Verify limit:** `twoFactor.verifyLimit: { max, windowMinutes }`, or `false`.
- **`back` link check of the 2FA screens:** `twoFactor.safeBack: false`, or a function as your own rule;
`safeAdminBack`, `wrapTotpViews`, `SafeTotpView` are exported.
- **2FA check:** per entity `custom.ipal.secondFactor` / `custom.totp.disableAccessWrapper` (one operation or
all); `twoFactor.guard: false` turns ipal-kit's own second-factor check off everywhere (not recommended);
`requireSecondFactor` / `secondFactorPassed` are exported for your own access functions and endpoints.