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:
Radosław Smoliński
2026-11-24 16:35:16 +01:00
co-authored by Claude Fable 5.2
parent ad51456eaa
commit b2c1dca5da
23 changed files with 315 additions and 66 deletions
+61
View File
@@ -15,6 +15,7 @@ import { findImportMapFile, missingImportMapEntries } from '../src/modules/secur
import { changedViewKeys, safeAdminBack, wrapTotpViews } from '../src/modules/security/totp/safeBack.js'
import { SafeTotpView } from '../src/modules/security/totp/SafeTotpView.js'
import { ipalKit } from '../src/plugin.js'
import { withTotpParts } from './helpers.js'
const i18n: I18nConfig = { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }
const admin = { adminRoute: '/admin' }
@@ -305,6 +306,26 @@ describe('SafeTotpView', () => {
})
})
describe('a 2FA factory that hands back the same view objects on every call', () => {
const views = {
SetupTOTP: { Component: { path: 'pkg/rsc#TOTPSetup' }, path: 'setup-totp' },
VerifyTOTP: { Component: { path: 'pkg/rsc#TOTPVerify' }, path: 'verify-totp' },
}
const factory = (o: { collection: string }) => (c: Config) =>
withTotpParts({ ...c, admin: { ...c.admin, components: { ...c.admin?.components, views: { ...c.admin?.components?.views, ...views } } } } as Config, o.collection)
const build = (incoming: Partial<Config> = {}) =>
ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: { issuer: 'E', plugin: factory as never } })({ collections: [], secret: 'x', ...incoming } as unknown as Config)
const paths = (config: Config) => Object.values(config.admin?.components?.views ?? {}).map((v) => (v as { Component: { path: string } }).Component.path)
test('is guarded when registered once, twice, and over a config that already holds those objects', async () => {
expect(paths(await build())).toEqual(Array(2).fill('@intecion/ipal-kit/admin#SafeTotpView'))
const already = factory({ collection: 'users' })({ collections: [{ slug: 'users', auth: true, fields: [] }], secret: 'x' } as unknown as Config)
expect(paths(already)).toEqual(['pkg/rsc#TOTPSetup', 'pkg/rsc#TOTPVerify']) // raw, the very objects the factory returns again
expect(paths(await build(already))).toEqual(Array(2).fill('@intecion/ipal-kit/admin#SafeTotpView'))
expect(views.VerifyTOTP.Component.path).toBe('pkg/rsc#TOTPVerify') // the factory's own objects are not changed
})
})
describe('safeAdminBack with values that are not a string', () => {
test.each([[['/admin', 'javascript:1']], [{}], [null], [1], [{ toString: () => 'javascript:1' }]])('%s', (value) => {
expect(safeAdminBack(value as never, { adminRoute: '/admin' })).toBe('/admin')
@@ -419,6 +440,46 @@ describe('the stale import-map check', () => {
}
})
// The search reads text on disk: these maps work in the build and are still reported. That is why the result
// only ever adds a notice (test/totpRedirectProvider.test.ts) and the log line says "may be".
test('a map that re-exports, spreads or computes its keys is reported although it may be fine', async () => {
const maps = [
"export { importMap } from './generatedMap.js'",
"import { generated } from './generatedMap.js'\nexport const importMap = { ...generated }",
'export const importMap = { [`@intecion/ipal-kit/admin#${name}`]: X }',
'',
]
for (const content of maps) {
const root = await project('src/app/(payload)/admin', content)
try {
expect([content, (await missingImportMapEntries({}, [ENTRY], root)).missing]).toEqual([content, [ENTRY]])
} finally {
await rm(root, { force: true, recursive: true })
}
}
})
test('only ipal-kit\'s own redirect provider gets the flag, not a project\'s provider', async () => {
const stale = await project('src/app/(payload)/admin', 'export const importMap = {}')
const providers = async (provider?: unknown) => {
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: { issuer: 'E', plugin: payloadTotpBeta7, provider } as never })({
admin: { importMap: { baseDir: stale } },
collections: [],
secret: 'x',
} as unknown as Config)
return JSON.stringify(config.admin?.components?.providers ?? [])
}
try {
vi.stubEnv('NODE_ENV', 'production')
expect(await providers()).toContain('"staleImportMap":true')
expect(await providers({ clientProps: { forceSetup: true }, path: '@/components/MyTotp#MyTotp' })).not.toContain('staleImportMap')
expect(await providers('@/components/MyTotp#MyTotp')).not.toContain('staleImportMap')
} finally {
vi.unstubAllEnvs()
await rm(stale, { force: true, recursive: true })
}
})
test('reports a missing entry, never an unknown state', async () => {
const stale = await project('src/app/(payload)/admin', 'export const importMap = { "@intecion/ipal-kit/admin#TotpRedirectProvider": X }')
const current = await project('src/app/(payload)/admin', `export const importMap = { "${ENTRY}": Y }`)