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:
Radosław Smoliński
2026-11-24 01:46:25 +01:00
co-authored by Claude Fable 5.2
parent 32faaa5e30
commit c56f0d41b8
34 changed files with 871 additions and 108 deletions
+36 -6
View File
@@ -3,7 +3,7 @@ import type { FieldAccess, PayloadRequest } from 'payload'
import { describe, expect, test, vi } from 'vitest'
import { adminOnly, adminOnlyField, adminOrEditor, adminOrEditorField, adminOrSelf, adminOrSelfIn, requireRole, requireRoleField } from '../src/modules/access/access.js'
import { hasMinimumRole, isAdmin, isEditor } from '../src/modules/access/predicates.js'
import { hasMinimumRole, isAdmin, isEditor, registerStaffCollections, staffRegistrySize } from '../src/modules/access/predicates.js'
import { buildRolesField } from '../src/modules/access/rolesField.js'
import { testEmailEndpoint } from '../src/modules/email/test/testEmailEndpoint.js'
@@ -17,11 +17,11 @@ const payload = { config: { admin: { user: 'users' } } } as never
describe('role predicates', () => {
test('hierarchy admin > editor > user', () => {
expect(isAdmin(admin)).toBe(true)
expect(isAdmin(editor)).toBe(false)
expect(isEditor(admin)).toBe(true)
expect(isEditor(user)).toBe(false)
expect(hasMinimumRole(user, 'user')).toBe(true)
expect(isAdmin(admin, payload)).toBe(true)
expect(isAdmin(editor, payload)).toBe(false)
expect(isEditor(admin, payload)).toBe(true)
expect(isEditor(user, payload)).toBe(false)
expect(hasMinimumRole(user, 'user', payload)).toBe(true)
})
test('unknown roles and odd shapes grant nothing', () => {
@@ -42,6 +42,36 @@ describe('role predicates', () => {
expect(isAdmin(member, withMembers)).toBe(true)
})
test('where the staff collections are not known, nobody is staff (browser bundle, middleware)', () => {
// no ipalKit config in this process (test/setup.ts clears the registry) and no source given
expect(isAdmin(admin)).toBe(false)
expect(isAdmin({ collection: 'customers', roles: ['admin'] })).toBe(false)
expect(hasMinimumRole(user, 'user')).toBe(false)
// …until the caller says where staff come from: the config (also the client config), or the list itself
expect(isAdmin(admin, { admin: { user: 'users' } })).toBe(true)
expect(isAdmin(admin, { staffCollections: ['users'] })).toBe(true)
expect(isAdmin({ collection: 'customers', roles: ['admin'] }, { staffCollections: ['users'] })).toBe(false)
})
test('two configs in one process: a call without a source gets only what both call staff', () => {
const a = {}
const b = {}
registerStaffCollections(a, ['users'])
expect(isAdmin(admin)).toBe(true)
registerStaffCollections(b, ['staff', 'customers'])
expect(isAdmin(admin)).toBe(false) // not staff in B
expect(isAdmin({ collection: 'customers', roles: ['admin'] })).toBe(false) // not staff in A
registerStaffCollections(b, ['users', 'customers'])
expect(isAdmin(admin)).toBe(true)
expect(isAdmin({ collection: 'customers', roles: ['admin'] })).toBe(false)
// with the source, each config gets its own answer
expect(isAdmin({ collection: 'customers', roles: ['admin'] }, { staffCollections: ['users', 'customers'] })).toBe(true)
// an instance taking over its config's token replaces the entry instead of adding one
const instance = {}
registerStaffCollections(instance, ['users'], a)
expect(staffRegistrySize()).toBe(2)
})
test('every ready access function refuses that account', () => {
const member = { id: 1, collection: 'members', roles: ['admin'] }
const req = { payload, user: member }