113 lines
5.6 KiB
TypeScript
113 lines
5.6 KiB
TypeScript
/**
|
|
* Lists from outside (1.18.0; security audit 2026-12, findings F4 and F5) and Payload's document locks for staff only
|
|
* (the "document locks" door), over real HTTP: Payload's REST and GraphQL handlers behind a server, a visitor login
|
|
* collection next to the staff one.
|
|
*/
|
|
import type { Payload } from 'payload'
|
|
|
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
import os from 'os'
|
|
import path from 'path'
|
|
import { getPayload } from 'payload'
|
|
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
|
|
|
|
import { iapl } from '../src/plugin.js'
|
|
import { buildTestConfig } from './helpers.js'
|
|
import { testDatabase } from './http/db.js'
|
|
import { cookiesOf, startPayloadServer, type TestServer } from './http/server.js'
|
|
|
|
vi.mock('server-only', () => ({}))
|
|
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
|
|
|
|
const PASSWORD = 'a-long-test-password-1'
|
|
let server: TestServer
|
|
let payload: Payload
|
|
let base = ''
|
|
let editor = ''
|
|
let customer = ''
|
|
|
|
const signIn = async (collection: string, email: string) => {
|
|
const res = await fetch(`${base}/api/${collection}/login`, { body: JSON.stringify({ email, password: PASSWORD }), headers: { 'content-type': 'application/json', origin: base }, method: 'POST' })
|
|
expect(res.status).toBe(200)
|
|
return cookiesOf(res).join('; ')
|
|
}
|
|
const get = async (path: string, cookie = '') => {
|
|
const res = await fetch(`${base}${path}`, { headers: { cookie, origin: base } })
|
|
return { body: (await res.json().catch(() => ({}))) as { docs?: unknown[]; limit?: number }, status: res.status }
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
const config = buildTestConfig({
|
|
admin: { user: 'users' },
|
|
collections: [
|
|
{ slug: 'notes', access: { read: () => true }, fields: [{ name: 'title', type: 'text' }] },
|
|
{ slug: 'customers', access: { create: () => false }, auth: true, fields: [] },
|
|
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
|
|
],
|
|
db: testDatabase('query_guard'),
|
|
editor: lexicalEditor(),
|
|
plugins: [iapl({ access: { authCollection: 'users' }, i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }, twoFactor: false })],
|
|
secret: 'integration-test-secret-0123456789',
|
|
telemetry: false,
|
|
})
|
|
payload = await getPayload({ config })
|
|
server = await startPayloadServer(config)
|
|
base = server.url
|
|
for (let i = 0; i < 130; i++) {await payload.create({ collection: 'notes' as never, data: { title: `n${i}` } as never })}
|
|
await payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['editor'] } as never })
|
|
await payload.create({ collection: 'customers' as never, data: { email: '[email protected]', password: PASSWORD } as never, overrideAccess: true })
|
|
editor = await signIn('users', '[email protected]')
|
|
customer = await signIn('customers', '[email protected]')
|
|
}, 180_000)
|
|
|
|
afterAll(async () => {
|
|
await server?.close()
|
|
await payload?.destroy?.()
|
|
})
|
|
|
|
describe('F4: public lists are capped for callers that are not staff', () => {
|
|
test('anonymous and a customer get at most 100; pagination=false and limit=0 too', async () => {
|
|
for (const cookie of ['', customer]) {
|
|
for (const q of ['limit=100000', 'pagination=false', 'limit=0']) {
|
|
const { body, status } = await get(`/api/notes?${q}&depth=0`, cookie)
|
|
expect([q, status, body.docs?.length]).toEqual([q, 200, 100])
|
|
}
|
|
}
|
|
expect((await get('/api/notes?limit=5')).body.docs).toHaveLength(5)
|
|
})
|
|
|
|
test('staff and the Local API are not capped', async () => {
|
|
expect((await get('/api/notes?limit=1000&depth=0', editor)).body.docs).toHaveLength(130)
|
|
expect((await payload.find({ collection: 'notes' as never, limit: 1000 })).docs).toHaveLength(130)
|
|
})
|
|
|
|
test('GraphQL lists from outside are capped as well', async () => {
|
|
const res = await fetch(`${base}/api/graphql`, { body: JSON.stringify({ query: '{ Notes(limit: 100000) { docs { id } } }' }), headers: { 'content-type': 'application/json', origin: base }, method: 'POST' })
|
|
const body = (await res.json()) as { data?: { Notes?: { docs: unknown[] } } }
|
|
expect(body.data?.Notes?.docs).toHaveLength(100)
|
|
})
|
|
})
|
|
|
|
describe('F5: an operator name in ?sort= is refused', () => {
|
|
test('$where, -$where, $natural and a.$ne answer 400 — anonymous and staff alike; a field name still sorts', async () => {
|
|
for (const cookie of ['', editor]) {
|
|
for (const sort of ['%24where', '-%24where', '%24natural', 'title.%24ne']) {
|
|
expect([sort, (await get(`/api/notes?sort=${sort}&limit=1`, cookie)).status]).toEqual([sort, 400])
|
|
}
|
|
}
|
|
expect((await get('/api/notes?sort=-title&limit=1')).status).toBe(200)
|
|
})
|
|
})
|
|
|
|
describe('Payload\'s document locks are for staff only', () => {
|
|
test('a customer is refused on every operation; an editor works as before', async () => {
|
|
const page = (await payload.find({ collection: 'notes' as never, limit: 1 })).docs[0] as { id: number | string }
|
|
const lock = { document: { relationTo: 'notes', value: page.id }, user: { relationTo: 'customers', value: 1 } }
|
|
const asCustomer = { 'content-type': 'application/json', cookie: customer, origin: base }
|
|
expect((await fetch(`${base}/api/payload-locked-documents`, { headers: asCustomer })).status).toBe(403)
|
|
expect((await fetch(`${base}/api/payload-locked-documents`, { body: JSON.stringify(lock), headers: asCustomer, method: 'POST' })).status).toBe(403)
|
|
expect((await fetch(`${base}/api/payload-locked-documents`, { headers: { cookie: '', origin: base } })).status).toBe(403)
|
|
expect((await fetch(`${base}/api/payload-locked-documents`, { headers: { cookie: editor, origin: base } })).status).toBe(200)
|
|
})
|
|
})
|