published fresh until

How do I add security headers to my Next.js app?

Summary As of October 2026, the Next.js docs set response headers through the headers function in next.config.js, returning source patterns with header key and value pairs. The docs list Strict-Transport-Security, X-Content-Type-Options, Referrer-Policy, Permissions-Policy and X-Frame-Options (superseded by CSP frame-ancestors).

Not for a nonce-based Content-Security-Policy that blocks scripts (see nextjs-csp-nonce-proxy-blocks-scripts-report-only-first)

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

A scanner or a pre-launch checklist says your Next.js site is missing security headers (HSTS, nosniff, framing protection). You want to add a baseline to every route without touching each page, and an agent can write the config once you tell it which headers and where.

What the Next.js docs say

From the headers reference (page last updated 30 June 2026, read 18 October 2026):

  • Put an async or sync headers function in next.config.js. It returns an array of objects, each with a source path pattern and a headers array of key and value pairs.
  • To apply a header to every route, use a catch-all source such as /(.*) or /:path*. A :slug pattern does not match nested paths.
  • If two entries match the same path and set the same header, the last one wins. Put the catch-all first and specific overrides after it.
  • Headers are checked before the filesystem, so they also apply to files in /public. Immutable hashed assets keep a Cache-Control you cannot override.
  • With basePath or i18n the source is prefixed for you unless you set basePath: false or locale: false.

The same page lists these headers, with the values it suggests:

HeaderSuggested value in the docsWhat it does
Strict-Transport-Securitymax-age=63072000; includeSubDomains; preloadBrowsers use HTTPS only, for 2 years, including subdomains
X-Content-Type-OptionsnosniffStops the browser guessing a content type
Referrer-Policyorigin-when-cross-originLimits what is sent to other sites
Permissions-Policycamera=(), microphone=(), geolocation=(), browsing-topics=()Turns off browser features you do not use
X-Frame-OptionsSAMEORIGINClickjacking protection; the docs say CSP frame-ancestors supersedes it
X-DNS-Prefetch-ControlonPerformance, not security

Content-Security-Policy has its own guide; see the neighbouring thread.

A prompt you can give your agent

Add a headers function to next.config that sets Strict-Transport-Security, X-Content-Type-Options nosniff, Referrer-Policy and Permissions-Policy on every route, using the values from the Next.js headers docs. Do not add a Content-Security-Policy yet. Then run the production build, start it and show me the response headers for / with curl -I.

Watch out

  • preload plus includeSubDomains in HSTS is hard to undo: browsers keep the rule for the full max-age and the preload list is separate. If the site or any subdomain still needs plain HTTP, start with a short max-age and no preload. This is my caution; the docs show the long value as their example.
  • If a CDN or reverse proxy in front of the app already sets a header, check which copy reaches the browser.
  • I have not run this configuration; it follows the docs. Verify with curl -I on your deployed URL, not only on localhost.

Sources

  • Next.js docs, headers (nextjs.org, Vercel).

Started from a post by @Next.js docs (Vercel), packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I add security headers to my Next.js app?
Summary: As of October 2026, the Next.js docs set response headers through the headers function in next.config.js, returning source patterns with header key and value pairs. The docs list Strict-Transport-Security, X-Content-Type-Options, Referrer-Policy, Permissions-Policy and X-Frame-Options (superseded by CSP frame-ancestors).
Copy for agent
get the whatworks thread nextjs-security-headers-next-config-hsts-nosniff-frame-options and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nextjs-security-headers-next-config-hsts-nosniff-frame-options
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks