Files
ipal-kit/src/modules/frontend/SiteProviders.tsx
T
Radosław SmolińskiandClaude Opus 5.5 ff4c595b91 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]>
2026-10-27 21:28:11 +01:00

119 lines
5.1 KiB
TypeScript

import type { ReactNode } from 'react'
import type { AnalyticsProps } from '../analytics/Analytics.js'
import type { ConsentCookieMap } from '../consent/useConsent.js'
import type { LightboxProviderProps } from '../lightbox/LightboxProvider.js'
import type { PageStatsProps } from '../stats/PageStats.js'
import type { SiteShell } from './createContentHelpers.js'
import { Analytics } from '../analytics/Analytics.js'
import { ConsentProvider } from '../consent/ConsentContext.js'
import { CookieBanner } from '../consent/CookieBanner.js'
import { CookieButton } from '../consent/CookieButton.js'
import { LightboxProvider } from '../lightbox/LightboxProvider.js'
import { JsonLdGraph } from '../seo/JsonLdGraph.js'
import { PageStats } from '../stats/PageStats.js'
import { MediaPreconnect } from '../storage/MediaPreconnect.js'
import { ThemeScript } from '../theme/ThemeScript.js'
import { TurnstileProvider } from '../turnstile/TurnstileProvider.js'
export type SiteProvidersProps = {
/** `defer` etc. for <Analytics>; `false` = no analytics tags at all. */
analytics?: false | Omit<AnalyticsProps, keyof SiteShell['analytics']>
/** The cookie banner. Default `<CookieBanner />`; pass your own configured one (`equalChoice`, classNames) or `false`. */
banner?: false | ReactNode
children: ReactNode
/** The "cookie settings" button. Default `<CookieButton />`; your own or `false`. */
cookieButton?: false | ReactNode
/** Extra cookies per category to delete when consent is withdrawn. */
cookieMap?: ConsentCookieMap
/** More site-wide JSON-LD nodes next to shell.jsonLd (e.g. buildNavigationJsonLd). */
jsonLd?: (null | Record<string, unknown> | undefined)[]
/**
* One shared image lightbox for the site: mounts <LightboxProvider> around the
* page, so every <LightboxTrigger> and `RenderRichText lightbox` uses it.
* `true` (texts from shell.lightboxTexts = Notifications → Lightbox) or
* `{ texts, classNames, icons, loop, unstyled }`. Default off. For renderImage / renderDialog (functions) mount the provider
* yourself in a client component.
*/
lightbox?: boolean | Omit<LightboxProviderProps, 'children' | 'renderDialog' | 'renderImage'>
/** Consent log endpoint, e.g. '/api/ipal/consent' (ipalKit consentLog: true). */
logEndpoint?: string
/**
* CSP nonce for a nonce-based policy (createProxy `csp`): read it in the layout
* with `(await headers()).get('x-nonce')`. Goes on the theme script and on the
* analytics tags the plugin injects (GTM passes it on to its own tags).
*/
nonce?: null | string
/** <link rel=preconnect> to the media origin (R2_PUBLIC_URL). Default true. */
preconnect?: boolean
/** getSiteShell(locale) from lib/content. */
shell: SiteShell
/**
* Built-in statistics (ipalKit stats: true): mounts <PageStats>. `true` or
* `{ locale, endpoint }`. Default off.
*/
stats?: boolean | PageStatsProps
/**
* Light / dark theme from Site Settings → Theme: renders <ThemeScript> first,
* so the theme is on <html> before the first paint. Put <SiteProviders> first in
* <body> and add `suppressHydrationWarning` to <html>. Default false.
*/
theme?: boolean
}
/**
* The wiring every [locale] layout repeats — consent, Turnstile, analytics,
* site JSON-LD, media preconnect — in the right order (Analytics inside the
* consent provider, forms inside Turnstile). Renders no markup of its own:
* <html>, <body>, header, footer and <main> stay in the layout.
*
* const shell = await getSiteShell(locale)
* <body>
* <SiteProviders shell={shell} logEndpoint="/api/ipal/consent" banner={<CookieBanner equalChoice />}>
* <Header … /><main>{children}</main><Footer … />
* </SiteProviders>
* </body>
*
* Every part is replaceable or switchable off; for a fully custom tree use the
* providers themselves (consent.md, turnstile.md) — the shell data still fits.
*/
export function SiteProviders({
analytics,
banner,
children,
cookieButton,
cookieMap,
jsonLd = [],
lightbox = false,
logEndpoint,
nonce,
preconnect = true,
shell,
stats = false,
theme = false,
}: SiteProvidersProps) {
return (
<>
{theme ? <ThemeScript {...shell.theme} nonce={nonce ?? undefined} /> : null}
{preconnect ? <MediaPreconnect /> : null}
<JsonLdGraph schemas={[...shell.jsonLd, ...jsonLd]} />
<ConsentProvider cookieMap={cookieMap} logEndpoint={logEndpoint} texts={shell.consent}>
<TurnstileProvider siteKey={shell.turnstileSiteKey}>
{lightbox ? (
<LightboxProvider texts={shell.lightboxTexts} {...(lightbox === true ? {} : lightbox)}>
{children}
</LightboxProvider>
) : (
children
)}
</TurnstileProvider>
{banner === undefined ? <CookieBanner /> : banner || null}
{cookieButton === undefined ? <CookieButton /> : cookieButton || null}
{analytics === false ? null : <Analytics {...shell.analytics} nonce={nonce ?? undefined} {...analytics} />}
{stats ? <PageStats {...(stats === true ? {} : stats)} /> : null}
</ConsentProvider>
</>
)
}