Home / Guides

How to add security headers on Cloudflare Pages

Add CSP, HSTS, X-Frame-Options and other security headers on Cloudflare Pages and Workers static assets with a _headers file. Copy-paste example, rule order gotchas, Functions caveat and testing.

Updated 2026-11-12 · Checked against Cloudflare's Pages and Workers headers docs on 2026-11-12 · ShipCheck guides

Cloudflare Pages serves your site over HTTPS, but it won't add the browser security headers that block XSS, clickjacking and MIME sniffing unless you ask. On Pages you ask with one plain-text file called _headers. No code change and no dashboard rule is needed for static files.

Step 1: create the _headers file

Create a file named _headers (no extension) that ends up in your build output directory. With Vite, Astro, SvelteKit static or most generators, put it in public/ (or static/) so the build copies it into dist/ or build/. With no framework, put it straight in the folder you deploy. Cloudflare reads the file and doesn't serve it.

/*
  Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
  X-Content-Type-Options: nosniff
  X-Frame-Options: DENY
  Referrer-Policy: strict-origin-when-cross-origin
  Permissions-Policy: camera=(), microphone=(), geolocation=()
  Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'

https://:project.pages.dev/*
  X-Robots-Tag: noindex

The first line of each block is a URL pattern (/* means every page), and the header lines underneath must be indented. The second block is optional: it keeps your *.pages.dev copy out of search results once you have a custom domain, using the placeholder syntax from Cloudflare's own example. Lines starting with # are comments.

Step 2: deploy and check

curl -sI https://yourproject.pages.dev | grep -iE "strict-transport|content-security|x-frame|x-content-type|referrer|permissions"

Or paste your URL into ShipCheck for a free header and HTTPS check with copy-paste fixes.

What these headers protect against

Tuning CSP for common services

To avoid breaking production, ship the policy as Content-Security-Policy-Report-Only first, click through the whole app with devtools open, fix what it reports, then switch to the enforcing header.

Rule order and repeated headers (the gotcha)

Every rule whose pattern matches a request applies, and rules run in the order they appear in the file. If two matching rules set the same header, Cloudflare joins the values with a comma instead of replacing them. That's fine for list headers like X-Robots-Tag, but for a security header it produces a value you didn't mean. To give one path a different value, detach the header with ! Header-Name in a rule placed after the broad one, then set it again:

/*
  X-Frame-Options: DENY

/embed/*
  ! X-Frame-Options
  X-Frame-Options: SAMEORIGIN

A detach placed above the rule it is meant to undo does nothing, because that rule hasn't added the header yet.

Limits worth knowing

Pages Functions, SSR and Workers

Cloudflare's docs are explicit: _headers rules are not applied to responses generated by Pages Functions (a functions/ folder or an advanced-mode _worker.js), even when the URL matches. If your framework renders pages on the server (Next.js via the Cloudflare adapter, Remix, SvelteKit SSR, Nuxt), set the headers on the Response in your Functions or middleware code instead. Moving to Workers with static assets? The same _headers file works there for static files, with the same exception for responses your Worker code creates.

Sister project (AgentCartridge, same owner): Cloudflare Pages / Workers _headers rules file for Cursor and Claude Code.

On a different host? See security headers on Vercel or security headers on Netlify.

Scan your app free

Paste your live URL or public GitHub repo. ShipCheck checks for leaked keys, committed .env files, missing security headers, HTTPS issues and slow pages, then gives you copy-paste fixes. No signup.

FAQ

Where does the Cloudflare Pages _headers file go?

In the folder that gets deployed (your build output directory, such as dist or build). With a framework, put it in public/ or static/ so the build copies it into the output. If it is not in the deployed output, Cloudflare ignores it.

Do _headers rules apply to Pages Functions or SSR pages?

No. Cloudflare's docs say _headers rules are not applied to responses generated by Pages Functions or a _worker.js, even when the URL matches. Set those headers on the Response in your Functions or Worker code.

Why did my header value get repeated with a comma?

When more than one rule matches a URL and both set the same header, Cloudflare joins the values with a comma. Give each security header one definition per path, or detach it with ! Header-Name in a later rule before setting it again.

More guides

How to add security headers on Netlify

Add CSP, HSTS, X-Frame-Options and other security headers on Netlify using a _headers file or netlify.toml. Copy-paste examples, framework tips and testing.

How to add security headers on Vercel

Copy-paste vercel.json to add CSP, HSTS, X-Frame-Options, X-Content-Type-Options, Referrer-Policy and Permissions-Policy on Vercel, plus how to test them.

The vibe-coded app launch checklist

A practical pre-launch checklist for apps built with Lovable, Cursor, Bolt, v0 or Replit: security, secrets, database rules, performance, SEO, legal and monitoring.

Exposed .env file or API key? How to check and rotate

How to find leaked API keys and committed .env files in your repo, JavaScript bundle or live site, rotate them safely, and stop it happening again.