2FA setup required: review fixes (SR-1 to SR-9)

SR-1: /api/ipal/admin-icon sends the upload security headers (sandbox policy,
nosniff) with the bytes: an SVG is shown, never run. fetchPublicImage stops
reading at the size limit.
SR-2: requireSetup.exempt exempts only on exactly true; an async function, and a
windowHours that is not a number or false, stop the start.
SR-3: on Postgres / SQLite the missing column makes every login fail: the log
says so, the docs call the migration a required step of the same deploy, and
ipal-doctor reports a SQL project without it.
SR-4: an admin's reset also ends the account's sessions.
SR-5: the reset needs a session that entered its own TOTP code by default.
SR-6: the doctor's import-map rule covers TwoFactorStatusField and TwoFactorAlert.
SR-7: building blocks exported; dashboard notice, status field, setup notice and
the server-side image fetch can be switched off one by one.
SR-8: totpSetupFrom is on the 2FA collection whenever 2FA is on.
SR-9: both ways out of "every administrator is blocked" in security.md.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-24 19:32:34 +01:00
co-authored by Claude Fable 5.2
parent 9d2ab43438
commit 75a6f5209e
34 changed files with 780 additions and 103 deletions
+32 -13
View File
@@ -194,6 +194,9 @@ export const ipalKit = (options: IpalOptions): Plugin => {
/** 2FA collection when "setup required" is on — its existing accounts get their setup window in onInit. */
let setupRequiredFor: null | string = null
let setupWindowHours = 0
/** requireSetup.setupNotice / statusField (on unless switched off). */
let setupNotice = true
let statusField = false
if (options.twoFactor !== false) {
const tf = options.twoFactor
if (!tf?.issuer) {
@@ -272,6 +275,10 @@ 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 window-start field is part of the 2FA collection whenever 2FA is on — the schema must
// not depend on requireSetup / guard (setupRequired.ts, buildSetupFields). The panel's status
// field joins it below, when the mode is on. Validates requireSetup at start either way.
if (tf.requireSetup !== false) {resolveRequireSetup(tf.requireSetup)}
// 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
@@ -298,24 +305,29 @@ export const ipalKit = (options: IpalOptions): Plugin => {
// "Setup required" (default): an account without TOTP reaches nothing but the setup
// screen, and only within the window; requireSetup: false = the package's rule.
const setup = resolveRequireSetup(tf.requireSetup)
const setupParts = typeof tf.requireSetup === 'object' ? tf.requireSetup : {}
config.custom = { ...config.custom, ipal: { ...config.custom?.ipal, twoFactor: { collection: target, ...setup } } }
if (setup.requireSetup) {
setupRequiredFor = target
setupWindowHours = setup.setupWindowMs / 3_600_000
setupNotice = setupParts.setupNotice !== false
statusField = setupParts.statusField !== false
// The stamp hook only while the mode is on: an account made while it is off gets its
// window when the mode is switched on (the start-up step), not a creation date long past.
config.collections = (config.collections ?? []).map((c) =>
c.slug === target
? { ...c, fields: [...c.fields, ...buildSetupFields()], hooks: { ...c.hooks, beforeChange: [...(c.hooks?.beforeChange ?? []), stampSetupFrom] } }
: c,
c.slug === target ? { ...c, hooks: { ...c.hooks, beforeChange: [...(c.hooks?.beforeChange ?? []), stampSetupFrom] } } : c,
)
config.admin = {
...config.admin,
components: {
...config.admin?.components,
beforeDashboard: [
...(config.admin?.components?.beforeDashboard ?? []),
{ path: '@intecion/ipal-kit/admin#TwoFactorAlert', serverProps: { adminRoute: options.adminRoute ?? '/admin' } },
],
},
if (setupParts.dashboardNotice !== false) {
config.admin = {
...config.admin,
components: {
...config.admin?.components,
beforeDashboard: [
...(config.admin?.components?.beforeDashboard ?? []),
{ path: '@intecion/ipal-kit/admin#TwoFactorAlert', serverProps: { adminRoute: options.adminRoute ?? '/admin' } },
],
},
}
}
}
// An admin resets an account's TOTP (lost phone) / reopens its setup window.
@@ -330,6 +342,11 @@ export const ipalKit = (options: IpalOptions): Plugin => {
)
}
// The window start (data) whenever 2FA is on; the status / reset control when the mode is.
config.collections = (config.collections ?? []).map((c) =>
c.slug === target ? { ...c, fields: [...c.fields, ...buildSetupFields({ statusField })] } : c,
)
// The package's texts are long and informal: ipal-kit's (admin-ux.md) go over
// them, the project's own i18n.translations over both. The 2FA screens get a
// tab title instead of "Payload" (view meta is a plain string: the site's language).
@@ -375,7 +392,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
p && (typeof p === 'string' ? p : p.path) === TOTP_PACKAGE_PROVIDER
? {
...ours,
clientProps: { forceSetup: true, ...(setupRequiredFor ? { requireSetup: true, setupWindowHours } : {}), ...ours.clientProps },
clientProps: { forceSetup: true, ...(setupRequiredFor && setupNotice ? { requireSetup: true, setupWindowHours } : {}), ...ours.clientProps },
serverProps: { ...(typeof p === 'object' ? p.serverProps : {}), ...ours.serverProps },
}
: p,
@@ -441,6 +458,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
const serverProps: Record<string, unknown> = {}
if (wl.settingsSlug) {serverProps.settingsSlug = wl.settingsSlug}
if (wl.nameField) {serverProps.nameField = wl.nameField}
if (wl.inlineImages === false) {serverProps.inlineImages = false}
const logoProps = { ...serverProps, ...(wl.logoField ? { imageField: wl.logoField } : {}) }
const iconProps = { ...serverProps, ...(wl.iconField ? { imageField: wl.iconField } : {}) }
@@ -513,6 +531,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
iconField: brandFields.iconField,
logoField: brandFields.logoField,
nameField: brandFields.nameField,
serveIcon: options.adminMeta?.serveIcon,
settingsSlug,
}),
]