Security: hide X-Powered-By, CSP report-only with reports, admin policy and nonces

- withSecurityConfig: poweredByHeader false + strips X-Powered-By from headers()
  (Next's and withPayload's), works on either side of withPayload
- buildSecurityHeaders({ csp }): CSP + Reporting-Endpoints; mode switches
  report-only → enforce
- buildCsp: reportUri/reportGroup, nonce + strict-dynamic, dev unsafe-eval,
  metaPixel/linkedIn/tiktok/clarity flags, media-src for R2, override
- buildAdminCsp(Headers): looser policy for Payload admin (Monaco CDN, blob workers)
- createCspReportRoute + parseCspReports: report collector for both formats
- createProxy({ csp }) + createCspNonce: per-request nonce, x-nonce header;
  SiteProviders/ThemeScript/Analytics/ConsentScript accept nonce
- docs: security.md, AGENT.md, ipal-setup/reference/upgrade; ipal-audit checks

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-27 21:28:11 +01:00
co-authored by Claude Opus 5.5
parent 771dda1c6f
commit ff4c595b91
22 changed files with 1059 additions and 186 deletions
+9 -4
View File
@@ -32,11 +32,13 @@ function readConsent(version: number | undefined) {
return parseConsent(raw, version) ?? DEFAULT_CONSENT
}
function injectScript(src: string) {
function injectScript(src: string, nonce?: string) {
if (document.querySelector(`script[src="${src}"]`)) {return}
const s = document.createElement('script')
s.src = src
s.async = true
// GTM reads its own script's nonce and passes it to Custom HTML tags.
if (nonce) {s.nonce = nonce}
document.head.appendChild(s)
}
@@ -68,6 +70,8 @@ export type AnalyticsProps = {
defer?: boolean
/** Fallback delay after window `load`, in ms. Default 3500. */
deferTimeoutMs?: number
/** CSP nonce (nonce-based policy) — set on the GA4 / GTM script tag. */
nonce?: string
} & AnalyticsConfig
const INTERACTION_EVENTS = ['scroll', 'pointerdown', 'touchstart', 'keydown'] as const
@@ -135,6 +139,7 @@ export function Analytics(props: AnalyticsProps) {
gtmContainerId,
linkedInPartnerId,
metaPixelId,
nonce,
tiktokPixelId,
} = props
@@ -162,13 +167,13 @@ export function Analytics(props: AnalyticsProps) {
if (gtmContainerId) {
window.dataLayer = window.dataLayer || []
window.dataLayer.push({ event: 'gtm.js', 'gtm.start': Date.now() })
injectScript(GTM_SCRIPT(gtmContainerId))
injectScript(GTM_SCRIPT(gtmContainerId), nonce)
} else if (ga4MeasurementId) {
injectScript(GA4_SCRIPT(ga4MeasurementId))
injectScript(GA4_SCRIPT(ga4MeasurementId), nonce)
gtag('js', new Date())
gtag('config', ga4MeasurementId)
}
}, [googleAllowed, ga4MeasurementId, gtmContainerId])
}, [googleAllowed, ga4MeasurementId, gtmContainerId, nonce])
// --- Non-Google: each when `ready` AND its consent category is granted ---
// Loaders are idempotent, so re-running on state changes is safe; a tag loads