K-1, K-2: only a roles field that holds a LIST counts; the start-up check asks each collection its own way

K-1 the plugin counts roles only from an array (getRoles). A single-valued roles field — a select, text or
    relationship without hasMany, a radio — never makes a plugin admin, yet canHoldStaffRole said true: on a
    project's own login collection with a single role select nobody could create, change, delete or unlock accounts,
    the account holding admin included. rolesAreAList(collection) is now a precondition of the admin rules. The same
    for hasPluginRoles: a single select whose values are all the plugin's had the admin rules already in 1.16.7 and
    gets 'an account of this collection' now — its accounts manage each other again; the value still grants nothing.
    A relationship with hasMany counts (ids that are text).
K-2 staffCollectionsNobodyManages returns { stuck, unchecked } and checks collection by collection: no query where
    the field cannot hold admin; a count for a select with hasMany; for text / json / relationship the accounts are
    read and the plugin's own test is applied. Where it cannot check it says so and claims nothing.
Tests: the reviewer's shapes over HTTP and the warning's table against a running Payload, 2FA off and on.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 03:44:25 +01:00
co-authored by Claude Fable 5.2
parent ccc6f85924
commit c3d95988e1
11 changed files with 406 additions and 47 deletions
+49 -7
View File
@@ -26,10 +26,6 @@ import { startPayloadServer } from './http/server.js'
const PASSWORD = 'a-long-test-password-1'
const ROLES: Field = { name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin', 'boss'], saveToJWT: true }
const TEXT_ROLES: Field = { name: 'roles', type: 'text', hasMany: true }
const late: Plugin = (config: Config) => ({
...config,
collections: [...(config.collections ?? []), { slug: 'late-crew', auth: true, fields: [ROLES] }, { slug: 'late-text', auth: true, fields: [TEXT_ROLES] }],
})
/** The shapes: the lowest role of the collection, and the plugin role an account of it must not be able to hand out. */
const SHAPES: { low: string[]; other?: string[]; slug: string; top: 'admin' | 'editor' }[] = [
{ slug: 'crew', low: ['user'], other: ['boss'], top: 'admin' },
@@ -38,8 +34,29 @@ const SHAPES: { low: string[]; other?: string[]; slug: string; top: 'admin' | 'e
{ slug: 'text-roles', low: ['user'], other: ['anything'], top: 'admin' },
{ slug: 'late-text', low: ['user'], top: 'admin' },
{ slug: 'json-roles', low: ['user'], top: 'admin' },
// a relationship with hasMany to documents whose ids are text: at depth 0 the account's `roles` is ['admin']
{ slug: 'rel-roles', low: ['user'], other: ['boss'], top: 'admin' },
]
/**
* Review of branch A (K-1): `roles` fields that hold ONE value. The plugin counts roles only from an array, so
* an account whose `roles` is `'admin'` there is nobody to the plugin: nothing to protect, and the collection's
* accounts keep managing each other exactly as on 1.16.7 (Payload's default for its own accounts).
*/
const SINGLE: { field: Field; low: unknown; slug: string; top: unknown }[] = [
{ slug: 'single', field: { name: 'roles', type: 'select', options: ['user', 'editor', 'admin', 'boss'] }, low: 'user', top: 'admin' },
{ slug: 'single-plugin', field: { name: 'roles', type: 'select', options: ['user', 'editor', 'admin'] }, low: 'user', top: 'admin' }, // all the plugin's values: admin rules on 1.16.7
{ slug: 'single-late', field: { name: 'roles', type: 'select', options: ['user', 'editor', 'admin', 'boss'] }, low: 'user', top: 'admin' },
{ slug: 'radio', field: { name: 'roles', type: 'radio', options: ['user', 'admin'] }, low: 'user', top: 'admin' },
{ slug: 'text-one', field: { name: 'roles', type: 'text' }, low: 'user', top: 'admin' },
{ slug: 'rel-one', field: { name: 'roles', type: 'relationship', relationTo: 'role-names' }, low: 'user', top: 'admin' },
]
const late: Plugin = (config: Config) => ({
...config,
collections: [...(config.collections ?? []), { slug: 'late-crew', auth: true, fields: [ROLES] }, { slug: 'late-text', auth: true, fields: [TEXT_ROLES] }, { slug: 'single-late', auth: true, fields: [SINGLE.find((x) => x.slug === 'single-late')!.field] }],
})
/** One Payload instance per test file: run from a file of its own for 2FA off and on. */
export function adminValueSuite(twoFactor: boolean): void {
test(`a staff collection whose accounts can hold editor or admin: the admin rules (2FA on: ${twoFactor})`, async () => {
@@ -51,6 +68,9 @@ export function adminValueSuite(twoFactor: boolean): void {
{ slug: 'viewers', auth: true, fields: [{ ...ROLES, options: ['viewer', 'editor'] } as Field] },
{ slug: 'text-roles', auth: true, fields: [TEXT_ROLES] },
{ slug: 'json-roles', auth: true, fields: [{ name: 'roles', type: 'json' }] },
{ slug: 'role-names', fields: [{ name: 'id', type: 'text' }] }, // role documents with text ids
{ slug: 'rel-roles', auth: true, fields: [{ name: 'roles', type: 'relationship', hasMany: true, relationTo: 'role-names' }] },
...SINGLE.filter((x) => x.slug !== 'single-late').map((x) => ({ slug: x.slug, auth: true as const, fields: [x.field] })),
// its own values with neither `editor` nor `admin`: the plugin cannot tell who its admins are — unchanged
{ slug: 'team', auth: true, fields: [{ ...ROLES, options: ['lead', 'member'] } as Field] },
{ slug: 'customers', access: { create: () => true }, auth: true, fields: [] },
@@ -60,7 +80,7 @@ export function adminValueSuite(twoFactor: boolean): void {
editor: lexicalEditor(),
plugins: [
ipalKit({
access: { authCollection: 'users', staffCollections: ['crew', 'agents', 'team', 'late-crew', 'viewers', 'text-roles', 'json-roles', 'late-text'] },
access: { authCollection: 'users', staffCollections: ['crew', 'agents', 'team', 'late-crew', 'viewers', 'text-roles', 'json-roles', 'late-text', 'rel-roles', ...SINGLE.map((x) => x.slug)] },
i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] },
twoFactor: twoFactor ? { issuer: 'Example', plugin: payloadTotp, verifyLimit: false } : false,
}),
@@ -74,7 +94,7 @@ export function adminValueSuite(twoFactor: boolean): void {
type Session = Record<string, string>
const call = (method: string, pathname: string, session: Session = {}, body?: unknown) =>
fetch(`${server.url}/api${pathname}`, { body: body === undefined ? undefined : JSON.stringify(body), headers: { 'content-type': 'application/json', ...session }, method })
const make = (collection: string, email: string, roles: string[]) => payload.create({ collection: collection as never, data: { email, password: PASSWORD, roles } as never })
const make = (collection: string, email: string, roles: unknown) => payload.create({ collection: collection as never, data: { email, password: PASSWORD, roles } as never })
const login = async (collection: string, email: string, password = PASSWORD): Promise<null | Session> => {
const body = (await (await call('POST', `/${collection}/login`, {}, { email, password })).json()) as { token?: string }
return body.token ? { authorization: `JWT ${body.token}` } : null
@@ -82,7 +102,9 @@ export function adminValueSuite(twoFactor: boolean): void {
const rolesOf = async (collection: string, id: unknown) => ((await payload.findByID({ id: id as never, collection: collection as never })) as { roles?: string[] }).roles
try {
const agent = await make('agents', '[email protected]', ['user'])
await call('POST', '/customers', {}, { email: '[email protected]', password: PASSWORD })
await make('agents', '[email protected]', ['admin'])
for (const id of ['user', 'admin', 'boss', 'editor']) {await payload.create({ collection: 'role-names' as never, data: { id } as never })}
// IPAL_TEST_SHAPE=<slug>: one shape only — for measuring an older tree, where the first open shape stops the run
for (const { slug, low, other, top } of SHAPES.filter((shape) => !process.env.IPAL_TEST_SHAPE || shape.slug === process.env.IPAL_TEST_SHAPE)) {
// the account the others must not take over: the collection's highest (an admin, or — `viewers` — an editor)
@@ -119,7 +141,7 @@ export function adminValueSuite(twoFactor: boolean): void {
const me = (await login(slug, `low@${slug}.example.com`))!
expect([slug, (await call('PATCH', `/${slug}/${mine.id}`, me, { password: 'my-own-new-password-1' })).status]).toEqual([slug, 200])
// who manages the accounts: an admin of the collection where it has one, and an admin of another staff collection
const managers = [(await login('agents', '[email protected]'))!, ...(top === 'admin' ? [(await login(slug, `top@${slug}.example.com`))!] : [])]
const managers = [(await login('agents', '[email protected]'))!, ...(top === 'admin' && slug !== 'rel-roles' ? [(await login(slug, `top@${slug}.example.com`))!] : [])] // (over REST a related role arrives as a document, not as 'admin': that account is no admin there)
for (const [index, manager] of managers.entries()) {
expect([slug, index, (await call('POST', `/${slug}`, manager, { email: `by-manager-${index}@${slug}.example.com`, password: PASSWORD, roles: low })).status, (await call('PATCH', `/${slug}/${mine.id}`, manager, { roles: low })).status]).toEqual([slug, index, 201, 200])
}
@@ -129,6 +151,26 @@ export function adminValueSuite(twoFactor: boolean): void {
expect([slug, (await call('POST', `/${slug}`, editor, { email: `by-editor@${slug}.example.com`, password: PASSWORD, roles: ['editor'] })).status]).toEqual([slug, 403])
}
}
// ── K-1: single-valued `roles` — as on 1.16.7. The value grants nothing, so nothing is closed. ──
for (const { slug, low, top } of SINGLE.filter((shape) => !process.env.IPAL_TEST_SHAPE || shape.slug === process.env.IPAL_TEST_SHAPE)) {
const boss = await make(slug, `top@${slug}.example.com`, top)
const mine = await make(slug, `low@${slug}.example.com`, low)
const victim = await make(slug, `victim@${slug}.example.com`, low)
const me = (await login(slug, `low@${slug}.example.com`))!
const holder = (await login(slug, `top@${slug}.example.com`))!
expect([slug, {
// an account that holds 'admin' in a single-valued field is not a plugin admin: it opens nothing
adminValueReaches: [(await call('GET', '/globals/site-integrations', holder)).status, (await call('PATCH', `/agents/${agent.id}`, holder, { roles: ['admin'] })).status, (await call('GET', '/users', holder)).status],
// the collection's accounts manage each other, the lowest and the one holding 'admin' alike
create: [(await call('POST', `/${slug}`, me, { email: `by-low@${slug}.example.com`, password: PASSWORD, roles: top })).status, (await call('POST', `/${slug}`, holder, { email: `by-top@${slug}.example.com`, password: PASSWORD, roles: low })).status],
patchOther: [(await call('PATCH', `/${slug}/${victim.id}`, me, { password: 'another-long-password-1' })).status, (await call('PATCH', `/${slug}/${victim.id}`, holder, { password: 'another-long-password-2' })).status],
zDelete: (await call('DELETE', `/${slug}/${victim.id}`, holder)).status,
}]).toEqual([slug, { adminValueReaches: [403, 403, 403], create: [201, 201], patchOther: [200, 200], zDelete: 200 }])
// an account of ANOTHER collection still gets nothing there
const outsider = (await login('customers', '[email protected]'))!
expect([slug, (await call('POST', `/${slug}`, outsider, { email: `by-visitor@${slug}.example.com`, password: PASSWORD, roles: top })).status, (await call('PATCH', `/${slug}/${boss.id}`, outsider, { password: 'taken-over-password-1' })).status, (await call('GET', `/${slug}`, outsider)).status]).toEqual([slug, 403, 403, 403])
expect(mine.id).toBeDefined()
}
// role values with neither `editor` nor `admin`: as before — accounts of the collection manage it, never their own roles
await make('team', '[email protected]', ['member'])
const member = (await login('team', '[email protected]'))!