Consent: withdrawal clears tool cookies, policy version, consent log, cookie scan; form input hardening

Consent (GDPR)
- Withdrawing consent now deletes GA/Meta/Ads/Clarity/LinkedIn/TikTok/Hotjar
  cookies (prefix patterns) on the host AND parent domains — previously only
  the locale cookie was removed and _ga & co. stayed. Projects extend the list
  via <ConsentProvider cookieMap>, merged with the defaults.
- Cookie Settings → Policy version: raising it asks every visitor again.
- <CookieBanner equalChoice>: Reject styled like Accept (UODO/EDPB), opt-in.
- Opt-in consent log (ipalKit({ consentLog: true }) + logEndpoint): random id,
  choices, version, time — no IP/UA; admin-only collection, retention purge.
- Cookie Settings → Cookies + getCookieTable + unstyled <CookieTable> (/rsc).
- Consent cookie gets Secure on HTTPS; consent id stored with the decision.
- ipal-consent-check: cookie-scan.mjs (before/after consent, known-tool
  catalog PL/EN, flags non-necessary cookies set before consent) and
  import-cookies.ts into the panel (idempotent, keeps editor edits).

Forms
- Server-side type checks per field from the panel definition: email = one
  address (blocks "a@x, b@y" recipient injection into {{email}} confirmations
  and CR/LF header injection), select/radio only configured options, numbers,
  booleans; objects/arrays rejected; control and bidi characters stripped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-16 02:31:58 +02:00
co-authored by Claude Opus 5.5
parent c9a50abc33
commit ebf70d49bd
24 changed files with 905 additions and 46 deletions
+67
View File
@@ -0,0 +1,67 @@
import { describe, expect, test } from 'vitest'
import { validateSubmission } from '../src/modules/forms/validateSubmission.js'
import { fakePayload } from './helpers.js'
const forms = {
f: {
fields: [
{ name: 'name', blockType: 'text' },
{ name: 'email', blockType: 'email' },
{ name: 'topic', blockType: 'select', options: [{ value: 'offer' }, { value: 'support' }] },
{ name: 'count', blockType: 'number' },
{ name: 'message', blockType: 'textarea' },
{ name: 'agree', blockType: 'checkbox' },
],
},
}
const { payload } = fakePayload({ forms })
const check = (data: Record<string, unknown>) => validateSubmission(payload, 'f', data)
describe('form validation — injection and abuse', () => {
test.each([
['[email protected], [email protected]'],
['[email protected];[email protected]'],
['[email protected]\r\nBcc: [email protected]'],
['Name <[email protected]>'],
['not-an-email'],
[`${'a'.repeat(250)}@x.pl`],
])('email %j is rejected — no extra recipients or headers', async (email) => {
expect(await check({ email })).toMatchObject({ field: 'email', kind: 'invalid_format', ok: false })
})
test('a valid email is trimmed and kept', async () => {
expect(await check({ email: ' [email protected] ' })).toMatchObject({
cleaned: { email: '[email protected]' },
ok: true,
})
})
test('select accepts only values configured in the panel', async () => {
expect(await check({ topic: 'offer' })).toMatchObject({ ok: true })
expect(await check({ topic: '<script>alert(1)</script>' })).toMatchObject({ field: 'topic', kind: 'invalid_format' })
})
test('objects, arrays and wrong types are rejected', async () => {
expect(await check({ name: { $gt: '' } })).toMatchObject({ field: 'name', kind: 'invalid_format' })
expect(await check({ message: ['a', 'b'] })).toMatchObject({ field: 'message', kind: 'invalid_format' })
expect(await check({ count: '12abc' })).toMatchObject({ field: 'count', kind: 'invalid_format' })
expect(await check({ agree: 'true' })).toMatchObject({ field: 'agree', kind: 'invalid_format' })
})
test('control and bidi characters are stripped; single-line text loses line breaks', async () => {
const res = await check({ name: 'Jan\r\nKowalski\u0007', message: 'Dzień\u0000 dobry‮\nlinia 2' })
expect(res).toMatchObject({ cleaned: { name: 'Jan Kowalski', message: 'Dzień dobry\nlinia 2' }, ok: true })
})
test('HTML is stored as text (the renderer and e-mails escape it)', async () => {
expect(await check({ message: '<img src=x onerror=alert(1)>' })).toMatchObject({
cleaned: { message: '<img src=x onerror=alert(1)>' },
ok: true,
})
})
test('numbers are normalized', async () => {
expect(await check({ count: ' 42 ' })).toMatchObject({ cleaned: { count: 42 }, ok: true })
})
})
+45
View File
@@ -12,6 +12,8 @@ import path from 'path'
import { buildConfig, getPayload } from 'payload'
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
import { buildConsentLogEndpoint } from '../src/modules/consent/consentLog.js'
import { getCookieTable } from '../src/modules/consent/getConsentTexts.js'
import { submitForm } from '../src/modules/forms/submitForm.js'
import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js'
import { ipalKit } from '../src/plugin.js'
@@ -42,6 +44,7 @@ beforeAll(async () => {
plugins: [
ipalKit({
access: { authCollection: 'users' },
consentLog: true,
forms: {},
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
twoFactor: false,
@@ -174,3 +177,45 @@ describe('getPageJsonLd', () => {
expect(await h.getPageJsonLd('pl', ['nie-ma'])).toEqual([])
})
})
describe('consent log', () => {
const post = (body: unknown, ip = '10.6.0.1') =>
buildConsentLogEndpoint().handler({
headers: new Headers({ 'x-real-ip': ip }),
json: async () => body,
payload,
} as never) as Promise<Response>
const state = { analytics: true, functional: false, marketing: false, necessary: true }
test('stores id, choices and version only', async () => {
const res = await post({ id: 'a1b2c3d4e5f6a7b8c9d0e1f2', state, version: 2 })
expect(res.status).toBe(201)
const { docs } = await payload.find({ collection: 'consent-records' as never, where: { consentId: { equals: 'a1b2c3d4e5f6a7b8c9d0e1f2' } } })
const rec = docs[0] as unknown as Record<string, unknown>
expect(rec).toMatchObject({ choices: state, version: 2 })
expect(Object.keys(rec).filter((k) => /ip|agent|email/i.test(k))).toEqual([])
})
test('rejects malformed records', async () => {
for (const bad of [{}, { id: 'x', state, version: 1 }, { id: 'a1b2c3d4e5f6a7b8', state: { analytics: 'yes' }, version: 1 }, { id: 'a1b2c3d4e5f6a7b8', state, version: 0 }]) {
expect((await post(bad, '10.6.0.2')).status).toBe(400)
}
})
test('the collection is not writable or readable anonymously', async () => {
await expect(payload.find({ collection: 'consent-records' as never, overrideAccess: false })).rejects.toThrow()
})
})
describe('cookie table', () => {
test('rows from Cookie Settings in the requested language', async () => {
await payload.updateGlobal({
slug: 'cookie-settings' as never,
data: { cookies: [{ name: '_ga', category: 'analytics', duration: '2 lata', provider: 'Google', purpose: 'Statystyki' }, { name: ' ' }] } as never,
locale: 'pl' as never,
})
expect(await getCookieTable({ locale: 'pl', payload })).toEqual([
{ name: '_ga', category: 'analytics', duration: '2 lata', provider: 'Google', purpose: 'Statystyki' },
])
})
})
+11
View File
@@ -202,3 +202,14 @@ describe('buildCsp — googleMaps', () => {
expect(buildCsp().value).toContain('https://fonts.gstatic.com')
})
})
describe('consent storage — version and id', () => {
test('a consent from another policy version is ignored; id round-trips', async () => {
const { readConsentRecord, serializeConsent } = await import('../src/modules/consent/storage.js')
const state = { analytics: true, functional: true, marketing: false, necessary: true }
const raw = serializeConsent(state, 2, 'abc123')
expect(readConsentRecord(raw, 2)).toMatchObject({ id: 'abc123', state, version: 2 })
expect(readConsentRecord(raw, 3)).toBeNull()
expect(readConsentRecord(raw)).toBeNull() // default version 1
})
})