Staff check (userMeetsRole): the 'no roles field = every account passes' rule applied to the account's own collection, so an account of a second auth collection (customers) passed every staff check. It now applies only to staff collections: the panel's login collection plus the new access.staffCollections. The injected auth collection uses the new adminOrSelfIn(slug), so an account of another collection with the same numeric id is not 'self'. 2FA: @clocklimited/payload-2fa wraps the collections and globals present when it runs; ipalKit's own globals and collections were added later and stayed unwrapped, so a password without the TOTP code opened them over REST and GraphQL. ipal-kit now has its own second-factor check (secondFactorPassed / requireSecondFactor): applied to every collection and global at the end of ipalKit, again in onInit for entities added by plugins listed after it, and inside userMeetsRole and the endpoints that take a project access function. It fails closed: an unrecognised user shape is refused, and startup stops when the injected 2FA plugin lacks the parts the check reads (twoFactor.guard: false opts out). Tests through real HTTP requests (REST and GraphQL) against Payload's route handlers, plus the reference smoke test. Docs, agent guide, skills and the reference skills copy updated; changelog entry without a version. Co-Authored-By: Claude Fable 5.2 <[email protected]>
80 lines
3.2 KiB
TypeScript
80 lines
3.2 KiB
TypeScript
/**
|
|
* The documented non-interactive paths keep working with 2FA on (real HTTP, see securityHttp.test.ts):
|
|
* an API key of a staff account, and the bearer token of GET /api/ipal/doctor.
|
|
*/
|
|
import type { Payload } from 'payload'
|
|
|
|
import { payloadTotp } from '@clocklimited/payload-2fa'
|
|
import { sqliteAdapter } from '@payloadcms/db-sqlite'
|
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
import os from 'os'
|
|
import path from 'path'
|
|
import { getPayload } from 'payload'
|
|
import { afterAll, beforeAll, expect, test, vi } from 'vitest'
|
|
|
|
import { ipalKit } from '../src/plugin.js'
|
|
import { buildTestConfig } from './helpers.js'
|
|
import { startPayloadServer, type TestServer } from './http/server.js'
|
|
|
|
vi.mock('server-only', () => ({}))
|
|
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
|
|
|
|
const API_KEY = 'b7f0c3a2-5d1e-4f6a-9c8b-0e1d2f3a4b5c'
|
|
const DOCTOR_TOKEN = 'doctor-token-0123456789-0123456789-abcdef'
|
|
let server: TestServer
|
|
let payload: Payload
|
|
|
|
beforeAll(async () => {
|
|
process.env.IPAL_DOCTOR_TOKEN = DOCTOR_TOKEN
|
|
const config = buildTestConfig({
|
|
collections: [
|
|
// The project's own staff collection, with API keys.
|
|
{ slug: 'users', auth: { useAPIKey: true }, fields: [] },
|
|
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
|
|
],
|
|
db: sqliteAdapter({ client: { url: 'file::memory:' }, push: true }),
|
|
editor: lexicalEditor(),
|
|
plugins: [
|
|
ipalKit({
|
|
access: { authCollection: 'users' },
|
|
forms: {},
|
|
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
|
|
twoFactor: { issuer: 'Example', plugin: payloadTotp },
|
|
}),
|
|
],
|
|
secret: 'integration-test-secret-0123456789',
|
|
telemetry: false,
|
|
})
|
|
payload = await getPayload({ config })
|
|
server = await startPayloadServer(config)
|
|
await payload.create({
|
|
collection: 'users' as never,
|
|
data: { apiKey: API_KEY, email: '[email protected]', enableAPIKey: true, password: 'a-long-test-password-1', roles: ['admin'], totpSecret: 'JBSWY3DPEHPK3PXP' } as never,
|
|
})
|
|
}, 120_000)
|
|
|
|
afterAll(async () => {
|
|
delete process.env.IPAL_DOCTOR_TOKEN
|
|
await server?.close()
|
|
await payload?.destroy?.()
|
|
})
|
|
|
|
const get = (pathname: string, authorization?: string) =>
|
|
fetch(`${server.url}/api${pathname}`, { headers: authorization ? { authorization } : {} })
|
|
|
|
test('an API key of an admin account reads Site integrations and form submissions without a TOTP code', async () => {
|
|
const key = `users API-Key ${API_KEY}`
|
|
expect((await get('/globals/site-integrations', key)).status).toBe(200)
|
|
expect((await get('/form-submissions', key)).status).toBe(200)
|
|
expect((await get('/users', key)).status).toBe(200)
|
|
expect((await get('/globals/site-integrations', 'users API-Key wrong-key')).status).toBe(403)
|
|
})
|
|
|
|
test('GET /api/ipal/doctor answers its bearer token, and nobody else', async () => {
|
|
const ok = await get('/ipal/doctor', `Bearer ${DOCTOR_TOKEN}`)
|
|
expect(ok.status).toBe(200)
|
|
expect(await ok.json()).toMatchObject({ usersWithout2fa: 0 })
|
|
expect((await get('/ipal/doctor')).status).toBe(401)
|
|
expect((await get('/ipal/doctor', 'Bearer not-the-token-0123456789-0123456789')).status).toBe(401)
|
|
})
|