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:
co-authored by
Claude Opus 5.5
parent
771dda1c6f
commit
ff4c595b91
@@ -1,6 +1,9 @@
|
||||
import type { BuildCspArgs } from '../security/buildCsp.js'
|
||||
import type { I18nConfig } from './types.js'
|
||||
|
||||
import { MARKDOWN_PREFIX, markdownHref, prefersMarkdown } from '../content/markdownNegotiation.js'
|
||||
import { buildCsp, buildReportingEndpoints } from '../security/buildCsp.js'
|
||||
import { createCspNonce, CSP_NONCE_HEADER } from '../security/cspNonce.js'
|
||||
import { isValidLocale } from './helpers.js'
|
||||
import { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './localeMiddleware.js'
|
||||
|
||||
@@ -23,6 +26,8 @@ type ProxyRequest = {
|
||||
|
||||
type ProxyResponse = {
|
||||
cookies: { set: (name: string, value: string, opts?: Record<string, unknown>) => void }
|
||||
/** Needed for `csp`; Next's NextResponse has it. */
|
||||
headers?: Headers
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,6 +45,16 @@ type CreateProxyOptions = {
|
||||
consentCategory?: 'functional' | 'necessary'
|
||||
consentCookieName?: string
|
||||
cookieName?: string
|
||||
/**
|
||||
* Content-Security-Policy with a per-request nonce (buildCsp args, no `nonce`).
|
||||
* The proxy creates the nonce, puts the policy on the request (Next reads the
|
||||
* nonce from it and adds it to its own scripts) and on the response, and
|
||||
* exposes the nonce as the `x-nonce` request header for <SiteProviders nonce>.
|
||||
* Nonces need dynamic rendering: every page under the matcher renders per
|
||||
* request (no ISR / static HTML). Static sites keep the header-only CSP from
|
||||
* buildSecurityHeaders `csp` instead. Default off.
|
||||
*/
|
||||
csp?: Omit<BuildCspArgs, 'nonce'>
|
||||
/** Expose x-locale / x-pathname request headers to RSC. Default true. */
|
||||
exposeHeaders?: boolean
|
||||
/**
|
||||
@@ -72,7 +87,7 @@ export function createProxy(
|
||||
config: I18nConfig,
|
||||
deps: { NextResponse: NextResponseLike } & CreateProxyOptions,
|
||||
): (request: ProxyRequest) => ProxyResponse {
|
||||
const { exposeHeaders = true, markdown, NextResponse, ...mwOptions } = deps
|
||||
const { csp, exposeHeaders = true, markdown, NextResponse, ...mwOptions } = deps
|
||||
const run = createLocaleMiddleware({ config, ...mwOptions })
|
||||
const mdPrefix = markdown ? (typeof markdown === 'string' ? markdown : MARKDOWN_PREFIX).replace(/\/$/, '') : null
|
||||
|
||||
@@ -92,6 +107,12 @@ export function createProxy(
|
||||
const result = run(request)
|
||||
|
||||
const requestHeaders = new Headers(request.headers)
|
||||
const nonce = csp ? createCspNonce() : undefined
|
||||
const policy = csp ? buildCsp({ ...csp, nonce }) : null
|
||||
if (policy && nonce) {
|
||||
requestHeaders.set(policy.key, policy.value)
|
||||
requestHeaders.set(CSP_NONCE_HEADER, nonce)
|
||||
}
|
||||
if (exposeHeaders) {
|
||||
const seg = pathname.split('/').filter(Boolean)[0] ?? ''
|
||||
const locale = isValidLocale(seg, config) ? seg : config.defaultLocale
|
||||
@@ -104,6 +125,14 @@ export function createProxy(
|
||||
? NextResponse.redirect(new URL(result.location, request.url))
|
||||
: NextResponse.next({ request: { headers: requestHeaders } })
|
||||
|
||||
if (policy) {
|
||||
response.headers?.set(policy.key, policy.value)
|
||||
if (csp?.reportUri) {
|
||||
const re = buildReportingEndpoints(csp.reportUri, csp.reportGroup)
|
||||
response.headers?.set(re.key, re.value)
|
||||
}
|
||||
}
|
||||
|
||||
if (result.cookie) {
|
||||
response.cookies.set(result.cookie.name, result.cookie.value, {
|
||||
path: '/',
|
||||
|
||||
Reference in New Issue
Block a user