2FA back link: the stale import-map notice never replaces the screen
R2-1: the notice was rendered instead of the 2FA screen, and its flag is a text search of the importMap.js on disk — not the map in the build. A false alarm (map generated in the build with the repository's file in the running image, a re-exporting map) hid a working code form and locked everyone out. The notice is now rendered above the screen, the log line says "may be", and the flag goes to ipal-kit's own provider only. R2-2: views are compared against copies, so a factory that reuses its view objects is guarded too; copyViews, changedViewKeys, SAFE_TOTP_VIEW, totpViewImportKey, findImportMapFile and missingImportMapEntries are exported; ipal-doctor warns when the config registers the 2FA package's own view; docs wording and a misplaced comment fixed. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
ad51456eaa
commit
b2c1dca5da
+19
-9
@@ -34,7 +34,7 @@ import { withFillLocales } from './modules/hooks/fillEmptyLocales.js'
|
||||
import { buildLocalizationConfig, validateI18nConfig } from './modules/i18n/index.js'
|
||||
import { buildSystemPagesFields } from './modules/pages/index.js'
|
||||
import { missingImportMapEntries } from './modules/security/totp/importMapCheck.js'
|
||||
import { changedViewKeys, SAFE_TOTP_VIEW, wrapTotpViews } from './modules/security/totp/safeBack.js'
|
||||
import { changedViewKeys, copyViews, SAFE_TOTP_VIEW, wrapTotpViews } from './modules/security/totp/safeBack.js'
|
||||
import {
|
||||
buildGuardStrategy,
|
||||
ensurePayloadGuarded,
|
||||
@@ -56,6 +56,8 @@ import { buildStatsCollection, buildStatsEndpoint } from './modules/stats/index.
|
||||
|
||||
/** The admin provider @clocklimited/payload-2fa registers; ipal-kit swaps it (twoFactor.provider). */
|
||||
const TOTP_PACKAGE_PROVIDER = '@clocklimited/payload-2fa/rsc#TOTPProvider'
|
||||
/** ipal-kit's own redirect provider (twoFactor.provider default). */
|
||||
const TOTP_REDIRECT_PROVIDER = '@intecion/ipal-kit/admin#TotpRedirectProvider'
|
||||
|
||||
/**
|
||||
* IPAL (Intecion Payload Advanced Library) plugin for Payload CMS 3.
|
||||
@@ -233,7 +235,12 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
||||
)
|
||||
}
|
||||
const endpointsBefore = new Set(config.endpoints ?? [])
|
||||
const viewsBefore: Record<string, unknown> = { ...config.admin?.components?.views }
|
||||
// Copies, so that a view the 2FA factory hands back is told apart from one it left alone
|
||||
// even when the factory returns the very same objects on every call.
|
||||
const viewsBefore = copyViews(config.admin?.components?.views)
|
||||
if (config.admin?.components?.views) {
|
||||
config.admin = { ...config.admin, components: { ...config.admin.components, views: viewsBefore as never } }
|
||||
}
|
||||
const entityEndpointsBefore = new Map<string, Set<unknown>>(
|
||||
[...(config.collections ?? []), ...(config.globals ?? [])].map((e) => [e.slug, new Set<unknown>(Array.isArray(e.endpoints) ? e.endpoints : [])]),
|
||||
)
|
||||
@@ -325,7 +332,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
||||
if (tf.provider !== false) {
|
||||
const custom = tf.provider
|
||||
const ours =
|
||||
typeof custom === 'object' ? custom : { path: custom ?? '@intecion/ipal-kit/admin#TotpRedirectProvider' }
|
||||
typeof custom === 'object' ? custom : { path: custom ?? TOTP_REDIRECT_PROVIDER }
|
||||
const providers = config.admin?.components?.providers ?? []
|
||||
config.admin = {
|
||||
...config.admin,
|
||||
@@ -365,17 +372,19 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
||||
// 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.
|
||||
// The check reads the file on disk, which need not be the map in the bundle — so it
|
||||
// only ever ADDS a notice; the screen itself is rendered as usual.
|
||||
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}`
|
||||
staleImportMap = `${stale.missing.join(', ')} was not found 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 && typeof p === 'object' && p.path === TOTP_REDIRECT_PROVIDER // ipal-kit's own provider only
|
||||
? { ...p, clientProps: { ...p.clientProps, staleImportMap: true } }
|
||||
: p,
|
||||
),
|
||||
@@ -752,14 +761,15 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
||||
adoptConfig(payload)
|
||||
// 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").',
|
||||
`[ipal] The admin import map may be out of date: ${staleImportMap}. If the 2FA setup and verify screens are ` +
|
||||
'EMPTY (no code form), nobody can sign in to the panel: run `pnpm payload generate:importmap`, commit ' +
|
||||
'importMap.js, rebuild and redeploy. If the screens work, the file on disk is not the map the build used — ' +
|
||||
'set twoFactor.importMapCheck: false (docs/security.md "A stale import map").',
|
||||
)
|
||||
}
|
||||
// Staff whose collection is not the 2FA collection log in without a second factor: say so.
|
||||
if (twoFactorCollection) {
|
||||
const uncovered = staffCollectionSlugs(payload).filter((slug) => slug !== twoFactorCollection)
|
||||
if (uncovered.length) {
|
||||
|
||||
Reference in New Issue
Block a user