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
+22 -1
View File
@@ -20,13 +20,34 @@ export const requireRole =
({ req: { user } }) =>
hasMinimumRole(user, minimum)
/** Admins see all; others are constrained to their own document. */
/**
* Admins see all; others are constrained to their own document.
*
* It cannot know which collection it guards, so "own document" means "the
* document with my id". In a project with a second auth collection use
* `adminOrSelfIn(slug)` instead: with numeric ids (Postgres, SQLite) customer
* no. 5 would otherwise match staff account no. 5.
*/
export const adminOrSelf: Access = ({ req: { user } }) => {
if (isAdmin(user)) {return true}
if (!user) {return false}
return { id: { equals: user.id } }
}
/**
* `adminOrSelf` for the auth collection `slug`: only an account OF THAT
* collection is "self". An account of another auth collection gets nothing.
*
* { slug: 'users', auth: true, access: { read: adminOrSelfIn('users'), update: adminOrSelfIn('users') } }
*/
export const adminOrSelfIn =
(slug: string): Access =>
({ req: { user } }) => {
if (isAdmin(user)) {return true}
if (!user || (user as { collection?: unknown }).collection !== slug) {return false}
return { id: { equals: user.id } }
}
/**
* Field-level access (returns boolean only — no Where support).
* Use in field `access.read/update`.
+2 -1
View File
@@ -4,12 +4,13 @@ export {
adminOrEditor,
adminOrEditorField,
adminOrSelf,
adminOrSelfIn,
authenticated,
requireRole,
requireRoleField,
} from './access.js'
export { injectRoles } from './injectRoles.js'
export { hasMinimumRole, isAdmin, isEditor, userMeetsRole } from './predicates.js'
export { hasMinimumRole, isAdmin, isEditor, staffCollectionSlugs, userMeetsRole } from './predicates.js'
export { buildRolesField } from './rolesField.js'
export type { AccessOption, Role } from './types.js'
export { ROLE_HIERARCHY } from './types.js'
+32 -9
View File
@@ -2,6 +2,7 @@ import type { BasePayload } from 'payload'
import type { Role } from './types.js'
import { secondFactorPassed } from '../security/totp/secondFactor.js'
import { ROLE_HIERARCHY } from './types.js'
/**
@@ -45,23 +46,45 @@ export function isEditor(user: MaybeUser): boolean {
return hasMinimumRole(user, 'editor')
}
/**
* The auth collections whose accounts are staff: the one the admin panel logs
* in with (`admin.user`) plus `access.staffCollections`. Accounts of any other
* auth collection — customers, members — are visitors to the plugin.
*/
export function staffCollectionSlugs(payload?: Pick<BasePayload, 'config'>): string[] {
const config = payload?.config
if (!config) {return []}
const extra = (config.custom as { ipal?: { staffCollections?: string[] } } | undefined)?.ipal?.staffCollections ?? []
return [...new Set([config.admin?.user, ...extra].filter((s): s is string => typeof s === 'string' && s !== ''))]
}
/**
* Role check for the plugin's own staff features (Site Integrations, the
* e-mail test, stats, consent records, form submissions, the settings
* globals): true when the user holds at least `minimum` — or, when the
* user's auth collection has no `roles` field (the project omitted `access`),
* for every logged-in user. Without roles all accounts are equal, as in
* Payload's own default; nobody being "admin" would lock those features away.
* globals). True when the user holds at least `minimum`.
*
* Pass `req` (it has `payload` and `user`). An unknown collection counts as
* having roles — the strict side. A project with its own role scheme in
* another field narrows access through globalOverrides / collection overrides.
* Without roles: when the user belongs to a STAFF collection (the panel's
* login collection, or one named in `access.staffCollections`) and that
* collection has no `roles` field (the project omitted `access`), every
* account of it passes — without roles all staff accounts are equal, as in
* Payload's own default. An account of any other auth collection (a
* `customers` collection has no `roles` field either) never passes this way.
*
* With 2FA on, an account that has TOTP set up and has not entered the code in
* this session never passes (secondFactorPassed).
*
* Pass `req` (it has `payload` and `user`). Without `payload` only a held role
* counts — the strict side. A project with its own role scheme in another
* field narrows access through globalOverrides / collection overrides.
*/
export function userMeetsRole({ payload, user }: { payload?: BasePayload; user?: unknown }, minimum: Role): boolean {
if (!user || typeof user !== 'object') {return false}
if (!secondFactorPassed({ payload, user })) {return false}
if (hasMinimumRole(user as MaybeUser, minimum)) {return true}
const slug = (user as { collection?: unknown }).collection
const fields = typeof slug === 'string' ? payload?.collections?.[slug as never]?.config.fields : undefined
if (typeof slug !== 'string' || !staffCollectionSlugs(payload).includes(slug)) {return false}
const collection = payload?.collections?.[slug as never]?.config
const fields = (collection?.flattenedFields ?? collection?.fields) as Array<{ name?: string }> | undefined
if (!fields) {return false}
return !fields.some((f) => 'name' in f && f.name === 'roles')
return !fields.some((f) => f.name === 'roles')
}
+10
View File
@@ -34,4 +34,14 @@ export type AccessOption = {
* `false` = that account gets what was submitted (`defaultRole` by default).
*/
firstUserAdmin?: boolean
/**
* More auth collections whose accounts are staff, besides the one the admin
* panel logs in with (`admin.user`). Only accounts of a staff collection can
* pass the plugin's staff checks (`userMeetsRole`): the settings globals,
* Site integrations, form submissions, consent records, stats, the cookie
* scanner. A staff collection WITHOUT a `roles` field passes them all (every
* account is admin to the plugin); add `buildRolesField()` to it to tell
* admins, editors and users apart. Default: none.
*/
staffCollections?: string[]
}