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
+23
View File
@@ -33,6 +33,7 @@ import { buildGlobalRevalidateHook } from './modules/hooks/buildRevalidateHook.j
import { withFillLocales } from './modules/hooks/fillEmptyLocales.js'
import { buildLocalizationConfig, validateI18nConfig } from './modules/i18n/index.js'
import { buildSystemPagesFields } from './modules/pages/index.js'
import { wrapTotpViews } from './modules/security/totp/safeBack.js'
import {
buildGuardStrategy,
ensurePayloadGuarded,
@@ -229,6 +230,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
)
}
const endpointsBefore = new Set(config.endpoints ?? [])
const viewsBefore = new Set(Object.keys(config.admin?.components?.views ?? {}))
const entityEndpointsBefore = new Map<string, Set<unknown>>(
[...(config.collections ?? []), ...(config.globals ?? [])].map((e) => [e.slug, new Set<unknown>(Array.isArray(e.endpoints) ? e.endpoints : [])]),
)
@@ -333,6 +335,27 @@ export const ipalKit = (options: IpalOptions): Plugin => {
},
}
}
// The package's setup and verify screens send the browser to the `back` query
// value after a valid code, unchecked: a crafted link runs script in the panel
// or leaves the site (safeBack.ts). ipal-kit's view goes in front of them and
// hands the package's view a checked value. `safeBack: false` leaves them as
// the package registered them; a function replaces the rule.
if (tf.safeBack !== false) {
const wrapped = wrapTotpViews(
(config.admin?.components?.views ?? {}) as Record<string, { Component?: unknown; path?: string }>,
(key) => !viewsBefore.has(key),
typeof tf.safeBack === 'function' ? tf.safeBack : undefined,
)
if (Object.keys(wrapped.dependencies).length) {
config.admin = {
...config.admin,
components: { ...config.admin?.components, views: wrapped.views as never },
// keeps the package's own views in the import map: the wrapper renders them from there
dependencies: { ...config.admin?.dependencies, ...wrapped.dependencies },
}
}
}
}
const agency = options.agency === undefined ? DEFAULT_AGENCY : options.agency