Merge claude/security-b-1172: the consent version only goes up

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 11:17:51 +01:00
co-authored by Claude Opus 5.5
9 changed files with 165 additions and 5 deletions
+37 -1
View File
@@ -1,8 +1,44 @@
import type { Field } from 'payload'
import type { Field, FieldHook, NumberField } from 'payload'
import { ValidationError } from 'payload'
import { iaplT } from '../../modules/admin-i18n/t.js'
import { rowLabel } from '../../modules/admin-ui/index.js'
import { consentCategoryOptions } from '../../modules/consent/categoryLabels.js'
/**
* Cookie settings → Consent version only goes up (1.17.2). A stored consent counts only under the version it was
* given for, so a LOWER number would make old consent cookies valid again — consent given under an older policy.
* A save through access control (the panel, REST, GraphQL) with a lower number is refused with a message; the
* Local API with `overrideAccess` (a script an admin runs) may still lower it. `iapl({ cookieSettings:
* { allowLowerPolicyVersion: true } })` leaves the field free, as before.
*/
export const policyVersionNeverDown: FieldHook = ({ global, originalDoc, overrideAccess, path, req, siblingData, value }) => {
if (overrideAccess === true) {return value}
const stored = Number((originalDoc as { policyVersion?: unknown } | undefined)?.policyVersion)
// what the save SENT: Payload passes the stored value for a field that was not sent
const sent = siblingData && 'policyVersion' in siblingData ? Number((siblingData as { policyVersion?: unknown }).policyVersion) : NaN
if (!Number.isFinite(stored) || !Number.isFinite(sent) || sent >= stored) {return value}
const vars = { stored }
const message = req?.t ? iaplT(req.t as never, 'policyVersionDown', vars) : `The consent version cannot go down: it is ${stored} now.`
throw new ValidationError({ errors: [{ message, path: path.join('.') }], global: global?.slug }, req?.t)
}
export type CookieSettingsFieldOptions = {
/** `true` = an editor may lower the consent version, as before 1.17.2. Default false. */
allowLowerPolicyVersion?: boolean
}
/** The Cookie settings fields, with the consent-version rule unless it is turned off. */
export function buildCookieSettingsFields({ allowLowerPolicyVersion = false }: CookieSettingsFieldOptions = {}): Field[] {
if (allowLowerPolicyVersion) {return cookieSettingsFields}
return cookieSettingsFields.map((field) =>
'name' in field && field.name === 'policyVersion'
? ({ ...field, hooks: { ...(field as NumberField).hooks, beforeValidate: [policyVersionNeverDown, ...((field as NumberField).hooks?.beforeValidate ?? [])] } } as Field)
: field,
)
}
const DEFAULT_HINT = { en: 'Leave empty to use the default.', pl: 'Zostaw puste, aby użyć domyślnej wartości.' }
const HIDDEN_HINT = { en: 'Leave empty to hide it.', pl: 'Zostaw puste, aby go nie pokazywać.' }
+3 -3
View File
@@ -1,14 +1,14 @@
import type { GlobalConfig } from 'payload'
import { userMeetsRole } from '../../modules/access/index.js'
import { cookieSettingsFields } from './fields.js'
import { buildCookieSettingsFields, type CookieSettingsFieldOptions } from './fields.js'
/**
* Builds the CookieSettings global ("Cookies" in the admin) — consent banner copy and the
* cookie list, managed by editors. With the cookie scanner it also carries its tabs.
* Public read (the banner needs it on the frontend for anonymous visitors).
*/
export function buildCookieSettings(): GlobalConfig {
export function buildCookieSettings(options: CookieSettingsFieldOptions = {}): GlobalConfig {
return {
slug: 'cookie-settings',
access: {
@@ -18,7 +18,7 @@ export function buildCookieSettings(): GlobalConfig {
admin: {
group: { en: 'Privacy', pl: 'Prywatność' },
},
fields: cookieSettingsFields,
fields: buildCookieSettingsFields(options),
label: { en: 'Cookies', pl: 'Cookies' },
}
}
+3
View File
@@ -1,3 +1,6 @@
export { buildCookieSettingsFields, policyVersionNeverDown } from './globals/CookieSettings/fields.js'
export type { CookieSettingsFieldOptions } from './globals/CookieSettings/fields.js'
export { buildCookieSettings } from './globals/CookieSettings/index.js'
export { lightboxNotificationFields } from './globals/Notifications/fields.js'
export { applyGlobalOverrides } from './globals/overrides.js'
export type { GlobalOverride, GlobalOverrides, IaplGlobalSlug, IpalGlobalSlug } from './globals/overrides.js'
+2
View File
@@ -9,6 +9,7 @@ export const consentTexts = defineTexts({
cookieIntegrationStats: 'Built-in statistics',
cookieIntegrationTheme: 'Theme choice',
cookieSourceProject: 'Project',
policyVersionDown: 'The consent version cannot go down: it is {{stored}} now. Raise it to ask every visitor again, or leave it as it is.',
},
pl: {
cookieIntegrationAdmin: 'Logowanie do panelu',
@@ -17,5 +18,6 @@ export const consentTexts = defineTexts({
cookieIntegrationStats: 'Wbudowane statystyki',
cookieIntegrationTheme: 'Wybór motywu',
cookieSourceProject: 'Projekt',
policyVersionDown: 'Wersji zgody nie można obniżyć: teraz wynosi {{stored}}. Podnieś ją, aby ponownie zapytać odwiedzających, albo zostaw bez zmian.',
},
})
+1 -1
View File
@@ -773,7 +773,7 @@ export const iapl = (options: IaplOptions): Plugin => {
reenterSecretOnChange: options.secrets?.reenterOnChange !== false,
writeOnlySecrets: options.secrets?.writeOnly !== false,
}),
withFillLocales('global', buildCookieSettings(), fillLocales['cookie-settings']),
withFillLocales('global', buildCookieSettings(options.cookieSettings), fillLocales['cookie-settings']),
buildNotifications({ lightbox: options.lightbox === true }),
]
// A global feeds every page's layout — revalidate the site on save.
+7
View File
@@ -129,6 +129,13 @@ export type IaplOptions = {
*/
cookieScanner?: boolean | CookieScannerOptions
/**
* Cookie settings (the global). `allowLowerPolicyVersion: true` lets an editor LOWER the consent version, as
* before 1.17.2 — a lower number makes consent given under an older policy valid again, so by default a save
* through access control with a lower number is refused (the Local API with `overrideAccess` may still).
*/
cookieSettings?: { allowLowerPolicyVersion?: boolean }
/**
* Forms — form-builder collections (forms, form-submissions) plus the
* callable submitForm (Turnstile + persistence + SMTP-from-panel email).