Security review round 2: no exception by name, staff list fails closed, verify limit
R2-1: the sweep skipped every collection whose slug starts with payload-,
so a plugin's payload-... collection added after ipalKit was never guarded.
There is no exception by name or prefix now: every collection and global
is guarded, Payload's own included, and the job queue's run / queue /
cancel rules too.
R2-2: role predicates called without a source fail closed where the staff
collections are not known (browser bundle, middleware, edge): nobody is
staff. They accept req.payload, a config (also the client config) or
{ staffCollections } as the source.
R2-3: the staff list is filed per config / Payload instance; a call without
a source gets only what every config in the process calls staff.
R2-4: payload.config is watched on the instance, so a config swapped in by
a dev reload is guarded before any request or Local API call.
R2-5: verify attempts are limited per account (default 5 in 10 minutes,
twoFactor.verifyLimit), counted in memory, never skipped.
R2-6: a user object without collection owes the second factor; endpoints
the 2FA package may add on collections or globals are guarded too; a
warning names staff collections that 2FA does not cover; docs state what
is not covered, fix the one-argument examples and the doctor example.
HTTP tests for each on payload-2fa beta.7, run on SQLite, Postgres and
MongoDB (test adapter added).
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
32faaa5e30
commit
c56f0d41b8
@@ -13,19 +13,50 @@ import { ROLE_HIERARCHY } from './types.js'
|
||||
*/
|
||||
type MaybeUser = { roles?: null | Role[] } | null | Record<string, unknown> | undefined
|
||||
|
||||
/** `req.payload`, or anything with the sanitized config on it. */
|
||||
type MaybePayload = null | Pick<BasePayload, 'config'> | undefined
|
||||
/**
|
||||
* Where the staff collections come from: `req.payload` (anything with the
|
||||
* sanitized `config` on it), a config itself (also the client config from
|
||||
* `useConfig()` — it has `admin.user`), or the list given outright.
|
||||
*/
|
||||
type StaffSource = { admin?: { user?: string }; config?: StaffConfig; custom?: unknown; staffCollections?: string[] } | null | undefined
|
||||
type StaffConfig = { admin?: { user?: string }; custom?: unknown }
|
||||
type MaybePayload = Pick<BasePayload, 'config'> | StaffSource
|
||||
|
||||
/**
|
||||
* The staff collections of the config ipalKit last built in this process —
|
||||
* what the predicates fall back to when they get no `payload` (`isAdmin(req.user)`
|
||||
* in a hook or an endpoint). `null` where ipalKit never ran: a browser bundle.
|
||||
* The staff lists of the configs ipalKit built in this process, by owner: the
|
||||
* running Payload instance once it exists, a token of the config before that.
|
||||
* Used when a predicate gets no source (`isAdmin(req.user)` in a hook).
|
||||
*/
|
||||
let registeredStaff: null | string[] = null
|
||||
const registered = new Map<object, string[]>()
|
||||
|
||||
/** Called by ipalKit (config time, then onInit with the sanitized config). Not for projects. */
|
||||
export function registerStaffCollections(slugs: string[]): void {
|
||||
registeredStaff = [...new Set(slugs.filter(Boolean))]
|
||||
/**
|
||||
* Called by ipalKit: at config time with a token, then with the Payload
|
||||
* instance (`replaces` = that config's token) on init and after every reload.
|
||||
* Not for projects.
|
||||
*/
|
||||
export function registerStaffCollections(owner: object, slugs: string[], replaces?: object): void {
|
||||
if (replaces) {registered.delete(replaces)}
|
||||
registered.set(owner, [...new Set(slugs.filter(Boolean))])
|
||||
}
|
||||
|
||||
/** For tests. */
|
||||
export function clearStaffRegistry(): void {
|
||||
registered.clear()
|
||||
}
|
||||
|
||||
/** For tests: how many configs / instances have a staff list filed. */
|
||||
export const staffRegistrySize = (): number => registered.size
|
||||
|
||||
/**
|
||||
* The staff collections every config in this process agrees on, or null when
|
||||
* ipalKit never ran here (a browser bundle, Next middleware, the edge
|
||||
* runtime). Two configs with different lists: only what both call staff — a
|
||||
* call that does not say which config it means must not get the wider answer.
|
||||
*/
|
||||
function agreedStaff(): null | string[] {
|
||||
if (!registered.size) {return null}
|
||||
const lists = [...registered.values()]
|
||||
return lists[0].filter((slug) => lists.every((list) => list.includes(slug)))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,29 +65,33 @@ export function registerStaffCollections(slugs: string[]): void {
|
||||
* (`access.authCollection`) and `access.staffCollections`. Accounts of any
|
||||
* other auth collection — customers, members — are visitors to the plugin:
|
||||
* no role and no staff check counts for them, whatever fields they have.
|
||||
*
|
||||
* Pass `req.payload` (or a config, or `{ staffCollections: [...] }`). Without
|
||||
* an argument: what the configs built in this process agree on, `[]` when
|
||||
* there is none.
|
||||
*/
|
||||
export function staffCollectionSlugs(payload?: MaybePayload): string[] {
|
||||
const config = payload?.config
|
||||
if (!config) {return registeredStaff ?? []}
|
||||
export function staffCollectionSlugs(source?: MaybePayload): string[] {
|
||||
if (!source) {return agreedStaff() ?? []}
|
||||
const given = (source as { staffCollections?: unknown }).staffCollections
|
||||
if (Array.isArray(given)) {return given.filter((s): s is string => typeof s === 'string' && s !== '')}
|
||||
const config = ((source as { config?: StaffConfig }).config ?? source) as StaffConfig
|
||||
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 !== ''))]
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this account's roles may count at all: it belongs to a staff
|
||||
* collection. Checked before any role. An account without `collection` (a
|
||||
* user object built by hand) does not count either — pass the user Payload
|
||||
* authenticated, or add `collection` yourself in server code you trust.
|
||||
*
|
||||
* Only where no staff list is known (no `payload` given and ipalKit never ran
|
||||
* in this bundle — client components) the roles are taken as they are; that is
|
||||
* for showing or hiding things, never for access control.
|
||||
* collection. Checked before any role, and it fails closed:
|
||||
* - an account without `collection` (a user object built by hand) is not staff;
|
||||
* - where the staff collections are not known — no source given and ipalKit
|
||||
* never ran in this bundle (client components, Next middleware, the edge
|
||||
* runtime) — nobody is staff. Pass the source there: `isAdmin(user, config)`
|
||||
* with the config from `useConfig()`, or `isAdmin(user, { staffCollections: ['users'] })`.
|
||||
*/
|
||||
function isStaffAccount(user: MaybeUser, payload?: MaybePayload): boolean {
|
||||
function isStaffAccount(user: MaybeUser, source?: MaybePayload): boolean {
|
||||
if (!user || typeof user !== 'object') {return false}
|
||||
if (!payload?.config && registeredStaff === null) {return true}
|
||||
const slug = (user as { collection?: unknown }).collection
|
||||
return typeof slug === 'string' && staffCollectionSlugs(payload).includes(slug)
|
||||
return typeof slug === 'string' && staffCollectionSlugs(source).includes(slug)
|
||||
}
|
||||
|
||||
/** Safely extracts the roles array from a loosely-typed user. */
|
||||
@@ -80,7 +115,9 @@ function highestRoleIndex(user: MaybeUser): number {
|
||||
*
|
||||
* Pass `req.payload` as the third argument where you have it (access
|
||||
* functions, hooks, endpoints); without it the staff collections of the
|
||||
* config ipalKit built in this process are used.
|
||||
* config ipalKit built in this process are used — and where there is none
|
||||
* (a browser bundle, middleware) the answer is false: pass the config from
|
||||
* `useConfig()` or `{ staffCollections: ['users'] }` there.
|
||||
*/
|
||||
export function hasMinimumRole(user: MaybeUser, minimum: Role, payload?: MaybePayload): boolean {
|
||||
if (!isStaffAccount(user, payload)) {return false}
|
||||
|
||||
Reference in New Issue
Block a user