You write a Content-Security-Policy, deploy it, open the site, and the browser enforces it. Then one day you look at the response headers on the production domain and see a different policy. No error, no warning, and both URLs answer 200.
This happened on our own site, kubmeta.com. The cause was a rule we had created ourselves, earlier, in the Cloudflare dashboard. While looking for it, we found a second place where the edge changes what we ship.
The setup
kubmeta.com is a static site on Cloudflare Pages, built from a git repository. Pages reads response headers from a _headers file in the build output. Our first version:
# HSTS and nosniff come from the Cloudflare zone settings: do not duplicate them here.
/*
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=(), interest-cohort=()
Content-Security-Policy: default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'
The site had no JavaScript at all, hence script-src 'none'. Note the comment on top: for HSTS and nosniff we had already decided which layer owns them. For the other four headers we had not, and that is the whole story.
The Cloudflare zone itself is managed with Terraform (how we moved it there).
The calendar that said “Something went wrong”
On 29 November we embedded a Cal.com booking calendar on /discuss/. The embed loads a script from app.cal.com and renders the calendar in an iframe, so the policy had to allow that origin in script-src, frame-src and connect-src. Even then, the calendar showed “Something went wrong” instead of time slots.
The cause was style-src 'self'. The embed adds inline styles to the host page, and the policy blocked them. The fix was to allow inline styles, and only styles:
Content-Security-Policy: default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' https://app.cal.com; frame-src https://app.cal.com; connect-src 'self' https://app.cal.com; base-uri 'none'; form-action 'none'; frame-ancestors 'none'
'unsafe-inline' in style-src is a much smaller risk than in script-src. Scripts stay limited to our origin and the calendar. And with img-src restricted to 'self' and data:, injected CSS cannot load images from someone else’s server, the usual trick for leaking data through CSS. When a policy blocks something, the browser console names the directive that did it, so start there.
The policy we wrote was not the policy served
While checking the fix, we compared the headers on the custom domain with those on the Pages project’s own *.pages.dev address. It is the same deployment, so the headers should be the same:
curl -sI https://kubmeta.com/discuss/
curl -sI https://<project>.pages.dev/discuss/
They were not (output shortened):
# https://kubmeta.com/discuss/
content-security-policy: base-uri 'self'
permissions-policy: accelerometer=(), ambient-light-sensor=(), bluetooth=(), camera=(self), gyroscope=(), …, microphone=(self), …
referrer-policy: same-origin
x-frame-options: SAMEORIGIN
# https://<project>.pages.dev/discuss/
content-security-policy: default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' https://app.cal.com; …
permissions-policy: camera=(), microphone=(), geolocation=(), interest-cohort=()
referrer-policy: strict-origin-when-cross-origin
x-frame-options: DENY
All four headers from _headers were different on kubmeta.com. The values there came from four zone-level response header transform rules. They had been created in the Cloudflare dashboard before the site moved to Pages, when the domain still served an older site, and we had imported them into Terraform exactly as they were:
response_headers = [
{ ref = "<rule-id>", name = "X-Frame-Options", header = "X-Frame-Options", value = "SAMEORIGIN" },
{ ref = "<rule-id>", name = "Permissions-Policy", header = "Permissions-Policy", value = "accelerometer=(), …" },
{ ref = "<rule-id>", name = "content-security-policy", header = "Content-Security-Policy", value = "base-uri 'self'" },
{ ref = "<rule-id>", name = "Referrer-Policy", header = "Referrer-Policy", value = "same-origin" },
]
Our module turns each entry into a rule with the expression true, meaning every request in the zone, and the operation set. “Set” overwrites a header with the same name coming from the origin. For a custom domain, Pages is the origin: it applies _headers, then the zone rules replace those values. The *.pages.dev hostname is not part of our zone, so no zone rule runs there.
What that meant in practice:
- The strict CSP protected only the pages.dev copy, which is closed to search engines and is not where visitors go. The production domain had
base-uri 'self'and nothing else: effectively no content policy. frame-ancestors 'none'andDENYbecameSAMEORIGIN, and camera and microphone were allowed for our own origin. Harmless for a static site, but not what we had decided.- Behavior depended on the URL. A CSP bug like the calendar one shows up on one address and not on the other, so a test on the preview URL tells you about the preview URL only.
Nothing warned us. No error, no log line, 200 on both.
At the time of writing, the zone rules still win on kubmeta.com. Deleting them is not a one-line fix: they match every request in the zone, including self-hosted apps behind tunnels on other subdomains, which may rely on them. With the same module, one way to keep both is to scope the zone rules away from the Pages hostnames:
{
ref = "<rule-id>"
name = "content-security-policy"
header = "Content-Security-Policy"
value = "base-uri 'self'"
expression = "not (http.host in {\"kubmeta.com\" \"www.kubmeta.com\"})"
},
The _headers policy is ready to take over on the site: it already allows the calendar.
The edge rewrote our HTML too
While comparing the two URLs, we also compared the HTML. The pages.dev copy was byte for byte the file in git. The custom domain’s was not:
curl -s https://kubmeta.com/discuss/ | diff -u dist/discuss/index.html -
- <a href="mailto:<address>"><address></a>
+ <a href="/cdn-cgi/l/email-protection#a3cb…"><span class="__cf_email__" data-cfemail="7018…">[email protected]</span></a>
-</body>
+<script data-cfasync="false" src="/cdn-cgi/scripts/<hash>/cloudflare-static/email-decode.min.js"></script></body>
This is Cloudflare’s Email Address Obfuscation, a zone setting, and on our zone it was on. It finds email addresses and mailto: links in the HTML, replaces them with encoded values and links to /cdn-cgi/l/email-protection, and injects a small script that decodes them in the browser. Every mailto: link on the site was rewritten, and the script was added to every page.
The idea is reasonable: scrapers that read raw HTML do not see the address. The side effects:
- The live HTML no longer matches the build. Any check that compares deployed pages with the build output fails, and you have to know why.
- A script we did not write runs on every page. It is served from our own origin, so
script-src 'self'allows it. Under our originalscript-src 'none', it would have been blocked and the links would have stayed pointed at/cdn-cgi/l/email-protectioninstead of opening a mail client, had that policy been in force on the custom domain. - Without JavaScript, the address is gone. Tools that read raw HTML, and visitors with scripts off, see “[email protected]”.
- It happens only on the custom domain. Preview URLs hide it, just like the header rules.
We decided to keep it. The address is public for people, not for scrapers, and the side effects are easy to live with once you know about them: when we compare the live site with the build, we expect exactly these lines to differ. What matters is that the decision is written down. In our module it is one more entry in the zone’s settings map, email_obfuscation = "on", so nobody switches it off by accident in the dashboard. If a page ever needs the plain address, Cloudflare configuration rules can turn the feature off for a single hostname.
It is not the only edge feature that edits HTML. Automatic HTTPS Rewrites, which we turn on in all our zones on purpose, rewrites http:// links to https://. Know which of these features are on in your zone.
What we took from it
One owner per header. Decide which layer sets each header, and write it down next to the header. Our _headers file already did that for HSTS and nosniff; the other four needed the same line. A split that works: the zone owns transport headers such as HSTS, and the application owns content headers such as CSP, framing and permissions, because only the application knows what its pages load.
Compare the custom domain with the pages.dev URL after every header change. An empty diff means the edge did not touch them:
h='^(content-security-policy|x-frame-options|referrer-policy|permissions-policy):'
diff <(curl -sI https://kubmeta.com/ | grep -iE "$h" | sort) \
<(curl -sI https://<project>.pages.dev/ | grep -iE "$h" | sort)
Diff the live HTML against the build output. Any difference you did not write was made by the edge.
Keep edge rules in Terraform. The rule was in our infrastructure repository, with a comment saying where it came from, so a grep finds it instead of a tour of dashboard pages. Code also shows the reach of a rule: the default expression true reads plainly as “every host in the zone”.
Test where your users are. A preview URL is fine for content. Header rules and HTML rewrites only show up on the production hostname.
Nothing here was a Cloudflare bug. Every layer did exactly what it was told. The problem was that two layers had been told different things, and only one of them was visible from the repository the site lives in. If you want a second pair of eyes on what your edge does to your responses, get in touch.
