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,4 +1,7 @@
|
||||
import type { BuildCspArgs } from './buildCsp.js'
|
||||
|
||||
import { isSiteNoindex } from '../seo/siteNoindex.js'
|
||||
import { buildCspHeaders } from './buildCsp.js'
|
||||
|
||||
/**
|
||||
* A single HTTP header, in the shape Next.js next.config headers() expects.
|
||||
@@ -7,10 +10,8 @@ export type SecurityHeader = { key: string; value: string }
|
||||
|
||||
export type BuildSecurityHeadersArgs = {
|
||||
/**
|
||||
* Extra headers to append or override. Same-key entries replace the default,
|
||||
* so you can e.g. add your project's Content-Security-Policy here — CSP is
|
||||
* intentionally NOT a default because it depends on the project's own
|
||||
* domains (scripts, images, fonts, analytics). Keep CSP in your project.
|
||||
* Extra headers to append or override. Same-key entries replace the default
|
||||
* (and the `csp` headers), so a hand-written Content-Security-Policy fits here too.
|
||||
*/
|
||||
additional?: SecurityHeader[]
|
||||
/**
|
||||
@@ -21,6 +22,13 @@ export type BuildSecurityHeadersArgs = {
|
||||
* that need window.opener; false to omit.
|
||||
*/
|
||||
coop?: 'same-origin' | 'same-origin-allow-popups' | false
|
||||
/**
|
||||
* Content-Security-Policy from buildCsp args — off by default, because a CSP
|
||||
* lists the project's own domains. With `reportUri` the Reporting-Endpoints
|
||||
* header is added too. Roll-out: `mode: 'report-only'` first, read the
|
||||
* reports, then `mode: 'enforce'` (the only change). See docs/security.md.
|
||||
*/
|
||||
csp?: BuildCspArgs | false
|
||||
/**
|
||||
* X-Frame-Options value. 'DENY' (default) blocks all framing; 'SAMEORIGIN'
|
||||
* allows same-origin framing. Note: CSP frame-ancestors supersedes this in
|
||||
@@ -56,19 +64,16 @@ export type BuildSecurityHeadersArgs = {
|
||||
* Permissions-Policy. These are identical across projects, so the plugin owns
|
||||
* the boilerplate; the client spreads the result into next.config's headers().
|
||||
*
|
||||
* Content-Security-Policy is deliberately excluded: a useful CSP enumerates the
|
||||
* exact domains a project loads from (its CDN, analytics, embeds), so it can't
|
||||
* be generic without being either too loose (useless) or too strict (breaks the
|
||||
* site). Add your project's CSP via `additional`.
|
||||
* Content-Security-Policy is opt-in: a useful CSP enumerates the exact domains
|
||||
* a project loads from (its CDN, analytics, embeds), so the project turns on the
|
||||
* buildCsp flags it needs through `csp` (or passes its own via `additional`).
|
||||
*
|
||||
* @example
|
||||
* // next.config.ts
|
||||
* import { buildSecurityHeaders } from '@intecion/ipal-kit'
|
||||
* const securityHeaders = buildSecurityHeaders({
|
||||
* hsts: process.env.NODE_ENV === 'production', // off in dev over http
|
||||
* additional: [
|
||||
* { key: 'Content-Security-Policy', value: "default-src 'self'; ..." },
|
||||
* ],
|
||||
* csp: { mode: 'report-only', reportUri: '/api/csp-report', analytics: true },
|
||||
* })
|
||||
* const nextConfig = {
|
||||
* async headers() {
|
||||
@@ -80,6 +85,7 @@ export function buildSecurityHeaders(args: BuildSecurityHeadersArgs = {}): Secur
|
||||
const {
|
||||
additional = [],
|
||||
coop = 'same-origin',
|
||||
csp = false,
|
||||
frameOptions = 'DENY',
|
||||
hsts = true,
|
||||
hstsIncludeSubDomains = true,
|
||||
@@ -123,6 +129,8 @@ export function buildSecurityHeaders(args: BuildSecurityHeadersArgs = {}): Secur
|
||||
headers.push({ key: 'X-Robots-Tag', value: 'noindex, nofollow' })
|
||||
}
|
||||
|
||||
if (csp) {headers.push(...buildCspHeaders(csp))}
|
||||
|
||||
// Merge additional: same-key entries override the defaults above.
|
||||
for (const extra of additional) {
|
||||
const i = headers.findIndex((h) => h.key.toLowerCase() === extra.key.toLowerCase())
|
||||
|
||||
Reference in New Issue
Block a user