Learn
Next.js security headers checklist: CSP nonces, source maps and data leaks
Next.js gives you three places to set response headers (next.config.js, the proxy file, and the hosting platform) and several ways to ship more to the browser than you meant to: a framework banner, source maps, environment variables and serialised page data. This checklist covers each one for a client project, with the configuration that works on current Next.js, what changes between Vercel and self-hosting, and which items an external scan can confirm for you.
For what each header does and a safe starting value, see the HTTP security headers cheat sheet. To see what a client's Next.js site sends today, run it through the free headers and TLS checker first.
1. Static headers in next.config.js
For headers that never change per request, async headers() in next.config.js is the simplest place. Turn off the X-Powered-By: Next.js banner in the same file:
// next.config.js
module.exports = {
poweredByHeader: false,
async headers() {
return [
{
source: '/(.*)',
headers: [
{ key: 'X-Content-Type-Options', value: 'nosniff' },
{ key: 'X-Frame-Options', value: 'DENY' },
{ key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
{ key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
{ key: 'Content-Security-Policy-Report-Only', value: "default-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" },
],
},
]
},
}
next.config.tstakes the same object as a default export.- Static export (
output: 'export') has no Next.js server, soheaders()does not apply. Set the headers at the host or CDN instead. - One layer only. If
vercel.json, nginx or Cloudflare also sets these headers, remove them there. Two copies of a CSP means the browser enforces both. The guide to configuring headers on Cloudflare, nginx and Vercel covers the other layers.
2. A strict CSP with nonces in the proxy
A Content-Security-Policy that blocks injected script needs a fresh nonce per request, and the place to generate it is the proxy file. On Next.js 16 and later that is proxy.ts, which exports proxy; on Next.js 15 and earlier it is middleware.ts, which exports middleware. A trimmed version of the pattern in the Next.js documentation:
// proxy.ts
import { NextRequest, NextResponse } from 'next/server'
export function proxy(request: NextRequest) {
const nonce = Buffer.from(crypto.randomUUID()).toString('base64')
const csp = [
"default-src 'self'",
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'`,
`style-src 'self' 'nonce-${nonce}'`,
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
"frame-ancestors 'none'",
].join('; ')
const requestHeaders = new Headers(request.headers)
requestHeaders.set('x-nonce', nonce)
requestHeaders.set('Content-Security-Policy', csp)
const response = NextResponse.next({ request: { headers: requestHeaders } })
response.headers.set('Content-Security-Policy', csp)
return response
}
export const config = {
matcher: [{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
}],
}
- Next.js reads the nonce from the request's CSP header and adds it to its own scripts. Pass it to third-party
<Script>tags yourself, readingx-noncewithheaders(). - Every page becomes dynamically rendered. Static generation, ISR and CDN caching of HTML no longer apply, which matters for a marketing site. Discuss the hosting cost with the client before you commit to it.
- Development needs
'unsafe-eval'; production does not. Add it only whenNODE_ENVisdevelopment. - The no-nonce alternative in the Next.js docs uses
'unsafe-inline'inscript-src. That keeps pages static but does not stop injected inline script, and ScanBastion reports it as a CSP that allows injected scripts. The experimental SRI option (App Router only) is a middle way worth testing.
3. Vercel or self-hosted
- Vercel already sends HSTS on the domains it serves; check what arrives before adding your own. Use
vercel.jsonornext.config.jsfor headers, not both. Check the project's Deployment Protection settings: preview deployments run the same code, sometimes against production data, on their own URLs. - Self-hosted (
next startor a container behind nginx, a load balancer or Cloudflare): you own HSTS and TLS, so set them at the proxy. Make sure the Node.js port is not reachable directly from the internet, bypassing the layer that sets the headers. With several instances, setNEXT_SERVER_ACTIONS_ENCRYPTION_KEYso they share one Server Actions key.
4. What a Next.js build can leak
- Production source maps. They are off by default. If
productionBrowserSourceMaps: trueis set, Next.js serves the original source next to every bundle. Turn it off, or upload maps to your error tracker instead of publishing them. NEXT_PUBLIC_variables. Their values are inlined into the JavaScript sent to every visitor at build time. An API secret, a service-role database key or a private token with that prefix is public. Search the built.next/staticoutput for known secret values before each release.- Page data. In the Pages Router, everything
getServerSidePropsorgetStaticPropsreturns is serialised into the page and served as JSON under/_next/data/. In the App Router, props passed from a Server Component to a Client Component reach the browser. Return only the fields the page displays, and mark data-access modules withimport 'server-only'. - Server Actions can be called by a direct POST request, whatever the page around them checks. Authenticate and authorise inside each action, not only in the proxy. In 2025, CVE-2025-29927 let a single request header skip middleware on self-hosted sites, and with it any authorisation that lived only there.
- The framework version. Keep Next.js and React on patched releases. The React Server Components flaw of December 2025 (CVE-2025-66478) allowed remote code execution on unpatched App Router sites running Next.js 15 and 16; upgrade to the latest patch release in your line.
5. What an external scan confirms
A ScanBastion scan identifies a Next.js site from /_next/static/ paths in its pages, and Vercel from the x-vercel-id header, and lists both under technologies detected. It then checks, on every crawled page:
- the security headers and cookie flags as delivered, including a CSP that is missing, report-only or weakened by
'unsafe-inline'without nonces; - first-party scripts that reference a source map, and, in the Standard profile, whether up to two of those maps are actually published;
- outdated JavaScript libraries, third-party scripts without Subresource Integrity, and error output in pages, through the built-in passive analysis.
It does not read your environment variables, judge what page data should be private, or report the Next.js version, so items in section 4 other than source maps stay manual checks. When the configuration is in place, run a scan and keep the before-and-after for the client.