Security headers on Cloudflare Pages: tested CSP and HSTS
A static site is about the easiest place to get security headers right, because the files and the policy live in the same repo. I added four headers to this site with one _headers file on Cloudflare Pages, then loaded every page in headless Chromium to see whether the policy broke anything. The before and after output below is from this site, so nothing is mocked.
What you get by default
First, what Pages already sends. This is the live response for this site before I changed anything, from a *.pages.dev hostname with curl -sI:
HTTP/2 200
content-type: text/html; charset=utf-8
access-control-allow-origin: *
cache-control: public, max-age=0, must-revalidate
referrer-policy: strict-origin-when-cross-origin
x-content-type-options: nosniff
server: cloudflare
I expected a bare response, so referrer-policy and x-content-type-options already being there was a small surprise. I left them alone. Missing were HSTS, CSP, clickjacking protection and Permissions-Policy. I only tested a pages.dev hostname, so check your own domain with curl -sI before assuming the same defaults.
If you paste curl -sI output into the HTTP security headers checker on this site, it runs in your browser and sends nothing. For the “before” response it said:
[warn] Strict-Transport-Security: Missing. ...
[warn] Content-Security-Policy: Missing. ...
[warn] X-Frame-Options: No clickjacking protection: neither X-Frame-Options nor CSP frame-ancestors. ...
[info] Permissions-Policy: Missing. ...
[ok] X-Content-Type-Options: nosniff.
[ok] Referrer-Policy: strict-origin-when-cross-origin.
The _headers file
Put a file named _headers (no extension) in the folder that gets deployed. For Astro that’s public/. This is what I shipped:
/*
Strict-Transport-Security: max-age=15552000
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'
X-Frame-Options: DENY
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()
HSTS tells browsers to use HTTPS only for this host for 180 days (15552000 seconds). Browsers ignore it when it arrives over plain HTTP, so it only matters on HTTPS responses. I left out includeSubDomains and preload on purpose, because both are hard to undo and getting off the preload list can take a long time. Start with something short like max-age=300, make sure nothing breaks, then raise it.
The CSP says everything must come from the same origin, plugins are off (object-src 'none'), and nobody may frame the page (frame-ancestors 'none'). form-action 'self' stops a hijacked form from posting somewhere else. X-Frame-Options: DENY is the old clickjacking header. Modern browsers use frame-ancestors, so I keep both for the old ones. Permissions-Policy turns off browser features this site never uses.
Two limits to know. _headers only applies to static assets, and responses from Pages Functions set their own headers. And Cloudflare documents size limits for the file, which I never got near.
The part that usually breaks: inline scripts
A strict script-src 'self' blocks inline <script> blocks, and static site generators love to emit them. Astro inlines small scripts into the HTML by default, and one of this site’s tools had exactly that. I didn’t want to weaken the policy with 'unsafe-inline', so I told the bundler never to inline scripts (vite.build.assetsInlineLimit: 0 in astro.config.mjs). Every script is now its own file, which 'self' allows.
The JSON-LD blocks (<script type="application/ld+json">) stayed inline and caused no violations. They’re data, not code, so the browser doesn’t run them.
I couldn’t avoid style-src 'unsafe-inline'. The code highlighter writes style="..." attributes onto code blocks. Inline styles are a much smaller risk than inline scripts, but it’s a real compromise and I won’t call this a hardened policy.
Prove it with a browser
The reliable way to find CSP breakage is to load every page and read the console. csp_check.mjs in the evidence folder reads the sitemap, opens each page in headless Chromium, records any message that mentions the Content Security Policy, and then pokes each interactive tool to make sure its script still runs:
ok 200 /
ok 200 /posts/
ok 200 /posts/spf-too-many-dns-lookups-permerror-fix/
...
ok tool works: /tools/spf-dmarc-checker/
ok tool works: /tools/security-headers-checker/
ok tool works: /tools/github-actions-cron/
ok tool works: /tools/subnet-calculator/
16 pages, 0 violation(s)/failure(s)
A test that can’t fail proves nothing, so there’s also a negative control: a page with an inline script and an inline onerror handler, served under the same policy. Chromium refused both:
inline script ran: false
violations seen: 2
- Executing inline script violates the following Content Security Policy directive 'script-src 'self''. ...
- Executing inline event handler violates the following Content Security Policy directive 'script-src 'self''. ...
I ran the page check twice, once against wrangler pages dev locally and once against a preview deployment on Cloudflare (a non-production branch of the same project). Both gave 0 violations.
After
The response from the preview deployment of the same build:
strict-transport-security: max-age=15552000
content-security-policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; ...
permissions-policy: camera=(), microphone=(), geolocation=(), payment=()
referrer-policy: strict-origin-when-cross-origin
x-content-type-options: nosniff
x-frame-options: DENY
The preview also carried x-robots-tag: noindex, which production doesn’t, so don’t be alarmed when you see it there. Because I didn’t set referrer-policy or x-content-type-options myself, nothing is duplicated. If you do set a header that Cloudflare already adds, run curl -sI and see how the two are combined before you ship.
If your site isn’t this simple
With analytics, fonts, embeds or third-party scripts, don’t start with an enforcing CSP. Send it first as Content-Security-Policy-Report-Only. Chromium logs what it would have blocked, and you can add report-uri or report-to to collect the violations. Add each legitimate source to the right directive (script-src, img-src, connect-src, and so on) rather than to default-src. When you switch to enforcing, keep re-running a browser check after every template change. And raise the HSTS max-age in steps: five minutes, a day, a week, six months.
Check many domains at once
For one site, the steps above are enough. If you manage a list of domains, my Apify Actor (linked below) reads the published security headers along with SPF, DMARC, DKIM, MTA-STS and TLS for the whole list in one run. It only reads public responses, and I haven’t tested it on lists in the thousands.
Reproduce it
run.sh before|after in the evidence folder records the live headers with curl -sI, runs this site’s checker over them, and optionally runs the Chromium check. The Chromium scripts need Playwright installed and PW_DIR pointing at it.
Related
- Email is the other half of domain security: DMARC rollout and SPF lookup limit.
- HTTPS and DNS: DNSSEC SERVFAIL test.
- Another tested guide on this site: GitHub Actions script injection and actionlint.