Security: staff checks only for staff collections; 2FA covers what the plugin adds

Staff check (userMeetsRole): the 'no roles field = every account passes'
rule applied to the account's own collection, so an account of a second
auth collection (customers) passed every staff check. It now applies only
to staff collections: the panel's login collection plus the new
access.staffCollections. The injected auth collection uses the new
adminOrSelfIn(slug), so an account of another collection with the same
numeric id is not 'self'.

2FA: @clocklimited/payload-2fa wraps the collections and globals present
when it runs; ipalKit's own globals and collections were added later and
stayed unwrapped, so a password without the TOTP code opened them over
REST and GraphQL. ipal-kit now has its own second-factor check
(secondFactorPassed / requireSecondFactor): applied to every collection
and global at the end of ipalKit, again in onInit for entities added by
plugins listed after it, and inside userMeetsRole and the endpoints that
take a project access function. It fails closed: an unrecognised user
shape is refused, and startup stops when the injected 2FA plugin lacks
the parts the check reads (twoFactor.guard: false opts out).

Tests through real HTTP requests (REST and GraphQL) against Payload's
route handlers, plus the reference smoke test. Docs, agent guide, skills
and the reference skills copy updated; changelog entry without a version.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-24 00:31:55 +01:00
co-authored by Claude Fable 5.2
parent bc1045a5b7
commit 0c61ac231b
40 changed files with 1224 additions and 63 deletions
+45 -3
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, adminOrSelf, injectRoles } from './modules/access/index.js'
import { adminOnly, adminOrSelfIn, injectRoles } 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,6 +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 { 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'
@@ -125,9 +126,11 @@ export const ipalKit = (options: IpalOptions): Plugin => {
access: {
create: adminOnly,
delete: adminOnly,
read: adminOrSelf,
// "self" = an account of THIS collection: with numeric ids a customer
// account no. 5 must not match staff account no. 5.
read: adminOrSelfIn(authSlug),
unlock: adminOnly,
update: adminOrSelf,
update: adminOrSelfIn(authSlug),
...options.access?.authAccess,
},
}
@@ -152,6 +155,9 @@ export const ipalKit = (options: IpalOptions): Plugin => {
// or import it, so projects that opt out (twoFactor: false) needn't install it.
// Wrapping access control (not just admin UI) means TOTP gates data access —
// no API bypass.
// ipal-kit's own second-factor check (secondFactor.ts) covers what the 2FA
// package cannot: everything added to the config after the package ran.
let secondFactorGuard = false
if (options.twoFactor !== false) {
const tf = options.twoFactor
if (!tf?.issuer) {
@@ -203,6 +209,26 @@ export const ipalKit = (options: IpalOptions): Plugin => {
totp: { issuer: tf.issuer },
})(config)
// 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
// package's `totp` strategy and `hasTotp` field: when those are not there,
// this is a version it was not written for — refuse to start rather than
// run with a second factor that may protect nothing.
if (tf.guard !== false) {
const missing = missingTotpParts(config, target)
if (missing.length) {
throw new Error(
`[ipal] twoFactor: the injected 2FA plugin did not add ${missing.join(', ')} to "${target}". ` +
'ipal-kit enforces the second factor on its own globals and collections through them, so it ' +
'cannot do that with this version of @clocklimited/payload-2fa. Use a supported version, or pass ' +
'twoFactor.guard: false to rely on the package alone (docs/security.md — not recommended).',
)
}
secondFactorGuard = true
config.custom = { ...config.custom, ipal: { ...config.custom?.ipal, twoFactor: { collection: target } } }
}
// 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).
@@ -355,6 +381,10 @@ 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 } }
}
}
// --- seo: apply @payloadcms/plugin-seo directly ---
@@ -596,10 +626,22 @@ export const ipalKit = (options: IpalOptions): Plugin => {
},
}
// --- second factor on everything in the config by now: the project's
// collections (wrapped by the package already; twice is harmless), the
// plugin's globals and collections, the form builder's.
if (secondFactorGuard) {
config = guardConfigAccess(config)
}
// --- hooks: onInit ---
const incomingOnInit = config.onInit
config.onInit = async (payload) => {
if (incomingOnInit) {await incomingOnInit(payload)}
// …and on what plugins listed after ipalKit added: they did not exist above.
if (secondFactorGuard) {
const late = guardPayloadAccess(payload)
if (late.length) {payload.logger.info(`[ipal] 2FA also enforced on entities added after ipalKit: ${late.join(', ')}.`)}
}
await migrateCookieSettingsSaveButton(payload)
await migrateTitleSeparator(payload, settingsSlug)
if (adminMeta) {await syncAdminMeta(payload, adminMeta, { nameField: brandFields.nameField, settingsSlug })}