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:
co-authored by
Claude Fable 5.2
parent
bc1045a5b7
commit
0c61ac231b
@@ -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')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user