How to add security headers to a site on Netlify
Last reviewed November 2, 2026 against the providers' current documentation.
The short version
Create a plain text file called _headers that ends up in your publish directory (with Vite, put it in public/), with a /* rule listing the headers, or add a [[headers]] block to netlify.toml. Deploy and check the response headers. Start the Content-Security-Policy in report-only mode, as Netlify's CSP docs do, and switch it to enforcing once the browser console shows no violations. If a page is rendered by a function (server-side rendering, Next.js), Netlify doesn't add these headers to it, so the app has to send them.
Why it matters
Security headers are instructions your server sends with every page. Content-Security-Policy tells the browser which scripts may run, so an injected script is blocked. X-Frame-Options (or CSP frame-ancestors) stops other sites from loading your app in a hidden frame to trick users into clicks. X-Content-Type-Options: nosniff stops the browser from guessing file types. Referrer-Policy limits how much of your URLs leak to other sites. Strict-Transport-Security (HSTS) makes returning browsers go straight to HTTPS. Netlify gives every site a free, automatically renewed HTTPS certificate, and its docs show HSTS as a header you add yourself in _headers or netlify.toml; the other headers are up to you as well.
Fix it, in this order
See what your site sends now
Run our free headers check on your live address, or open your site, press F12, go to the Network tab, reload and click the first request to read its Response Headers. Note which ofcontent-security-policy,x-frame-options,x-content-type-options,referrer-policyandstrict-transport-securityare missing.Add a _headers file to your publish directory
Netlify reads a plain text file called_headersfrom the publish directory, the folder of finished files it deploys after your build (oftendist/orbuild/). Don't create it in the build output by hand, because the next build replaces that folder: with Vite, put it inpublic/, which Vite copies to the root ofdist/as-is. A path on its own line, then indentedName: valuelines, with/*matching every page:
Lines starting with/* Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self' X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff Referrer-Policy: strict-origin-when-cross-origin Permissions-Policy: camera=(), microphone=(), geolocation=() Strict-Transport-Security: max-age=31536000#are comments and header names are case-insensitive. On Jekyll, files starting with_are left out of the build unless you add_headerstoincludein_config.yml.Or use netlify.toml
If you'd rather keep everything in one config file, put a[[headers]]table innetlify.tomlat the root of your repository (or your base directory).foris the path pattern and[headers.values]holds the headers:
Netlify's docs say headers set this way are global for all builds and can't be scoped to a branch or deploy context; for per-context headers they describe copying a different[[headers]] for = "/*" [headers.values] Content-Security-Policy-Report-Only = "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'" X-Frame-Options = "SAMEORIGIN" X-Content-Type-Options = "nosniff" Referrer-Policy = "strict-origin-when-cross-origin" Permissions-Policy = "camera=(), microphone=(), geolocation=()" Strict-Transport-Security = "max-age=31536000"_headersfile into the publish directory in each context's build command. Pick one place for each rule rather than defining the same path in both files.SSR, functions and Next.js: send headers from the app
Netlify's docs are explicit: custom headers apply only to files Netlify serves from its own storage. A URL handled by a function or edge function, such as a server-side rendered page, or content proxied from another site doesn't get them; the function or proxied site has to return the headers itself. A plain Vite or React single-page app is static files, so_headerscovers it. For Next.js, Netlify runs the app through its OpenNext adapter and server-rendered pages are handled by functions, so set the headers in the app withheaders()innext.config.js(Next.js's documented option, same config as in our Vercel guide) and check a server-rendered page on the deploy, not only the homepage.Deploy, then watch the CSP reports and enforce it
Push the change and check the response headers of the new deploy, or rerun the headers check. If nothing changed, make sure_headersreally is in the deployed folder (look at the deploy's file list) and that the path pattern matches the URL you tested. WithContent-Security-Policy-Report-Onlynothing is blocked; the browser console lists everything the policy would block. Click through your app (log in, pay, upload) and add each legitimate domain to the matching directive, for example analytics inscript-srcand your Supabase or API address inconnect-src. When the console is clean, rename the header toContent-Security-Policyand deploy again. Until then,X-Frame-Optionsis what stops framing, because a report-onlyframe-ancestorsblocks nothing. Netlify also offers a Content Security Policy extension (or the@netlify/plugin-csp-noncebuild plugin) that adds a fresh nonce to your scripts on every request using edge functions, and suggests testing the policy with Google's CSP Evaluator.HSTS: start without preload
The example above sendsStrict-Transport-Security: max-age=31536000(one year) for the domain you visited. Netlify's HTTPS docs showmax-age=63072000; includeSubDomains; preloadfor joining the browsers' preload list via hstspreload.org, but they warn to usepreloadonly once the domain and all subdomains work over HTTPS only, because it's hard to remove; they also note the domain must be reachable on itswwwsubdomain. AddincludeSubDomainsandpreloadlater, once you're sure.
Or ask your AI tool to do it
Paste this into Lovable, Bolt, Cursor, Claude Code or whichever tool built your app. Review what it changes before you deploy.
My site is deployed on Netlify. Add security headers for every path. If the publish directory is built from a public/ or static/ folder (for example Vite's public/), create a _headers file there so it ends up in the publish directory; otherwise add a [[headers]] block with for = "/*" to netlify.toml at the repository root. Add: Content-Security-Policy-Report-Only (start from default-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self', and add every external domain the app really loads scripts, styles, fonts, images and API calls from), X-Frame-Options: SAMEORIGIN, X-Content-Type-Options: nosniff, Referrer-Policy: strict-origin-when-cross-origin, Permissions-Policy: camera=(), microphone=(), geolocation=() and Strict-Transport-Security: max-age=31536000 (no preload). If any page is rendered by a Netlify Function, edge function or Next.js server rendering, also make those responses send the same headers (for Next.js, an async headers() in next.config.js). List the external domains you added to the CSP and why, so I can review it before switching it from report-only to enforcing.
How this happens
Nothing breaks without them, so nobody adds them. Netlify's automatic HTTPS makes a new deploy look finished, and AI builders like Bolt, Lovable or Cursor generate the app, not the hosting config. The headers only show up as a problem when someone runs a scanner or a customer's security review asks for them.
Check that it's really fixed
After you deploy, run the free check. It reads your live site the way an attacker would and tells you if any secret key is still visible.
Topic check: Security headers check
Find the other leaks before someone else does
The free check reads your home page and its scripts. The website security report goes through up to 30 pages and 40 JavaScript files, plus .env and .git files, security headers, cookies and CORS, domain and email records, and libraries with known vulnerabilities. Every finding comes with the fix for your host and a prompt for your AI tool.
$19 once, including 20 runs over 30 days so you can fix things and check again.

Questions
Does Netlify add security headers by default?
Netlify provides a free HTTPS certificate on every site, created and renewed automatically. Its docs present HSTS as a header you include yourself in _headers or netlify.toml, and Content-Security-Policy, X-Frame-Options, X-Content-Type-Options and Referrer-Policy are custom headers you add the same way. Check your own response headers to see what your site sends today.
Where does the _headers file go?
In the publish directory, the folder Netlify deploys after your build. Because the build recreates that folder, put the file somewhere your build copies into it: public/ for Vite (other frameworks have a similar static folder). For a site with no build step, put it next to index.html.
_headers or netlify.toml?
Either works for static files. netlify.toml keeps headers with your build settings in one file, and its settings override conflicting ones in the Netlify UI. _headers is the one Netlify's docs use for per-branch or per-context headers. Use one place per rule.
Why don't my headers show up on some pages?
Netlify applies custom headers only to files it serves itself. Pages rendered by a function or edge function (server-side rendering, many Next.js pages) and proxied content don't get them, so those responses have to set the headers in code. Also check that _headers is really in the deployed folder; on Jekyll it's excluded unless listed in include.
Will a Content Security Policy break my site?
An enforcing one can, if it leaves out a domain your app needs. Start with Content-Security-Policy-Report-Only, which Netlify describes as a dry run that collects violations without blocking anything, and switch to Content-Security-Policy once the console is clean.
Why does your free check still say "No Content Security Policy"?
The free check only counts an enforcing Content-Security-Policy header, so a report-only one still shows up until you switch it on. The $19 report grades the policy itself (report-only, 'unsafe-inline', 'unsafe-eval', wildcards) and drafts a starting CSP from the domains your pages actually load, with a ready _headers block when it detects Netlify.
Sources
- Netlify: Custom headers (_headers, netlify.toml, what they don't apply to)
- Netlify: File-based configuration (netlify.toml)
- Netlify: HTTPS (SSL) and HSTS preload
- Netlify: Content Security Policy (report-only, nonce extension)
- Netlify: Security checklist
- Netlify: Next.js on Netlify (OpenNext adapter)
- Vite: The public directory
- Next.js: headers in next.config.js
- MDN: Strict-Transport-Security