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
+39 -1
View File
@@ -19,7 +19,7 @@ import type { I18nConfig } from '../src/modules/i18n/types.js'
import { countBelowPanelRole, panelAccess, resolvePanelRole } from '../src/modules/access/panelRole.js'
import { checkPassword, resolvePasswordRule } from '../src/modules/access/passwordRule.js'
import { adoptLateAuthCollections, bothAllow, hasPluginRoles, openSignUpCollections, rolesByAdminOnly, rolesNotOwn, sameStaffCollectionAccess, staffCollectionsOnOwnRoles, staffCollectionsOpenToOthers, staffCreateAccess, staffReadAccess, staffUpdateAccess, unknownStaffCollections } from '../src/modules/access/selfRegistration.js'
import { adoptLateAuthCollections, bothAllow, canHoldStaffRole, hasPluginRoles, openSignUpCollections, rolesByAdminOnly, rolesNotOwn, sameStaffCollectionAccess, staffCollectionsNobodyManages, staffCollectionsOnOwnRoles, staffCollectionsOpenToOthers, staffCreateAccess, staffReadAccess, staffUpdateAccess, unknownStaffCollections } from '../src/modules/access/selfRegistration.js'
import { ipalKit } from '../src/plugin.js'
import { buildTestConfig, withTotpParts } from './helpers.js'
import { testDatabase } from './http/db.js'
@@ -565,6 +565,19 @@ export function accountRulesSuite(twoFactor: boolean): void {
expect(hasPluginRoles(roles([{ label: 'Admin', value: 'admin' }, { label: 'Editor', value: 'editor' }]))).toBe(true)
expect(hasPluginRoles(roles(['superadmin', 'writer']))).toBe(false)
expect(hasPluginRoles(roles(['admin', 'writer']))).toBe(false) // one foreign value: the project's own roles
// …but an account that CAN hold `editor` or `admin` is the plugin's business: the admin rules apply (review H-2, J-3)
expect(canHoldStaffRole(roles(['admin', 'writer']))).toBe(true)
expect(canHoldStaffRole(roles(['user', 'editor', 'admin', 'boss']))).toBe(true)
expect(canHoldStaffRole(roles(['viewer', 'editor']))).toBe(true)
expect(canHoldStaffRole(roles([{ label: 'Admin', value: 'admin' }, { label: 'Boss', value: 'boss' }]))).toBe(true)
expect(canHoldStaffRole(roles(['superadmin', 'writer']))).toBe(false)
expect(canHoldStaffRole(roles(['Admin', 'administrator', 'user']))).toBe(false) // the values themselves, nothing like them
expect(canHoldStaffRole({ slug: 'x', fields: [] })).toBe(false) // no roles field: nothing to hand out (every account is an admin)
// a field whose values cannot be listed can hold anything
expect(canHoldStaffRole({ slug: 'x', fields: [{ name: 'roles', type: 'text' }] } as never)).toBe(true)
expect(canHoldStaffRole({ slug: 'x', fields: [{ name: 'roles', type: 'json' }] } as never)).toBe(true)
expect(canHoldStaffRole(roles([]))).toBe(true)
expect(canHoldStaffRole({ slug: 'x', fields: [{ type: 'row', fields: [{ name: 'roles', type: 'text' }] }] } as never)).toBe(true)
expect(hasPluginRoles(roles([]))).toBe(false)
expect(hasPluginRoles({ slug: 'x', fields: [] })).toBe(false)
expect(hasPluginRoles({ slug: 'x', fields: [{ name: 'roles', type: 'text' }] } as never)).toBe(false)
@@ -731,6 +744,31 @@ export function accountRulesSuite(twoFactor: boolean): void {
expect(adminRule(await build({ access: { authCollection: 'users', firstUserAdmin: false }, accounts: { panelRole: false } }))).toBeUndefined()
})
test('staffCollectionsNobodyManages: the admin rules without any admin — for the start-up warning', async () => {
const roles = (options: string[]) => [{ name: 'roles', type: 'select', options }]
const ruled = { applied: ['create', 'update', 'delete'], ownRoles: false }
const collections = {
crew: { config: { slug: 'crew', auth: true, custom: { ipalAccountRules: ruled }, fields: roles(['user', 'admin', 'boss']) } },
team: { config: { slug: 'team', auth: true, custom: { ipalAccountRules: { applied: ['create'], ownRoles: true } }, fields: roles(['lead', 'member']) } },
users: { config: { slug: 'users', auth: true, custom: { ipalAccountRules: ruled }, fields: roles(['user', 'editor', 'admin']) } },
}
const withAdmins = (admins: Record<string, number>, fail = false) => ({
collections,
count: async ({ collection }: { collection: string }) => {
if (fail) {throw new Error('cannot query')}
return { totalDocs: admins[collection] ?? 0 }
},
})
// nobody is an admin anywhere: the collections on the admin rules, without the plugin's own (it has its own warning)
expect(await staffCollectionsNobodyManages(withAdmins({}) as never, ['users', 'crew', 'team'], 'users')).toEqual(['crew'])
// an admin in the collection, or in another staff collection, manages it
expect(await staffCollectionsNobodyManages(withAdmins({ crew: 1 }) as never, ['users', 'crew', 'team'], 'users')).toEqual([])
expect(await staffCollectionsNobodyManages(withAdmins({ users: 2 }) as never, ['users', 'crew', 'team'], 'users')).toEqual([])
// nothing on the admin rules; or the accounts cannot be counted: nothing is said
expect(await staffCollectionsNobodyManages(withAdmins({}) as never, ['users', 'team'], 'users')).toEqual([])
expect(await staffCollectionsNobodyManages(withAdmins({}, true) as never, ['users', 'crew'], 'users')).toEqual([])
})
test('openSignUpCollections: slugs of auth collections an anonymous request may create in', async () => {
const collections = {
customers: { config: { slug: 'customers', access: { create: () => true }, auth: true } },