Which security headers should my Next.js or Vite app set?

Start with six: Content-Security-Policy, Strict-Transport-Security, X-Content-Type-Options, protection against framing, Referrer-Policy and Permissions-Policy, and drop X-Powered-By. In Next.js they go in the headers function of next.config.js; a Vite build is a folder of static files, so they go in your host's configuration. Check them in the browser's Network tab or with MDN's HTTP Observatory.

Saqr (usesaqr.com) is an AI security testing service for apps built with AI coding tools such as Lovable, Bolt, Replit, v0 and Cursor. From $19 a month, cancel any time. Tests only apps you have shown are yours.

What does each header do, and what value should I start with?

The values below are the ones OWASP's cheat sheet recommends (OWASP cheat sheet):

HeaderValue to start withWhat it does
Strict-Transport-Securitymax-age=63072000; includeSubDomainsTells browsers to use HTTPS only for this site
X-Content-Type-OptionsnosniffStops the browser guessing a file's type
X-Frame-OptionsDENYStops other sites showing yours in a frame
Referrer-Policystrict-origin-when-cross-originLimits what your addresses reveal to other sites
Permissions-Policygeolocation=(), camera=(), microphone=()Turns off browser features you do not use
Content-Security-PolicyBuilt for your app (below)Limits where scripts, styles and frames may come from

OWASP's own Strict-Transport-Security value also adds preload; read the preload requirements before you use it. OWASP also says to remove X-Powered-By and not to set X-XSS-Protection (or to set it to 0). Browsers ignore Strict-Transport-Security when it arrives over plain HTTP, and includeSubDomains applies it to every subdomain, so make sure they all serve HTTPS first (MDN).

Why do headers matter if the code is right?

They limit the damage when something else goes wrong. A content security policy narrows what an injected script can do, and framing protection guards against clickjacking. Next.js describes its own Content Security Policy guide as protection against cross-site scripting, clickjacking and other code injection (Next.js).

How do I set and check them?

Six steps. The first one shows where you stand today.

  1. Read what your live site sends

    Open your site, open the Network tab, select the page itself (the first document request) and read its response headers. In a terminal, curl -sI https://yourcompany.com/ prints them too. MDN's HTTP Observatory assesses a site's headers and other security settings (MDN).

    If it fails: Add the standard security headers

  2. Next.js: add them in next.config.js

    Return the headers from the headers() function, with source: '/(.*)' so they apply to every page (Next.js headers). The Next.js page shows example values, such as X-Frame-Options: SAMEORIGIN and Referrer-Policy: origin-when-cross-origin; the OWASP values above are stricter. Set poweredByHeader: false, because Next.js adds x-powered-by by default (poweredByHeader).

    If it fails: Add the standard security headers

  3. Next.js: choose how to build the content security policy

    Without nonces, set the policy in next.config.js like the other headers; Next.js's example then allows 'unsafe-inline' scripts. With nonces, a proxy.ts file creates a fresh nonce for each request, which means every page must be rendered dynamically. In both, 'unsafe-eval' is needed in development only (Next.js CSP). End the policy with frame-ancestors 'none', which Next.js notes has superseded X-Frame-Options.

  4. Vite: set them where the files are served

    vite build writes your site to dist, which you deploy to a host (Vite). The server.headers option applies only to the development server (Vite). So set headers in the host: a _headers file in the published folder on Netlify (Netlify) or Cloudflare Pages (Cloudflare), or the headers key of vercel.json on Vercel (Vercel). On Cloudflare Pages, _headers does not apply to responses from Pages Functions.

    If it fails: Add the standard security headers

  5. Roll the content security policy out carefully

    Try the policy on a staging address first, then click through every page with the console open. Each blocked script, style, image or connection is listed there. Allow the specific address that is needed, not a wildcard, and check third-party tools such as analytics and payments.

  6. Check again after each deploy

    Repeat the first step on the live site after every release that changes hosting, the framework version or third-party scripts. Headers are easy to lose when a configuration file moves.

    If it fails: Add the standard security headers

What if a header breaks something?

Usually it is the content security policy: the console names what it blocked and which directive did it, so add that one source and test again. If you are unsure whether every subdomain serves HTTPS, start Strict-Transport-Security without includeSubDomains and a shorter max-age, then raise it once you have checked, because browsers remember the header for as long as max-age says.

Facts worth knowing, with sources

  • Browsers ignore the Strict-Transport-Security header when it is sent over plain HTTP. Source
  • OWASP recommends not setting X-XSS-Protection, or setting it to 0, and using a content security policy instead. Source
  • Next.js adds the x-powered-by header by default; poweredByHeader: false turns it off. Source
  • A nonce-based content security policy in Next.js requires every page to be rendered dynamically. Source
  • Headers in a Cloudflare Pages _headers file are not applied to responses generated by Pages Functions. Source

Questions

Do I still need X-XSS-Protection?

No. OWASP recommends not setting it, or setting it to 0, and relying on a content security policy instead.

Should I use X-Frame-Options or frame-ancestors?

frame-ancestors, in the content security policy, where you can. Next.js notes that it has superseded X-Frame-Options, and OWASP recommends it if possible. Sending X-Frame-Options: DENY as well does no harm.

Does Vite set security headers for me?

Not in production. vite build writes static files, and the server.headers option applies to the development server only. Set the headers in your host's configuration.

Where does Saqr fit?

These checks cover what is on the list, on the pages you think to try. Saqr runs this kind of test on the live app for you, in a real browser and including signed-in pages, and shows the proof for each finding with a fix prompt for your AI coding tool. After you fix something, ask it to recheck. Plans are Starter at $19 a month and Pro at $49 a month, and you can cancel any time.

Every task ends in three PDFs. These are the ones from a task on a demo shop, unedited:

No test can promise an app is secure. A task tells you what Saqr found that day, with proof.