Security review fixes: staff collection first everywhere, 2FA package endpoints guarded, per-entity opt-out, hot reload

F1: roles count only for accounts of a staff collection, in every exported
predicate and access builder (isAdmin, isEditor, hasMinimumRole, adminOnly,
adminOrEditor, requireRole, adminOrSelf, adminOrSelfIn, the Field variants,
the roles field) and first in userMeetsRole. An account of another auth
collection with its own roles: ['admin'] gets nothing. Staff collections:
admin.user, access.authCollection, access.staffCollections. Predicates take
req.payload as an optional last argument and otherwise use the staff
collections of the config ipalKit built in this process.

F2: every endpoint @clocklimited/payload-2fa registers answers accounts of
the 2FA collection only (setup-totp wrote the secret into the 2FA collection
under the caller's id, whatever collection the caller came from); remove and
the admin reset/remove endpoints also need the second factor.

F3: per-entity opt-out honoured, operation by operation: the package's
custom.totp.disableAccessWrapper and ipal-kit's custom.ipal.secondFactor
(passed on to the package); the package's global disableAccessWrapper too.

F4: a no-user auth strategy on the 2FA collection guards the live config at
the start of each request, so entities from plugins listed after ipalKit
stay guarded after a dev hot reload and with disableOnInit.

Tests through HTTP for all four; the 2FA tests now run on payload-2fa
3.0.0-beta.7 (what client sites use), a second file on beta.1 (alias), and
the HTTP files can run against Postgres (IPAL_TEST_POSTGRES_URI). Docs,
agent guide, skills and changelog updated; the not-yet-enrolled limitation
is stated plainly.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-24 01:02:34 +01:00
co-authored by Claude Fable 5.2
parent 97ee1289aa
commit 32faaa5e30
30 changed files with 836 additions and 187 deletions
+35 -7
View File
@@ -11,7 +11,7 @@ import { buildNotifications } from './globals/Notifications/index.js'
import { applyGlobalOverrides } from './globals/overrides.js'
import { buildSiteIntegrations } from './globals/SiteIntegrations/index.js'
import { buildSiteSettings } from './globals/SiteSettings/index.js'
import { adminOnly, adminOrSelfIn, injectRoles } from './modules/access/index.js'
import { adminOnly, adminOrSelfIn, injectRoles, registerStaffCollections, staffCollectionSlugs } from './modules/access/index.js'
import { applyAdminI18n } from './modules/admin-i18n/apply.js'
import { buildAdminImageEndpoints } from './modules/branding/adminImages.js'
import { buildAdminMeta, DEFAULT_AGENCY, syncAdminMeta } from './modules/branding/adminMeta.js'
@@ -33,7 +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 { guardConfigAccess, guardPayloadAccess, missingTotpParts } from './modules/security/totp/secondFactor.js'
import { buildGuardStrategy, ensurePayloadGuarded, guardConfigAccess, guardTotpEndpoint, missingTotpParts, passOptOutsToPackage } from './modules/security/totp/secondFactor.js'
import { TOTP_TEXTS, TOTP_VIEW_TITLE } from './modules/security/totp/totpTexts.js'
import { buildSeoPlugin, injectAutoFillMeta, injectSeoTabs } from './modules/seo/index.js'
import { buildIndexNowHook } from './modules/seo/indexNow.js'
@@ -203,12 +203,21 @@ export const ipalKit = (options: IpalOptions): Plugin => {
`"${adminSlug}", or pass twoFactor.allowCollectionMismatch: true if that is deliberate.`,
)
}
const endpointsBefore = new Set(config.endpoints ?? [])
// custom.ipal.secondFactor (ipal-kit's per-entity opt-out) → the package's own key
config = passOptOutsToPackage(config)
config = await totp({
collection: target,
forceSetup: true, // ENFORCED — every user must set up TOTP; no opt-out
totp: { issuer: tf.issuer },
})(config)
// The package's endpoints (setup, verify, remove, admin reset / remove) take
// `req.user` from ANY auth collection and act on the 2FA collection by id.
// Only accounts of the 2FA collection may use them (guardTotpEndpoint) —
// whatever the package registered, also paths a later version adds.
config.endpoints = (config.endpoints ?? []).map((e) => (endpointsBefore.has(e) ? e : guardTotpEndpoint(e, target)))
// 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
// end of this function (and other plugins' in onInit). The guard reads the
@@ -227,6 +236,16 @@ export const ipalKit = (options: IpalOptions): Plugin => {
}
secondFactorGuard = true
config.custom = { ...config.custom, ipal: { ...config.custom?.ipal, twoFactor: { collection: target } } }
// Called by Payload on every request before any access function: guards whatever
// the live config has that was added after ipalKit (start, dev hot reload).
const sweep = buildGuardStrategy((payload, late) =>
payload.logger.info(`[ipal] 2FA also enforced on entities added after ipalKit: ${late.join(', ')}.`),
)
config.collections = (config.collections ?? []).map((c) =>
c.slug === target && typeof c.auth === 'object'
? { ...c, auth: { ...c.auth, strategies: [sweep as never, ...(c.auth.strategies ?? [])] } } // first: a strategy that finds a user ends the round
: c,
)
}
// The package's texts are long and informal: ipal-kit's (admin-ux.md) go over
@@ -381,10 +400,17 @@ export const ipalKit = (options: IpalOptions): Plugin => {
// --- access: inject roles into the client's auth collection ---
if (options.access) {
config = injectRoles(config, options.access)
// Further staff collections for userMeetsRole (the panel's login collection always is one).
if (options.access.staffCollections?.length) {
config.custom = { ...config.custom, ipal: { ...config.custom?.ipal, staffCollections: options.access.staffCollections } }
}
// Staff collections besides the panel's login collection: the one that carries
// the roles, and the ones the project names. Roles and staff checks count only
// for accounts of a staff collection (staffCollectionSlugs).
const staff = [...new Set([options.access.authCollection, ...(options.access.staffCollections ?? [])])]
config.custom = { ...config.custom, ipal: { ...config.custom?.ipal, staffCollections: staff } }
}
// For predicates called without `req.payload` (isAdmin(req.user) in a hook): what this
// config says now; onInit repeats it with the sanitized config (admin.user as Payload resolved it).
{
const panel = config.admin?.user ?? (config.collections ?? []).find((c) => c.auth)?.slug
registerStaffCollections(staffCollectionSlugs({ config: { admin: { user: panel }, custom: config.custom } } as never))
}
// --- seo: apply @payloadcms/plugin-seo directly ---
@@ -637,9 +663,11 @@ export const ipalKit = (options: IpalOptions): Plugin => {
const incomingOnInit = config.onInit
config.onInit = async (payload) => {
if (incomingOnInit) {await incomingOnInit(payload)}
registerStaffCollections(staffCollectionSlugs(payload))
// …and on what plugins listed after ipalKit added: they did not exist above.
// (The guard strategy does the same on the first request — after a hot reload too.)
if (secondFactorGuard) {
const late = guardPayloadAccess(payload)
const late = ensurePayloadGuarded(payload)
if (late.length) {payload.logger.info(`[ipal] 2FA also enforced on entities added after ipalKit: ${late.join(', ')}.`)}
}
await migrateCookieSettingsSaveButton(payload)