Files
ipal-kit/src/modules/consent/storage.ts
T
radoslaw.smolinskiandClaude Opus 5.5 ebf70d49bd 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]>
2026-10-16 02:31:58 +02:00

62 lines
2.3 KiB
TypeScript

import type { ConsentState } from './categories.js'
import { CONSENT_CATEGORIES, DEFAULT_CONSENT } from './categories.js'
/**
* Consent persistence in a cookie. Stored with a version so old consents can be
* invalidated when the policy changes (Cookie Settings → Policy version in the
* panel), a timestamp (GDPR: consent must be dated) and a random id that links
* the visitor's choice to the optional server-side consent record. Readable both
* client-side (banner) and server-side (script gating, proxy).
*/
export const CONSENT_COOKIE = 'cookie-consent'
/** Default policy version — the panel's Cookie Settings → Policy version overrides it. */
export const CONSENT_VERSION = 1
export const CONSENT_MAX_AGE = 60 * 60 * 24 * 180 // 180 days
type StoredConsent = {
/** Random id of this decision (no personal data) — matches the consent record. */
id?: string
state: ConsentState
timestamp: string
version: number
}
/** Random id for a consent decision. */
export function newConsentId(): string {
const bytes = new Uint8Array(12)
globalThis.crypto.getRandomValues(bytes)
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')
}
export function serializeConsent(state: ConsentState, version: number = CONSENT_VERSION, id?: string): string {
const payload: StoredConsent = {
...(id ? { id } : {}),
state,
timestamp: new Date().toISOString(),
version,
}
return encodeURIComponent(JSON.stringify(payload))
}
/** Full stored record (id, timestamp, version) — null when missing, unreadable or from another policy version. */
export function readConsentRecord(raw: string | undefined, version: number = CONSENT_VERSION): ({ state: ConsentState } & StoredConsent) | null {
if (!raw) {return null}
try {
const parsed = JSON.parse(decodeURIComponent(raw)) as StoredConsent
if (parsed.version !== version) {return null}
const state = { ...DEFAULT_CONSENT }
for (const cat of CONSENT_CATEGORIES) {
if (typeof parsed.state?.[cat] === 'boolean') {state[cat] = parsed.state[cat]}
}
state.necessary = true
return { ...parsed, state }
} catch {
return null
}
}
export function parseConsent(raw: string | undefined, version: number = CONSENT_VERSION): ConsentState | null {
return readConsentRecord(raw, version)?.state ?? null
}