Consent compliance: Global Privacy Control honoured, equal choice by default, withdrawal that stops running tools, consent log on by default

- GPC (gpc.ts): read in the browser before the first paint (consentBannerScript) and in the consent store; pages stay
  static. 'honour' (default): no banner, analytics and marketing off, no request to Google or a pixel; an explicit
  choice on the site wins; a grant made without the signal gives way when it appears. 'ask' and false as opt-outs.
- equalChoice defaults to true; a development warning when the banner has no 'reject'.
- Withdrawal: Meta / TikTok / Clarity told at once, listed localStorage / sessionStorage keys removed.
- Clarity gets consentv2 when it loads; ads_data_redaction with the Consent Mode default.
- consentLog on by default (consentLog: false turns it off); SiteProviders posts to it by itself; a missing SQL table
  answers 503 with one log line.
- Default banner text: one sentence on changing the choice and browser settings; the GPC line (PL / EN).
- Doctor: consent-log-migration, consent-banner rules.
- Stats: Sec-GPC over a choice made without it keeps the visitor id out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 13:38:39 +01:00
co-authored by Claude Opus 5.5
parent 41771fd51a
commit 99036eb70c
31 changed files with 1130 additions and 120 deletions
+27 -5
View File
@@ -15,12 +15,22 @@ import { CONSENT_CATEGORY_LABELS } from './categoryLabels.js'
* the time. NO IP address, user agent or other personal data — the id alone
* links a record to the browser that holds the cookie.
*
* Opt-in: `iapl({ consentLog: true })` adds the collection and the endpoint
* POST /api/ipal/consent; the layout passes `<ConsentProvider logEndpoint="/api/ipal/consent">`.
* Records older than `retentionDays` (default 3 years) are purged automatically.
* On by default since 1.17.0 (`iapl({ consentLog: false })` turns it off): adds the collection and the endpoint
* POST /api/ipal/consent. <SiteProviders> posts there by itself (the site shell knows the path); a layout with its
* own <ConsentProvider> passes `logEndpoint="/api/ipal/consent"`. Postgres / SQLite: the collection is a table, so
* the deploy that brings it needs a migration — until it exists a write answers 503 and logs one line; the page and
* the banner never notice. Records older than `retentionDays` (default 3 years) are purged automatically.
*/
export const CONSENT_LOG_SLUG = 'consent-records'
/** The endpoint's path under the API route (`/api` + this). */
export const CONSENT_LOG_PATH = '/ipal/consent'
/** A database error that means the collection's table is not there (a SQL project without the migration). */
export function isMissingTableError(err: unknown): boolean {
const text = err instanceof Error ? `${err.message} ${(err as { cause?: { message?: string } }).cause?.message ?? ''}` : String(err)
return /relation "?[\w.]*consent_records"? does not exist|no such table:? *"?[\w.]*consent_records|consent_records.*does(?: not|n't) exist/i.test(text)
}
export type ConsentLogOptions = {
/**
@@ -72,6 +82,8 @@ export function buildConsentLogCollection(): CollectionConfig {
}
const purge = createDailyPurge(CONSENT_LOG_SLUG)
/** The missing-table line is logged once per process, not on every visitor's click. */
let missingTableLogged = false
export function buildConsentLogEndpoint({ retentionDays = 1095 }: ConsentLogOptions = {}): Endpoint {
return {
@@ -85,7 +97,8 @@ export function buildConsentLogEndpoint({ retentionDays = 1095 }: ConsentLogOpti
} catch {
return Response.json({ ok: false }, { status: 400 })
}
const b = body as { id?: unknown; state?: Record<string, unknown>; version?: unknown }
// `gpc` (Global Privacy Control at the time of the choice) may come with the body; the record does not store it.
const b = body as { gpc?: unknown; id?: unknown; state?: Record<string, unknown>; version?: unknown }
const valid =
typeof b.id === 'string' &&
/^[0-9a-f]{16,64}$/.test(b.id) &&
@@ -110,11 +123,20 @@ export function buildConsentLogEndpoint({ retentionDays = 1095 }: ConsentLogOpti
purge(req.payload, { createdAt: { less_than: new Date(Date.now() - retentionDays * 86_400_000).toISOString() } })
return Response.json({ ok: true }, { status: 201 })
} catch (err) {
if (isMissingTableError(err)) {
if (!missingTableLogged) {
missingTableLogged = true
req.payload.logger.error(
"[iapl] consent log: the consent-records table does not exist — create and apply a migration (pnpm payload migrate:create consent-log, commit, migrate at container start) or set iapl({ consentLog: false }). Banner choices are not recorded until then; the site works.",
)
}
return Response.json({ ok: false }, { status: 503 })
}
req.payload.logger.error(`[iapl] consent log write failed: ${err instanceof Error ? err.message : String(err)}`)
return Response.json({ ok: false }, { status: 500 })
}
},
method: 'post',
path: '/ipal/consent',
path: CONSENT_LOG_PATH,
}
}