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 `. 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 ` (constant-time; an unset or short secret never matches). */ export function cronSecretMatches(req: Pick, 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 const iapl = (custom.iapl ?? {}) as Record 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").' ) }