- Q3: the integration status (emailStatus, turnstileStatus) is written alone through the database adapter (writeGlobalFields, exported): a visitor's form submit no longer puts back an admin's SMTP or Turnstile change saved at the same moment. addToCookieList reads the list again right before writing and works the change out again when an editor's save changed it (up to 5 times). Both races reproduced exactly in test/statusWrites.test.ts, on the three adapters. - Q4: cookieScanner.access and the doctor's access go through accessAllowed: any truthy answer still allows, an answer that is not a boolean is reported once per option; strict (only true) is announced for 1.19.0. - Q5: related documents that are drafts are left out of what the public helpers answer (resolveRoute, getSettings, getArchiveEntries, the 404 page, llms-full; Markdown and MCP through resolveRoute): null alone, removed from a list, value null in a polymorphic one. A preview keeps them; draftRelations: 'keep' keeps the old behaviour. withoutDraftRelations, applyDraftRelations exported. - Docs: content.md, frontend.md, integrations.md, cookie-scanner.md, security.md, AGENT.md; iapl-nav and iapl-doctor skills. Co-Authored-By: Claude Opus 5.5 <[email protected]>
59 lines
2.8 KiB
TypeScript
59 lines
2.8 KiB
TypeScript
import type { Endpoint, PayloadRequest } from 'payload'
|
|
|
|
import type { CookieIntegration, CookieScanContext, CookieScannerOptions } from './types.js'
|
|
|
|
import { userMeetsRole } from '../../access/predicates.js'
|
|
import { secondFactorPassed } from '../../security/totp/secondFactor.js'
|
|
import { accessAllowed } from '../../shared/accessAnswer.js'
|
|
import { DEFAULT_COOKIE_INTEGRATIONS, integrationLabel } from './integrations.js'
|
|
import { cookieScanContext, enabledCookieIntegrations, parseCookieNames, runCookieScan } from './scan.js'
|
|
|
|
export const COOKIE_SCAN_PATH = '/ipal/cookie-scan'
|
|
|
|
/**
|
|
* GET /api/ipal/cookie-scan → `{ integrations: [{ key, label }] }` — what a scan would cover.
|
|
* POST /api/ipal/cookie-scan, body `{ names?: string[] | string }` → runs a scan,
|
|
* `{ ok, summary: { created, updated, unchanged, unknown }, integrations }`.
|
|
* Editors and up by default (`cookieScanner.access`).
|
|
*/
|
|
export function buildCookieScanEndpoints(options: CookieScannerOptions, plugin: CookieScanContext['plugin']): Endpoint[] {
|
|
const allowed = async (req: PayloadRequest) =>
|
|
options.access ? secondFactorPassed(req) && accessAllowed('cookieScanner.access', await options.access({ req })) : userMeetsRole(req, 'editor')
|
|
const integrations = options.integrations ? options.integrations(DEFAULT_COOKIE_INTEGRATIONS) : DEFAULT_COOKIE_INTEGRATIONS
|
|
// Names in the admin language of the editor who asks.
|
|
const list = (items: CookieIntegration[], req: PayloadRequest) =>
|
|
items.map((i) => ({ key: i.key, label: integrationLabel(i, req.i18n?.language) }))
|
|
|
|
return [
|
|
{
|
|
handler: async (req) => {
|
|
if (!(await allowed(req))) {return Response.json({ ok: false }, { status: 403 })}
|
|
const enabled = await enabledCookieIntegrations(integrations, await cookieScanContext(req.payload, plugin, req))
|
|
return Response.json({ integrations: list(enabled, req), ok: true })
|
|
},
|
|
method: 'get',
|
|
path: COOKIE_SCAN_PATH,
|
|
},
|
|
{
|
|
handler: async (req) => {
|
|
if (!(await allowed(req))) {return Response.json({ ok: false }, { status: 403 })}
|
|
let body: { names?: unknown } = {}
|
|
try {
|
|
body = ((await (req as unknown as Request).json()) ?? {}) as { names?: unknown }
|
|
} catch {
|
|
// empty body = scan without extra names
|
|
}
|
|
try {
|
|
const { enabled, summary } = await runCookieScan({ names: parseCookieNames(body.names), options, payload: req.payload, plugin, req })
|
|
return Response.json({ integrations: list(enabled, req), ok: true, summary })
|
|
} catch (err) {
|
|
req.payload.logger.error(`[iapl] cookie scan failed: ${err instanceof Error ? err.message : String(err)}`)
|
|
return Response.json({ ok: false }, { status: 500 })
|
|
}
|
|
},
|
|
method: 'post',
|
|
path: COOKIE_SCAN_PATH,
|
|
},
|
|
]
|
|
}
|