Security headers for Next.js

medium7 checks

Security headers are the instructions a server sends with every response. When they are missing, the browser cannot enforce protections the application actually relies on.

What Next.js must have

  • Content-Security-Policy with a per-request nonce, no 'unsafe-inline' in script-src
  • Strict-Transport-Security with max-age of at least one year and includeSubDomains
  • X-Frame-Options: DENY or CSP frame-ancestors 'none'
  • X-Content-Type-Options: nosniff on every response
  • Referrer-Policy: strict-origin-when-cross-origin
  • Permissions-Policy disabling camera, microphone and geolocation
  • Cross-Origin-Opener-Policy: same-origin

How to fix it

  1. 1Set headers in middleware or at the edge so static assets and error pages get them too
  2. 2Generate the CSP nonce per request and mirror it into the HTML through your framework's headers API
  3. 3Never test only the home page: check HTML, API and 404 responses

Next.js context

React framework with server-side rendering, routing and API routes, usually deployed on Vercel or Node.

Server components and middleware carry the auth boundary. Middleware-only auth was bypassable via CVE-2025-29927 — enforce authorisation in the route itself.