- form-submissions create: a staff account with its second factor (formSubmissionOverrides.access.create keeps the way back) - jobsRunAccess (default on when the project writes no jobs.access.run): admin with the second factor or Bearer CRON_SECRET (16+); start-up note when the secret is missing; jobsRunAccess: false keeps Payload's default - iapl-doctor deep rule visitor-writes: entities a visitor login account may write, asked by behaviour - reference: Pages, Posts, CaseStudies, Media, Header, Footer, Company get adminOrEditor write rules - closed doors: submissions, pages, media, header, company closed for customers; the fixture's job queue refused without the secret, run with it - route snapshot reasons, docs, AGENT.md, skills, changelog draft 1.17.2 Co-Authored-By: Claude Opus 5.5 <[email protected]>
96 lines
4.5 KiB
TypeScript
96 lines
4.5 KiB
TypeScript
/**
|
|
* Payload's job queue over HTTP (1.17.2, IAWL idea 35, owner's Q2): when the project writes no `jobs.access.run`,
|
|
* only an admin signed in with the second factor, or `Authorization: Bearer <CRON_SECRET>`, may run it. Payload's
|
|
* default let any signed-in account of any auth collection run the queue — a customer included.
|
|
*/
|
|
import type { Config, SanitizedConfig } from 'payload'
|
|
|
|
import { sqliteAdapter } from '@payloadcms/db-sqlite'
|
|
import { afterEach, describe, expect, test, vi } from 'vitest'
|
|
|
|
import { jobsRunNotice, withJobsRunAccess } from '../src/modules/security/jobsAccess.js'
|
|
import { iapl } from '../src/plugin.js'
|
|
import { buildTestConfig } from './helpers.js'
|
|
|
|
vi.mock('server-only', () => ({}))
|
|
|
|
const SECRET = 'a-cron-secret-of-32-characters!!'
|
|
const i18n = { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }
|
|
|
|
const project = (jobs?: Config['jobs']): Config =>
|
|
({
|
|
collections: [{ slug: 'customers', auth: true, fields: [] }, { slug: 'media', fields: [], upload: true }],
|
|
db: sqliteAdapter({ client: { url: 'file::memory:' } }),
|
|
jobs: { ...jobs, tasks: [{ slug: 'noop', handler: () => ({ output: {} }) }] },
|
|
secret: 'jobs-access-test',
|
|
telemetry: false,
|
|
}) as unknown as Config
|
|
|
|
const build = (jobs?: Config['jobs'], options: object = {}) =>
|
|
buildTestConfig({ ...project(jobs), plugins: [iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false, ...options } as never)] })
|
|
|
|
/** Calls the sanitized run rule the way Payload's run endpoint does. */
|
|
const runAs = async (config: SanitizedConfig, user: unknown, authorization?: string) => {
|
|
const req = { headers: new Headers(authorization ? { authorization } : {}), payload: { config }, user }
|
|
return Boolean(await (config.jobs.access!.run as (a: { req: unknown }) => unknown)({ req }))
|
|
}
|
|
|
|
const admin = { id: 1, collection: 'users', roles: ['admin'] }
|
|
const editor = { id: 2, collection: 'users', roles: ['editor'] }
|
|
const customer = { id: 3, collection: 'customers' }
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs()
|
|
})
|
|
|
|
describe('jobs.access.run when the project writes none', () => {
|
|
test('an admin, or the cron secret; not an editor, a customer or nobody', async () => {
|
|
vi.stubEnv('CRON_SECRET', SECRET)
|
|
const config = await build()
|
|
expect(await runAs(config, admin)).toBe(true)
|
|
expect(await runAs(config, null, `Bearer ${SECRET}`)).toBe(true)
|
|
expect(await runAs(config, null)).toBe(false)
|
|
expect(await runAs(config, customer)).toBe(false)
|
|
expect(await runAs(config, editor)).toBe(false)
|
|
expect(await runAs(config, null, 'Bearer wrong-secret-wrong-secret')).toBe(false)
|
|
expect(await runAs(config, customer, SECRET)).toBe(false) // without "Bearer "
|
|
}, 30_000)
|
|
|
|
test('a missing or short secret never matches', async () => {
|
|
vi.stubEnv('CRON_SECRET', 'short')
|
|
const config = await build()
|
|
expect(await runAs(config, null, 'Bearer short')).toBe(false)
|
|
vi.stubEnv('CRON_SECRET', '')
|
|
expect(await runAs(config, null, 'Bearer ')).toBe(false)
|
|
}, 30_000)
|
|
|
|
test('secretVar names another variable', async () => {
|
|
vi.stubEnv('MY_JOBS_SECRET', SECRET)
|
|
const config = await build(undefined, { jobsRunAccess: { secretVar: 'MY_JOBS_SECRET' } })
|
|
expect(await runAs(config, null, `Bearer ${SECRET}`)).toBe(true)
|
|
}, 30_000)
|
|
|
|
test('the project\'s own rule wins; jobsRunAccess: false keeps Payload\'s default', async () => {
|
|
const own = await build({ access: { run: () => true } })
|
|
expect(await runAs(own, null)).toBe(true)
|
|
const payloadDefault = await build(undefined, { jobsRunAccess: false })
|
|
expect(await runAs(payloadDefault, customer)).toBe(true) // Payload: any signed-in account
|
|
}, 30_000)
|
|
|
|
test('a config without jobs gets the rule too (Payload turns the queue on for scheduled publishing)', () => {
|
|
const config = withJobsRunAccess({ secret: 'x' } as Config)
|
|
expect(typeof config.jobs?.access?.run).toBe('function')
|
|
expect(withJobsRunAccess({ secret: 'x' } as Config, false).jobs).toBeUndefined()
|
|
})
|
|
|
|
test('at start: a note when the queue is on, the rule is iapl\'s and the secret is missing', () => {
|
|
const custom = { iapl: { jobsRunDefault: { secretVar: 'CRON_SECRET' } } }
|
|
vi.stubEnv('CRON_SECRET', '')
|
|
expect(jobsRunNotice({ custom, jobs: { enabled: true } })).toMatch(/Authorization: Bearer <CRON_SECRET>.*not set/)
|
|
expect(jobsRunNotice({ custom, jobs: { enabled: false } })).toBeNull()
|
|
expect(jobsRunNotice({ custom: {}, jobs: { enabled: true } })).toBeNull()
|
|
vi.stubEnv('CRON_SECRET', SECRET)
|
|
expect(jobsRunNotice({ custom, jobs: { enabled: true } })).toBeNull()
|
|
})
|
|
})
|