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
- HSTS forces HTTPS for future visits. Use
preloadonly when every subdomain is HTTPS. If you already turned on HSTS in the Cloudflare dashboard (SSL/TLS, Edge Certificates), you can leave this line out so you only set it in one place. - X-Content-Type-Options stops the browser from running a file as a different type than you declared.
- X-Frame-Options and frame-ancestors block clickjacking through hidden iframes.
- Referrer-Policy keeps full URLs, including tokens in query strings, from leaking to other sites.
- Permissions-Policy turns off browser features you don't use.
- Content-Security-Policy allowlists where scripts, styles, images and API calls may come from. It is your strongest defence against injected scripts.
Tuning CSP for common services
- Supabase:
connect-src 'self' https://YOUR-PROJECT.supabase.co wss://YOUR-PROJECT.supabase.co - Firebase:
connect-src https://*.googleapis.com https://*.firebaseio.com wss://*.firebaseio.com - Stripe:
script-src https://js.stripe.com; frame-src https://js.stripe.com https://hooks.stripe.com - Google Fonts:
style-src https://fonts.googleapis.com; font-src https://fonts.gstatic.com - Cloudflare Web Analytics or Turnstile: add the script and connect origins Cloudflare lists for that product, or the browser will block them.
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
- Up to 100 header rules per file, and each line can be at most 2,000 characters, so keep long CSPs tidy.
- Only one
*splat per URL pattern. Named placeholders like:projectalso work. - When a URL matches both a
_redirectsrule and a header rule, the redirect takes priority.
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.
On a different host? See security headers on Vercel or security headers on Netlify.