1.17.2 (owner's Q1-Q3): form submissions over REST for staff only; Payload's job queue for an admin or Bearer CRON_SECRET; doctor visitor-writes; explicit write rules on the reference site
- 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]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
fc70a34f3e
commit
4e68b7e44a
@@ -0,0 +1,78 @@
|
||||
import type { Config, PayloadRequest } from 'payload'
|
||||
|
||||
import { createHash, timingSafeEqual } from 'node:crypto'
|
||||
|
||||
import { userMeetsRole } from '../access/predicates.js'
|
||||
|
||||
/**
|
||||
* Who may run Payload's job queue over HTTP (`GET /api/payload-jobs/run`, `/api/payload-jobs/handle-schedules`,
|
||||
* both checked by `jobs.access.run`). Payload's own default is any signed-in account of ANY auth collection — a
|
||||
* customer of a shop included (IAWL idea 35: a route open to the web is a visitor, not the system). Since 1.17.2,
|
||||
* when the project writes no `jobs.access.run`, iapl sets: an admin signed in with the second factor (an admin's API
|
||||
* key counts), or `Authorization: Bearer <CRON_SECRET>`. A project's own rule always wins; `jobsRunAccess: false`
|
||||
* keeps Payload's default. Jobs run in-process (`jobs.autoRun`, `payload jobs:run`, `payload.jobs.run()`) never
|
||||
* pass through this rule.
|
||||
*/
|
||||
export type JobsRunAccessOptions = {
|
||||
/** The variable holding the cron secret. Default `CRON_SECRET`. Shorter than 16 characters = not used. */
|
||||
secretVar?: string
|
||||
}
|
||||
|
||||
export const CRON_SECRET_ENV = 'CRON_SECRET'
|
||||
/** The secret's minimum length: a short one would be guessable over the run route. */
|
||||
export const CRON_SECRET_MIN_LENGTH = 16
|
||||
|
||||
const sha = (value: string) => createHash('sha256').update(value).digest()
|
||||
|
||||
/** True when the request carries `Authorization: Bearer <secret>` (constant-time; an unset or short secret never matches). */
|
||||
export function cronSecretMatches(req: Pick<PayloadRequest, 'headers'>, secret: string | undefined): boolean {
|
||||
const expected = secret?.trim() ?? ''
|
||||
if (expected.length < CRON_SECRET_MIN_LENGTH) {return false}
|
||||
const header = req.headers?.get?.('authorization') ?? ''
|
||||
if (!/^Bearer\s+/i.test(header)) {return false}
|
||||
return timingSafeEqual(sha(header.replace(/^Bearer\s+/i, '').trim()), sha(expected))
|
||||
}
|
||||
|
||||
/**
|
||||
* The rule iapl puts on `jobs.access.run` (exported for a project that writes its own and wants to keep this part):
|
||||
* jobs: { access: { run: (args) => jobsRunAccess()(args) || myOwnCheck(args) } }
|
||||
*/
|
||||
export function jobsRunAccess({ secretVar = CRON_SECRET_ENV }: JobsRunAccessOptions = {}) {
|
||||
return ({ req }: { req: PayloadRequest }): boolean =>
|
||||
cronSecretMatches(req, process.env[secretVar]) || userMeetsRole(req, 'admin')
|
||||
}
|
||||
|
||||
/**
|
||||
* The config with iapl's `jobs.access.run`, only when the project wrote none (checked before Payload fills its
|
||||
* default in, so a project's rule is never mistaken for Payload's). Also when the config has no `jobs` at all:
|
||||
* Payload turns the queue on by itself for collections with scheduled publishing. Marks the config
|
||||
* (`custom.iapl.jobsRunDefault`) so the start-up note knows the rule is iapl's.
|
||||
*/
|
||||
export function withJobsRunAccess(config: Config, options: false | JobsRunAccessOptions = {}): Config {
|
||||
if (options === false || config.jobs?.access?.run) {return config}
|
||||
const custom = (config.custom ?? {}) as Record<string, unknown>
|
||||
const iapl = (custom.iapl ?? {}) as Record<string, unknown>
|
||||
return {
|
||||
...config,
|
||||
custom: { ...custom, iapl: { ...iapl, jobsRunDefault: { secretVar: options.secretVar ?? CRON_SECRET_ENV } } },
|
||||
jobs: { ...config.jobs, access: { ...config.jobs?.access, run: jobsRunAccess(options) } } as Config['jobs'],
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The one line said at start when the queue is on and the rule is iapl's: how a cron reaches the run route.
|
||||
* Null when there is nothing to say.
|
||||
*/
|
||||
export function jobsRunNotice(config: { custom?: unknown; jobs?: { enabled?: boolean } }): null | string {
|
||||
const own = ((config.custom as { iapl?: { jobsRunDefault?: { secretVar?: string } } } | undefined)?.iapl)?.jobsRunDefault
|
||||
if (!own || !config.jobs?.enabled) {return null}
|
||||
const secretVar = own.secretVar ?? CRON_SECRET_ENV
|
||||
const secret = process.env[secretVar]?.trim() ?? ''
|
||||
if (secret.length >= CRON_SECRET_MIN_LENGTH) {return null}
|
||||
return (
|
||||
`[iapl] Payload's job queue runs over HTTP (/api/payload-jobs/run, /handle-schedules) only for an admin signed in with the second factor ` +
|
||||
`or with "Authorization: Bearer <${secretVar}>" (1.17.2), and ${secretVar} is ${secret ? `shorter than ${CRON_SECRET_MIN_LENGTH} characters` : 'not set'}. ` +
|
||||
`A cron that calls the run route: set ${secretVar} and send the header. In-process runs (jobs.autoRun, payload jobs:run) are not affected. ` +
|
||||
'Your own jobs.access.run wins; iapl({ jobsRunAccess: false }) keeps Payload\'s default (docs/security.md "Payload\'s job queue").'
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user