2FA back link: review fixes — views guarded by object, client views, a loud stale import map

SB-1: the 2FA package replaces a view that already stands under its keys, so a
key that existed before the factory ran was wrongly treated as the project's own
and left unguarded (also with a double registration, or ipalKit over its own
output). Views are now compared by object, right after the factory.

SB-2: SafeTotpView renders through Payload's RenderServerComponent: a client
component set as a 2FA view gets only client props plus the checked back.

SB-3: a production build with the previous import map shows an empty 2FA screen.
twoFactor.importMapCheck (on, production only) reads the committed map at start:
an error in the log and a message on the 2FA screens. Docs name the real symptom.

SB-4: enabled: false restores admin.dependencies; wrapTotpViews keeps exportName.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-24 16:16:39 +01:00
co-authored by Claude Fable 5.2
parent 93e75426ab
commit ad51456eaa
21 changed files with 477 additions and 70 deletions
+40 -4
View File
@@ -33,7 +33,8 @@ 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 { missingImportMapEntries } from './modules/security/totp/importMapCheck.js'
import { changedViewKeys, SAFE_TOTP_VIEW, wrapTotpViews } from './modules/security/totp/safeBack.js'
import {
buildGuardStrategy,
ensurePayloadGuarded,
@@ -92,7 +93,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
if (options.enabled === false) {
return {
...config,
admin: { ...config.admin, components: incomingConfig.admin?.components },
admin: { ...config.admin, components: incomingConfig.admin?.components, dependencies: incomingConfig.admin?.dependencies },
endpoints: incomingConfig.endpoints,
onInit: incomingConfig.onInit,
}
@@ -171,6 +172,8 @@ export const ipalKit = (options: IpalOptions): Plugin => {
let secondFactorGuard = false
/** The 2FA collection, when 2FA is on. */
let twoFactorCollection: null | string = null
/** Set when the committed admin import map lacks the 2FA view wrapper (production). */
let staleImportMap: null | string = null
/** This config's entry in the staff registry until a Payload instance takes it over. */
const staffToken = {}
/**
@@ -230,7 +233,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
)
}
const endpointsBefore = new Set(config.endpoints ?? [])
const viewsBefore = new Set(Object.keys(config.admin?.components?.views ?? {}))
const viewsBefore: Record<string, unknown> = { ...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 : [])]),
)
@@ -253,6 +256,11 @@ export const ipalKit = (options: IpalOptions): Plugin => {
config.collections = guardNewEntityEndpoints(config.collections, entityEndpointsBefore, target, guards as never)
config.globals = guardNewEntityEndpoints(config.globals, entityEndpointsBefore, target, guards as never)
twoFactorCollection = target
// The views the 2FA package registered, by object: it REPLACES a view the project (or an
// earlier run of the package, or of ipalKit) already had under its keys, so a key that
// existed before is no sign that the view is the project's own. Taken here, before the
// texts step below re-creates those objects.
const viewsFromPackage = changedViewKeys(viewsBefore, { ...config.admin?.components?.views })
// The package wraps only what is in the config right now. ipal-kit's own
// globals and collections come later, so they get ipal-kit's guard at the
@@ -344,7 +352,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
if (tf.safeBack !== false) {
const wrapped = wrapTotpViews(
(config.admin?.components?.views ?? {}) as Record<string, { Component?: unknown; path?: string }>,
(key) => !viewsBefore.has(key),
(key) => viewsFromPackage.has(key),
typeof tf.safeBack === 'function' ? tf.safeBack : undefined,
)
if (Object.keys(wrapped.dependencies).length) {
@@ -354,6 +362,27 @@ export const ipalKit = (options: IpalOptions): Plugin => {
// keeps the package's own views in the import map: the wrapper renders them from there
dependencies: { ...config.admin?.dependencies, ...wrapped.dependencies },
}
// A production build with the import map of the previous version shows an EMPTY 2FA
// screen (Payload renders nothing for a path the map lacks): say so, in the log at
// start and on the screen itself (importMapCheck.ts). Development regenerates the map.
if (tf.importMapCheck !== false && process.env.NODE_ENV === 'production') {
const stale = await missingImportMapEntries(config, [SAFE_TOTP_VIEW])
if (stale.missing.length) {
staleImportMap = `${stale.missing.join(', ')} is not in ${stale.file}`
const providers = config.admin?.components?.providers ?? []
config.admin = {
...config.admin,
components: {
...config.admin?.components,
providers: providers.map((p) =>
p && typeof p === 'object' && (p.clientProps as { forceSetup?: boolean } | undefined)?.forceSetup !== undefined
? { ...p, clientProps: { ...p.clientProps, staleImportMap: true } }
: p,
),
},
}
}
}
}
}
}
@@ -724,6 +753,13 @@ export const ipalKit = (options: IpalOptions): Plugin => {
// Payload's dev reload swaps `payload.config` and runs no onInit: adopt the new config at once.
watchPayloadConfig(payload, adoptConfig)
// Staff whose collection is not the 2FA collection log in without a second factor: say so.
if (staleImportMap) {
payload.logger.error(
`[ipal] The admin import map is out of date: ${staleImportMap}. The 2FA setup and verify screens are EMPTY ` +
'until it is regenerated, so nobody can sign in to the panel. Run `pnpm payload generate:importmap`, commit ' +
'importMap.js, rebuild and redeploy (docs/security.md "The `back` link of the 2FA screens").',
)
}
if (twoFactorCollection) {
const uncovered = staffCollectionSlugs(payload).filter((slug) => slug !== twoFactorCollection)
if (uncovered.length) {