Access callbacks allow only on a literal true (owner's decision for 1.18.0), strictAccessAnswers: false for the old reading; the new 1.18.0 security options checked strictly at start; docs, skills, changelog
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d90825a396
commit
c6bfb621f5
@@ -7,7 +7,7 @@ import type { CookieScannerOptions, CookieStorage } from './types.js'
|
||||
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
import { secondFactorPassed } from '../../security/totp/secondFactor.js'
|
||||
import { accessAllowed } from '../../shared/accessAnswer.js'
|
||||
import { accessAllowed, strictAccessAnswers } from '../../shared/accessAnswer.js'
|
||||
import { CONSENT_CATEGORIES } from '../categories.js'
|
||||
import { COOKIE_REVIEW_SLUG } from './collection.js'
|
||||
import { textFor } from './merge.js'
|
||||
@@ -157,7 +157,7 @@ export async function approveCookies({
|
||||
export function buildCookieApprovalEndpoints(options: CookieScannerOptions): Endpoint[] {
|
||||
const approval = typeof options.approval === 'object' ? options.approval : {}
|
||||
const allowed = async (req: PayloadRequest) =>
|
||||
options.access ? secondFactorPassed(req) && accessAllowed('cookieScanner.access', await options.access({ req })) : userMeetsRole(req, 'editor')
|
||||
options.access ? secondFactorPassed(req) && accessAllowed('cookieScanner.access', await options.access({ req }), { strict: strictAccessAnswers(req) }) : userMeetsRole(req, 'editor')
|
||||
return [
|
||||
{
|
||||
handler: async (req) => {
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { CookieCrawlOptions, CookieScanContext, CookieScannerOptions } from
|
||||
|
||||
import { userMeetsRole } from '../../../access/predicates.js'
|
||||
import { secondFactorPassed } from '../../../security/totp/secondFactor.js'
|
||||
import { accessAllowed } from '../../../shared/accessAnswer.js'
|
||||
import { accessAllowed, strictAccessAnswers } from '../../../shared/accessAnswer.js'
|
||||
import {
|
||||
COOKIE_CRAWL_PATH,
|
||||
COOKIE_CRAWL_RESULTS_PATH,
|
||||
@@ -61,7 +61,7 @@ export function buildCookieCrawlEndpoints({
|
||||
}): Endpoint[] {
|
||||
const crawl: CookieCrawlOptions = typeof options.crawl === 'object' ? options.crawl : {}
|
||||
const isStaff = async (req: PayloadRequest) =>
|
||||
options.access ? secondFactorPassed(req) && accessAllowed('cookieScanner.access', await options.access({ req })) : userMeetsRole(req, 'editor')
|
||||
options.access ? secondFactorPassed(req) && accessAllowed('cookieScanner.access', await options.access({ req }), { strict: strictAccessAnswers(req) }) : userMeetsRole(req, 'editor')
|
||||
const read = async (req: PayloadRequest) =>
|
||||
(await req.payload.findGlobal({ slug: COOKIE_CRAWL_SLUG as never, depth: 0, overrideAccess: true, req })) as CrawlDoc
|
||||
const write = (req: PayloadRequest, data: Partial<CrawlDoc>) =>
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { CookieIntegration, CookieScanContext, CookieScannerOptions } from
|
||||
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
import { secondFactorPassed } from '../../security/totp/secondFactor.js'
|
||||
import { accessAllowed } from '../../shared/accessAnswer.js'
|
||||
import { accessAllowed, strictAccessAnswers } from '../../shared/accessAnswer.js'
|
||||
import { DEFAULT_COOKIE_INTEGRATIONS, integrationLabel } from './integrations.js'
|
||||
import { cookieScanContext, enabledCookieIntegrations, parseCookieNames, runCookieScan } from './scan.js'
|
||||
|
||||
@@ -18,7 +18,7 @@ export const COOKIE_SCAN_PATH = '/ipal/cookie-scan'
|
||||
*/
|
||||
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')
|
||||
options.access ? secondFactorPassed(req) && accessAllowed('cookieScanner.access', await options.access({ req }), { strict: strictAccessAnswers(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) =>
|
||||
|
||||
@@ -14,7 +14,7 @@ import { IMAGE_RULES } from '../performance/budgets.js'
|
||||
import { countPendingEnrolments, mailConfigured } from '../security/totp/enrolmentConfirm.js'
|
||||
import { secondFactorPassed, SETUP_FROM_FIELD } from '../security/totp/secondFactor.js'
|
||||
import { countAccountsWithoutTotp } from '../security/totp/setupRequired.js'
|
||||
import { accessAllowed } from '../shared/accessAnswer.js'
|
||||
import { accessAllowed, strictAccessAnswers } from '../shared/accessAnswer.js'
|
||||
import { readEnv } from '../shared/envName.js'
|
||||
import { clientIp, consumeRateLimit, tooManyRequests } from '../shared/rateLimit.js'
|
||||
import { retentionMap } from '../shared/retention.js'
|
||||
@@ -194,7 +194,7 @@ export function buildDoctorEndpoint(options: DoctorEndpointOptions = {}): Endpoi
|
||||
const limit = await consumeRateLimit({ key: clientIp(req.headers), max: 10, scope: 'iapl-doctor', windowMs: 60_000 })
|
||||
if (!limit.ok) {return tooManyRequests(limit.retryAfter, { error: 'Too many requests' })}
|
||||
const given = (req.headers.get('authorization') ?? '').replace(/^Bearer\s+/i, '').trim()
|
||||
const allowed = (given && sameToken(given, token)) || (options.access ? secondFactorPassed(req) && accessAllowed('doctor.access', await options.access({ req })) : false)
|
||||
const allowed = (given && sameToken(given, token)) || (options.access ? secondFactorPassed(req) && accessAllowed('doctor.access', await options.access({ req }), { strict: strictAccessAnswers(req) }) : false)
|
||||
if (!allowed) {return Response.json({ error: 'Unauthorized' }, { status: 401 })}
|
||||
const status = await collectDoctorStatus(req.payload)
|
||||
return Response.json(status, { headers: { 'cache-control': 'no-store' } })
|
||||
|
||||
@@ -1,30 +1,50 @@
|
||||
/**
|
||||
* The answer of a project's access callback (`cookieScanner.access`, the doctor's `access`), read the same way
|
||||
* everywhere (owner's decision Q4, 1.18.0).
|
||||
* everywhere (owner's decisions Q4 and "Tak, w 1.18.0").
|
||||
*
|
||||
* Today any truthy answer lets the request in, as before: `({ req }) => req.user` returns an account object, and
|
||||
* a function that returns `'no'` or `{}` lets the request in too. From the next minor version only `true` will
|
||||
* allow. Until then the first answer that is not a boolean is reported once per option in the server log, so a
|
||||
* project sees it before the change. (A callback needs a request to be asked, so this cannot be checked at start:
|
||||
* the warning comes with the first call.)
|
||||
* Since 1.18.0 only a literal `true` allows. `({ req }) => req.user` returns an account object and is now REFUSED;
|
||||
* so are `1`, `'yes'`, `{}` and a Promise that was not awaited. The first answer that is not a boolean is reported
|
||||
* once per option in the server log, naming the option and the fix. (A callback needs a request to be asked, so
|
||||
* this cannot be checked at start: the report comes with the first call.)
|
||||
*
|
||||
* A project that needs the old reading back sets `iapl({ strictAccessAnswers: false })`: any truthy answer allows
|
||||
* again (still reported once per option).
|
||||
*/
|
||||
const warned = new Set<string>()
|
||||
|
||||
export type AccessAnswerOptions = {
|
||||
/** Only a literal `true` allows (default). `false` = any truthy answer allows, as before 1.18.0. */
|
||||
strict?: boolean
|
||||
/** Where to report (default `console.warn`). */
|
||||
warn?: (message: string) => void
|
||||
}
|
||||
|
||||
/** True when the request may pass; warns once per `option` when the answer is not a boolean. */
|
||||
export function accessAllowed(option: string, answer: unknown, { warn = (m) => console.warn(m) }: AccessAnswerOptions = {}): boolean {
|
||||
const kindOf = (answer: unknown): string => {
|
||||
if (answer === null) {return 'null'}
|
||||
if (Array.isArray(answer)) {return 'an array'}
|
||||
if (answer && typeof (answer as { then?: unknown }).then === 'function') {return 'a Promise (not awaited)'}
|
||||
if (typeof answer === 'object') {return 'an object'}
|
||||
return /^[aeiou]/.test(typeof answer) ? `an ${typeof answer}` : `a ${typeof answer}`
|
||||
}
|
||||
|
||||
/** True when the request may pass; reports once per `option` when the answer is not a boolean. */
|
||||
export function accessAllowed(option: string, answer: unknown, { strict = true, warn = (m) => console.warn(m) }: AccessAnswerOptions = {}): boolean {
|
||||
const allowed = strict ? answer === true : Boolean(answer)
|
||||
if (typeof answer !== 'boolean' && !warned.has(option)) {
|
||||
warned.add(option)
|
||||
const kind = answer === null ? 'null' : Array.isArray(answer) ? 'an array' : typeof answer === 'object' ? 'an object' : `a ${typeof answer}`
|
||||
warn(
|
||||
`[iapl] ${option} returned ${kind}, not true or false. It is still read as ${answer ? 'allowed' : 'refused'}; from the next minor version only \`true\` allows. Return a boolean, e.g. \`({ req }) => Boolean(req.user)\` (docs/security.md "Access callbacks").`,
|
||||
`[iapl] ${option} returned ${kindOf(answer)}, not true or false, so the request was ${allowed ? 'allowed' : 'refused'}` +
|
||||
`${strict ? ' (only `true` allows since 1.18.0)' : ' (strictAccessAnswers: false reads any truthy answer as allowed)'}. ` +
|
||||
'Return a boolean, e.g. `({ req }) => Boolean(req.user)` (docs/security.md "Access callbacks").',
|
||||
)
|
||||
}
|
||||
return Boolean(answer)
|
||||
return allowed
|
||||
}
|
||||
|
||||
/** Whether the config behind this request reads access answers strictly (`strictAccessAnswers`, default true). */
|
||||
export function strictAccessAnswers(req: { payload?: { config?: { custom?: Record<string, unknown> } } } | undefined): boolean {
|
||||
const iapl = req?.payload?.config?.custom?.iapl as { strictAccessAnswers?: boolean } | undefined
|
||||
return iapl?.strictAccessAnswers !== false
|
||||
}
|
||||
|
||||
/** For tests: forget which options were reported. */
|
||||
|
||||
@@ -80,6 +80,19 @@ export function assertPluginSwitches(options: Record<string, unknown>): void {
|
||||
assertSwitchOrObject(name, options[name])
|
||||
}
|
||||
assertBooleanSwitch('indexNow', options.indexNow)
|
||||
// 1.18.0 (security audit): `listLimit: 0` / `'false'` must not read as off; a max below 1 caps every list to nothing
|
||||
assertSwitchOrObject('listLimit', options.listLimit, { allowTrue: false })
|
||||
const listLimit = bag(options.listLimit)
|
||||
if (listLimit) {
|
||||
assertNumberSwitch('listLimit.max', listLimit.max, { min: 1 })
|
||||
assertBooleanSwitch('listLimit.staff', listLimit.staff)
|
||||
if (listLimit.exclude !== undefined && !(Array.isArray(listLimit.exclude) && listLimit.exclude.every((c) => typeof c === 'string'))) {
|
||||
throw new Error(`${where('listLimit.exclude')} must be a list of collection slugs such as ['products'] (received ${JSON.stringify(listLimit.exclude)}).`)
|
||||
}
|
||||
}
|
||||
assertBooleanSwitch('refuseOperatorSort', options.refuseOperatorSort)
|
||||
assertSwitchOrObject('lockedDocuments', options.lockedDocuments, { allowTrue: false })
|
||||
assertBooleanSwitch('strictAccessAnswers', options.strictAccessAnswers)
|
||||
// 1.17.2: `adminSameOrigin: 0` / `'false'` must not read as off (it took the origin check off the panel actions)
|
||||
assertSwitchOrObject('adminSameOrigin', options.adminSameOrigin)
|
||||
const adminSameOrigin = bag(options.adminSameOrigin)
|
||||
|
||||
Reference in New Issue
Block a user