A staff collection whose accounts can hold editor or admin gets the admin rules (H-2, J-3)

In the released 1.16.7. The plugin's checks go by the value on the account, but a project's own staff collection
without rules got the admin rules only when ALL its role values were the plugin's; otherwise 'an account of this
collection' could create, change and delete accounts. Measured on bc0fa65 (SQLite; the first shape also on Postgres
and MongoDB, 2FA off and on): with roles user/editor/admin/boss, with a text and with a json roles field, config-time
and added by a later plugin, an account with the role user creates an admin (201) who reads Site integrations, changes
another staff collection's roles and lists the panel's accounts (200 / 200 / 200), takes the admin over and deletes it;
with viewer/editor a viewer creates an editor who gets past the access rule of Site settings.
canHoldStaffRole(collection): a roles select with editor or admin among its values, or a roles field whose values
cannot be listed. Then: create / delete / unlock an admin, update an admin or the account itself, roles only by an
admin. A project's own rules win. A warning at start when no staff account is an admin and such a collection exists.
The built-site fixture gets a config-time and a late collection of that kind; nothing here touches copied collections.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 02:59:44 +01:00
co-authored by Claude Fable 5.2
parent 01219b69d3
commit be2e6d52d9
10 changed files with 318 additions and 10 deletions
+14
View File
@@ -9,6 +9,18 @@ import type { Config, Plugin } from 'payload'
*/
export const E2E_LATE_STAFF = process.env.E2E_LATE_STAFF === '1'
/** Slugs the fixture names as staff (payload.config.ts → `access.staffCollections`). */
export const E2E_STAFF_SLUGS = ['late-staff', 'e2e-crew', 'e2e-late-text']
/**
* Listed BEFORE ipalKit: a staff collection of the project's own, without rules, whose roles are the plugin's values
* PLUS one more. `admin` on an account of it makes a plugin admin, so its accounts must not hand that value out.
*/
export const e2eEarlyStaff: Plugin = (config: Config) =>
E2E_LATE_STAFF
? { ...config, collections: [...(config.collections ?? []), { slug: 'e2e-crew', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin', 'boss'], saveToJWT: true }] }] }
: config
export const e2eLateStaff: Plugin = (config: Config) =>
E2E_LATE_STAFF
? {
@@ -17,6 +29,8 @@ export const e2eLateStaff: Plugin = (config: Config) =>
...(config.collections ?? []),
// no access rules at all: Payload's defaults ("any logged-in user") are what ipalKit must replace
{ slug: 'late-staff', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin'], saveToJWT: true }], versions: true },
// a late staff collection whose `roles` is a text field: any value can be written there, `admin` included
{ slug: 'e2e-late-text', auth: true, fields: [{ name: 'roles', type: 'text', hasMany: true }] },
{ slug: 'e2e-visitors', access: { create: () => true }, auth: true, fields: [] },
],
}
+29 -2
View File
@@ -1,7 +1,7 @@
import { type APIRequestContext, expect, test } from '@playwright/test'
import { randomUUID } from 'node:crypto'
import { totp } from './totp'
import { secondTestAdmin, totp } from './totp'
/**
* The account rules on a BUILT site, for a staff collection that a plugin listed after ipalKit adds
@@ -25,7 +25,8 @@ test.describe('a late staff collection on a built site', () => {
const tag = randomUUID().slice(0, 8)
// a fully signed-in admin of the panel's collection
const admin = await context(playwright, baseURL!)
expect((await admin.post('/api/users/login', { data: { email, password } })).status()).toBe(200)
// (the second test admin: the smoke and back-link specs spend the first one's 2FA attempts)
expect((await admin.post('/api/users/login', { data: { email: secondTestAdmin(email!), password } })).status()).toBe(200)
expect((await (await admin.post('/api/verify-totp', { data: { token: totp(secret!) } })).json()).ok).toBe(true)
// anonymous: the first-user route of the (possibly empty) late collection, in two spellings
@@ -69,6 +70,32 @@ test.describe('a late staff collection on a built site', () => {
// a short password is refused there too (the password rule)
expect((await admin.post('/api/late-staff', { data: { email: `s-${tag}@example.com`, password: 'tooshort', roles: ['user'] } })).status()).toBe(400)
// Staff collections whose accounts can hold `admin` although the roles are not just the plugin's — a select with
// one more value (in the config), a text field (added by a later plugin): the lowest account does not make an
// admin, take the collection's admin over, or delete it, and reaches nothing an admin reaches.
for (const slug of ['e2e-crew', 'e2e-late-text']) {
const itsAdmin = await admin.post(`/api/${slug}`, { data: { email: `a-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['admin'] } })
const itsUser = await admin.post(`/api/${slug}`, { data: { email: `u-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['user'] } })
expect([slug, itsAdmin.status(), itsUser.status()]).toEqual([slug, 201, 201])
const itsAdminId = (await itsAdmin.json()).doc.id as string
const itsUserId = (await itsUser.json()).doc.id as string
const api = await context(playwright, baseURL!)
expect((await api.post(`/api/${slug}/login`, { data: { email: `u-${tag}@${slug}.example.com`, password: PASSWORD } })).status()).toBe(200)
expect([slug, {
create: (await api.post(`/api/${slug}`, { data: { email: `made-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['admin'] } })).status(),
integrations: (await api.get('/api/globals/site-integrations')).status(),
patch: (await api.patch(`/api/${slug}/${itsAdminId}`, { data: { password: `taken-${PASSWORD}` } })).status(),
users: (await api.get('/api/users')).status(),
// last: it would remove the account the other calls are about
zDelete: (await api.delete(`/api/${slug}/${itsAdminId}`)).status(),
}]).toEqual([slug, { create: 403, integrations: 403, patch: 403, users: 403, zDelete: 403 }])
await api.patch(`/api/${slug}/${itsUserId}`, { data: { roles: ['admin'] } })
expect((await (await admin.get(`/api/${slug}/${itsUserId}`)).json()).roles, `${slug}: its own roles`).toEqual(['user'])
expect((await api.patch(`/api/${slug}/${itsUserId}`, { data: { password: `own-${PASSWORD}` } })).status(), `${slug}: its own password`).toBe(200)
for (const id of [itsAdminId, itsUserId]) {expect((await admin.delete(`/api/${slug}/${id}`)).status()).toBe(200)}
await api.dispose()
}
// an admin: everything, and it cleans up
expect((await admin.get('/api/late-staff/versions')).status()).toBe(200)
expect((await admin.post('/api/late-staff/unlock', { data: { email: `low-${tag}@example.com` } })).status()).toBe(200)
+5 -2
View File
@@ -19,7 +19,7 @@ import { Pages } from './collections/Pages'
import { Posts } from './collections/Posts'
// @starter:endif
// @starter:if demo
import { E2E_LATE_STAFF, e2eLateStaff } from '../e2e/late-staff-fixture'
import { E2E_LATE_STAFF, E2E_STAFF_SLUGS, e2eEarlyStaff, e2eLateStaff } from '../e2e/late-staff-fixture'
// @starter:endif
import { contentConfig } from './content.config'
import { db } from './db'
@@ -56,11 +56,14 @@ export default buildConfig({
sharp,
typescript: { outputFile: path.resolve(dirname, 'payload-types.ts') },
plugins: [
// @starter:if demo
e2eEarlyStaff, // a test fixture, a no-op without E2E_LATE_STAFF=1 (e2e/late-staff-fixture.ts)
// @starter:endif
ipalKit({
i18n: i18nConfig,
// @starter:if demo
// (the staffCollections entry exists only for e2e/late-staff.spec.ts, with E2E_LATE_STAFF=1)
access: { authCollection: 'users', ...(E2E_LATE_STAFF ? { staffCollections: ['late-staff'] } : {}) },
access: { authCollection: 'users', ...(E2E_LATE_STAFF ? { staffCollections: E2E_STAFF_SLUGS } : {}) },
// @starter:else
//: access: { authCollection: 'users' },
// @starter:endif