How to Check and Configure Security Headers in Next.js Apps

You need to set headers to match your app's routes and security needs. A static docs site may need different rules from a checkout flow. An app that needs SharedArrayBuffer may also need a different set of headers.1
Why Next.js needs custom header rules
CSP controls allowed content, while HSTS enforces HTTPS.2 Fresh Next.js starter apps lack most app-specific security headers. The response may lack CSP, HSTS and X-Frame-Options when viewed in browser tools.1, 3
Some headers are low-risk defaults for most live apps. CSP and rules for cross-origin isolation need more care because they can block required content.1
Essential headers to audit
X-Content-Type-Options set to nosniff tells browsers to trust the declared Content-Type. It stops them guessing a file's type from its contents.1
Without this rule, a browser may treat a wrongly labelled file as code. The nosniff value is a safe default for most Next.js apps.1
Content-Security-Policy, or CSP, defines which sources may supply scripts and styles. It also controls images, fonts, frames and other resources.1, 2
A well-designed CSP can reduce harm from XSS and injected code. The default-src 'self' rule limits resources to the app's own origin unless a more specific rule overrides it.1
Strict-Transport-Security, or HSTS, tells browsers to use HTTPS for a set period. This helps defend against SSL stripping and attacks that force HTTP instead.1
X-Frame-Options controls whether other pages can embed your page in a frame. This helps stop clickjacking, where attackers use your content in hidden frames.1, 3
DENY blocks all framing, while SAMEORIGIN allows frames from the same origin. CSP frame-ancestors offers more precise rules and is preferred for new projects.1
With frame-ancestors, 'none' blocks framing and 'self' permits the same origin. Some apps retain X-Frame-Options to support older browsers.1
Permissions-Policy controls access to browser APIs such as the camera and microphone. Disable those features if your site does not need them.3
Referrer-Policy controls what details are sent when a link takes users to another origin. Stricter rules can reduce the detail available to analytics.3, 1
The strict-origin-when-cross-origin value is a baseline option for Referrer-Policy. The origin-when-cross-origin value is another baseline option.1
Optional DNS and cross-origin controls
X-DNS-Prefetch-Control manages whether the browser looks up domain names ahead of time. Its on value enables this behaviour, while off disables it.2
Cross-origin isolation generally needs COOP and COEP together. COEP is relevant to apps that need APIs such as SharedArrayBuffer.1
COEP can require resources from other origins to opt in. CORP tells the browser which origins may load or embed a resource.1
Setting headers in next.config.js
Next.js lets you set custom response headers in next.config.js. Add an async headers function that returns an array of rules.2, 3
Each rule has a source field and a headers field. The source field matches the incoming request path.2
The source pattern '/(.*)' applies the rule to all routes. A specific path pattern can target one route instead.2
The headers field holds an array of response header objects. Each object uses key for the header name and value for its setting.2
When inline scripts need CSP nonces
Next.js may create inline scripts and styles while rendering a page. A static script-src 'self' rule can block scripts the app needs.1
Adding 'unsafe-inline' weakens CSP because it allows inline scripts broadly. For stricter security needs, a nonce-based CSP is a safer option.1
A nonce is a random token used once for each request. Attach it to the CSP header and to trusted scripts or styles.1
The browser blocks inline code unless its nonce matches. In current Next.js apps, proxy.js can create that token before the page renders.1
The proxy can attach the nonce to request headers and the response header. This puts the token's creation at request time.1
Deployment pitfalls
An overly strict CSP can block Next.js scripts, inline styles and custom fonts. It can also block outside scripts, analytics and images.1
Choose CSP rules that allow the resources your app needs. Payment flows, popups and embedded content can also be affected by strict header rules.2, 1
Use HSTS includeSubDomains only once HTTPS is stable. Only use includeSubDomains and preload when all subdomains support HTTPS.1
Do not enable COEP across all routes just to raise a scan grade. It can block outside images, scripts, frames, workers and analytics.1
Those resources need to load with CORS or carry compatible CORP headers. COEP can therefore block content that has not opted in.1
Checking active response headers
Run your app on localhost and open your browser's developer tools. Select the Network tab, then select the localhost request.3
Inspect the response headers shown for that request. This view shows which security headers are active.1, 3
Online header scanners can inspect your web app's HTTP response headers. They can show header status before and after you add the rules.2
SimplyScan's scanner is called the SimplyScan Security Engine. SimplyScan includes security headers among its registered checks and exposes the check_security_headers MCP tool.
SimplyScan's check_security_headers tool grades CSP, HSTS, X-Frame-Options and other headers from A to F. SimplyScan connects as a remote MCP server in clients that support Streamable-HTTP.4
Sign in and create a free account API key in Settings to use this connection.4 SimplyScan offers free scans with a free account.
Over the last 30 days, completed free and full scans had median times of at most 30 seconds. Their upper-quartile times were also at most 30 seconds.
SimplyScan uses database eligibility for scan badges. SimplyScan also rechecks TLS and security headers for those badges.
Sources
Frequently asked questions
Is your app secure? Run a free security scan in 30 seconds · free account required.
Scan your app free →