Files
ipal-kit/test/accessControl.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 10da62ea4c Lock down injected users, 2FA slug, forms and globals writes
- Injected auth collection with roles: admins manage all accounts, others
  only themselves (Payload's default let any account edit or delete any other)
- 2FA defaults to the resolved auth collection instead of 'users'
- SiteSettings, CookieSettings, Notifications, Forms: write needs editor+;
  form Emails tab readable by editor+ only

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 16:58:00 +02:00

120 lines
4.8 KiB
TypeScript

import type { Access, CollectionConfig, Config, Field, GlobalConfig } from 'payload'
import { describe, expect, test, vi } from 'vitest'
import type { I18nConfig } from '../src/modules/i18n/types.js'
import { ipalKit } from '../src/plugin.js'
const i18n: I18nConfig = {
defaultLocale: 'pl',
locales: [
{ code: 'pl', label: 'Polski' },
{ code: 'en', label: 'English' },
],
}
const empty = (extra: Partial<Config> = {}): Config =>
({ collections: [], secret: 'x', ...extra }) as unknown as Config
const admin = { id: 1, roles: ['admin'] }
const editor = { id: 2, roles: ['editor'] }
const user = { id: 3, roles: ['user'] }
/** Runs a collection/global access function the way Payload does. */
const run = (fn: Access | undefined, u: unknown) =>
fn ? fn({ req: { user: u } } as never) : 'payload-default'
describe('injected auth collection', () => {
test('with roles: a plain user can only see and edit itself, never create or delete', async () => {
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty())
const access = config.collections?.find((c) => c.slug === 'users')?.access ?? {}
expect(run(access.update, user)).toEqual({ id: { equals: 3 } })
expect(run(access.read, user)).toEqual({ id: { equals: 3 } })
expect(run(access.delete, user)).toBe(false)
expect(run(access.create, user)).toBe(false)
expect(run(access.delete, editor)).toBe(false)
expect(run(access.update, null)).toBe(false)
expect(run(access.update, admin)).toBe(true)
expect(run(access.delete, admin)).toBe(true)
expect(run(access.create, admin)).toBe(true)
})
test("the project's own auth collection keeps its own access", async () => {
const own = { slug: 'users', access: { read: () => true }, auth: true, fields: [] }
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(
empty({ collections: [own] as CollectionConfig[] }),
)
expect(config.collections?.find((c) => c.slug === 'users')?.access).toBe(own.access)
})
})
describe('2FA wraps the auth collection people log in with', () => {
const totp = () => vi.fn((_: unknown) => (c: Config) => c)
test('defaults to access.authCollection, not a literal "users"', async () => {
const plugin = totp()
await ipalKit({ access: { authCollection: 'admins' }, i18n, twoFactor: { issuer: 'A', plugin: plugin as never } })(
empty(),
)
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'admins' }))
})
test('defaults to admin.user when access is not configured', async () => {
const plugin = totp()
await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: plugin as never } })(
empty({ admin: { user: 'staff' } } as Partial<Config>),
)
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'staff' }))
})
test('an explicit collectionSlug still wins', async () => {
const plugin = totp()
await ipalKit({
access: { authCollection: 'users' },
i18n,
twoFactor: { collectionSlug: 'admins', issuer: 'A', plugin: plugin as never },
})(empty())
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'admins' }))
})
})
describe('globals: public read, editor+ write', () => {
test.each(['site-settings', 'cookie-settings', 'notifications'])('%s', async (slug) => {
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty())
const access = (config.globals?.find((g) => g.slug === slug) as GlobalConfig).access ?? {}
expect(run(access.read, null)).toBe(true)
expect(run(access.update, user)).toBe(false)
expect(run(access.update, null)).toBe(false)
expect(run(access.update, editor)).toBe(true)
expect(run(access.update, admin)).toBe(true)
})
})
describe('forms collection: public read, editor+ write, Emails hidden from plain users', () => {
const build = async () => {
const config = await ipalKit({ access: { authCollection: 'users' }, forms: {}, i18n, twoFactor: false })(empty())
return config.collections?.find((c) => c.slug === 'forms') as CollectionConfig
}
test('create / update / delete need editor', async () => {
const { access = {} } = await build()
expect(run(access.read, null)).toBe(true)
for (const op of ['create', 'update', 'delete'] as const) {
expect(run(access[op], user)).toBe(false)
expect(run(access[op], editor)).toBe(true)
}
})
test('the Emails field (recipients, BCC) is unreadable for a plain user', async () => {
const forms = await build()
const emails = forms.fields.find((f: Field) => 'name' in f && f.name === 'emails') as {
access?: { read?: (a: unknown) => boolean }
}
expect(emails.access?.read?.({ req: { user } })).toBe(false)
expect(emails.access?.read?.({ req: { user: editor } })).toBe(true)
})
})