Why Cloudflare was removing our security headers
Your server sends a CSP but visitors never get it? How we traced missing security headers to an old Cloudflare Worker route that Trace didn't show.

You've written a Content-Security-Policy, checked it in your server's configuration and deployed it. Then a header scanner tells you your site has no CSP at all. You look again at the config and the policy is right there.
If your site sits behind Cloudflare, or any proxy or CDN, the headers your server sends and the headers your visitors receive are two different things. Anything between the two can add, change or remove them, and nothing in your own logs will show it. A security header that never reaches the browser protects nobody.
That happened to us in October 2026, and it took several wrong guesses to find. Here's how we narrowed it down, why Cloudflare's own tracing tool didn't help, and the earlier, quieter way the same header had vanished inside nginx. If your headers change somewhere between your server and the browser, the method here will find where.
The server sent one set of headers, and visitors got another#
Our website is static files served by nginx, with Cloudflare in front. On 3 October 2026, nginx sent a full set of security headers: an enforced Content-Security-Policy (CSP) allowing two inline scripts by their SHA-256 hashes, a Permissions-Policy, Cross-Origin-Opener-Policy, and Referrer-Policy: strict-origin-when-cross-origin.
Our own website checkup, run against our own site, said "Content security policy: not set". A scan of the home page with Cloudflare's URL Scanner the same afternoon showed what a browser was actually given:
| Home page, 3 October 2026 | Our server sent | Visitors received |
|---|---|---|
| Content-Security-Policy | Enforced, with two hashes | Missing |
| Permissions-Policy | Set | Missing |
| Referrer-Policy | strict-origin-when-cross-origin | same-origin |
| X-XSS-Protection | Not sent | 1; mode=block |
| Expect-CT | Not sent | max-age=86400, enforce |
| X-Visitor-Country | Not sent | US |
Two headers removed, one rewritten and three added. Our nginx configuration couldn't explain any of it, so the change was happening on the way out through Cloudflare. The last row turned out to be the answer, and we didn't notice it for another day.
Compare the origin with the edge before you change anything#
The first job is to prove where the change happens, because the tempting fix, adding a CSP somewhere else, would have left us with two policies and no idea which one a browser was obeying. So we fetch the same URL twice: once straight from the origin server, skipping Cloudflare, and once through Cloudflare the way a visitor would.
curl's --resolve option does the first part. It sends the request to an address you choose while keeping the real hostname, so TLS and nginx's server_name matching behave exactly as they do for a visitor:
# What your server sends: straight to the origin, past Cloudflare
curl -sk -o /dev/null -D - \
--resolve www.example.com:443:ORIGIN_IP https://www.example.com/
# What visitors get: through Cloudflare
curl -s -o /dev/null -D - https://www.example.com/
The -k is there because a Cloudflare Origin CA certificate is trusted by Cloudflare, not by your computer. Two cautions. If your zone challenges automated clients, the second request can come back as a Cloudflare challenge page, with Cloudflare's own CSP on it and none of yours, so check the status code and body before reading the headers. And put the two outputs side by side, sorted, rather than checking for the one header you're worried about. The extra headers are evidence too.
One Cloudflare setting explained half the difference#
Two of the three added headers, and the rewritten Referrer-Policy, matched a Cloudflare feature exactly. Managed Transforms include one called "Add security headers", and Cloudflare's reference lists what it adds: x-content-type-options: nosniff, x-xss-protection: 1; mode=block, x-frame-options: SAMEORIGIN, referrer-policy: same-origin and expect-ct: max-age=86400, enforce. That's the rewritten Referrer-Policy and both of the headers we never sent.
Neither addition helps a modern site. MDN marks X-XSS-Protection as deprecated and non-standard, warns that it can create XSS vulnerabilities in otherwise safe websites, and recommends a CSP instead. Expect-CT is deprecated too, since Chromium now enforces Certificate Transparency by default. We turned the transform off, and on 4 October the home page, a sales page and the member sign-in all arrived with our own Referrer-Policy and without the two old headers.
But nothing in that transform removes headers. Something else was taking out the CSP and the Permissions-Policy.
A query string made the problem disappear#
On the evening of 3 October a check from a browser showed the CSP arriving, and we reported it fixed. The next morning our checkup still said "not set". The checkup wasn't wrong. The browser check had added a cache-busting query string to the URL, and that turned out to matter more than anything else we'd tested.
Fetching the home page three ways made the disagreement plain:
| Home page, 4 October 2026 | CSP | Permissions-Policy | X-Visitor-Country |
|---|---|---|---|
| Our server directly | Yes | Yes | None |
| A browser through Cloudflare, URL with a query string | Yes | Yes | None |
| Our checker through Cloudflare, bare URL | No | No | GB |
That's where the extra header finally got the attention it deserved. Nobody had set an x-visitor-country header on purpose. It wasn't in our repository, in the live nginx configuration or in the member area's code, and the origin didn't send it however it was asked. Cloudflare's own country header, CF-IPCountry, is added to the request on its way to your server, not to the response. So whatever added x-visitor-country had been written by somebody, and it was probably the same thing removing our two headers.
The next step was to vary one thing at a time, through Cloudflare, and count the CSP headers in each response:
for path in / '/?x=1' /index.html /robots.txt /wordpress-hosting/; do
printf '%-22s csp=%s\n' "$path" \
"$(curl -s -o /dev/null -D - "https://www.example.com$path" | grep -ci '^content-security-policy')"
done
| Through Cloudflare, 4 October 2026 | CSP | Permissions-Policy | X-Visitor-Country |
|---|---|---|---|
/ | No | No | GB |
/?x=1 | Yes | Yes | None |
/index.html | Yes | Yes | None |
/robots.txt | Yes | Yes | None |
/wordpress-hosting/ | Yes | Yes | None |
The bare home page lost its headers over HTTP/1.1 and HTTP/2, over IPv4 and IPv6, with and without compression, from a browser and from curl. Every other URL kept them. The body was the same in every case: the bare URL, the query-string URL and the origin's own response were all 65,773 bytes with the same MD5 checksum. So whatever was responsible changed only headers, and only when the path was exactly / with nothing after it.
One more test settled whether nginx was involved. We sent a request through Cloudflare with a unique marker in its user agent, then searched the origin's access log for it:
M="header-probe-$(date +%s)"
curl -s -o /dev/null -D - -A "Mozilla/5.0 (compatible; $M)" https://www.example.com/
sleep 10 # a buffered access log only writes every few seconds
grep "$M" /var/log/nginx/access.log
The request was there, answered with a normal 200. nginx had sent the headers, and they were gone by the time the response reached us.
Cloudflare Trace showed nothing, because the cause wasn't a rule#
A change that applies to one exact URL sounds like a rule with a condition such as "URI path equals /". Cloudflare has a tool for finding exactly that. Cloudflare Trace simulates a request through Cloudflare's network and lists which rules and settings match it, phase by phase. We traced the bare home page URL.
The trace showed no match in the response header modification phase, and no Worker or Snippet. We also ruled out Cloudflare's content security rules, which manage CSP at the edge, because they aren't available on our plan. Trace's list of limitations names the products it won't show, and Workers aren't on it. In our case, though, a Worker was exactly what it didn't show.
Workers are code, and a Worker attached to a route can rewrite any response that route matches. Routes are managed apart from the rules pages, so they're easy to miss when you're searching the dashboard by eye. The zone's Workers Routes held an old route with a Worker attached, one nobody had thought to check. It added x-visitor-country and dropped the CSP and Permissions-Policy, on that one URL.
Cloudflare's documentation on route patterns explains the odd shape of the symptoms. A pattern with no path implies / and matches that URL "and nothing else", and query strings are part of what's matched, so only a pattern ending in * catches them. A route like that matches the bare home page and misses /?x=1, which is precisely what we measured.
The fix was deleting one old Worker#
We removed the Worker on 4 October 2026. Through Cloudflare, the bare home page then arrived with the CSP and the Permissions-Policy, x-visitor-country was gone, and the other URLs were unchanged. Our full checkup of our own site scored 36 checks good, none to warn about and none bad.
The clue had been in the first scan all along. On 3 October, Cloudflare's URL Scanner showed X-Visitor-Country: US, in the first table above. We read the headers that were wrong and skipped the one that shouldn't have been there at all.
The same CSP had vanished once before, inside nginx#
This wasn't the first time our CSP had gone missing without an error. When we first added it, on 7 September 2026, we wrote it the way you'd write a long value in a shell script, one directive per line:
# Broken: the backslashes and line breaks become part of the value
add_header Content-Security-Policy
"default-src 'self'; \
script-src 'self'; \
object-src 'none'" always;
# Works: one line
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'" always;
nginx reads that without complaint. But nginx has no backslash line-continuation. Its configuration parser lets a quoted string run across lines, and a backslash followed by a line break isn't one of the escapes it recognises, so both are copied into the value. The result is a header value with line breaks in it, which HTTP's own specification calls invalid.
The policy never reached the browser. Fetched from the origin with curl, the response carried X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Strict-Transport-Security, Permissions-Policy and the rest, but no usable Content-Security-Policy: we saw no CSP line at all. Depending on the protocol and the client, you might instead see a cut-off line or an error, so treat any of those as the same symptom. Every other header survived because each was already written on one line. We put the policy on one line the same afternoon, with a comment above it saying why, so nobody "tidies" it back. Our deployment notes now say that if the CSP is the one header missing from the origin's response, the likeliest cause is a policy split across lines.
A related nginx behaviour catches people in the same way. According to nginx's add_header documentation, headers are inherited from the level above "if and only if there are no add_header directives defined on the current level". By default, a location block that adds any header of its own with add_header, Cache-Control say, silently drops every security header set on the server block. (nginx 1.29.3 added an add_header_inherit directive that can merge them instead; most servers run an older version.) On 11 September 2026 we found our cart, checkout handoff, sign-in form and member area missing three security headers for exactly that reason. We now keep the shared headers in a snippet that every such block includes.
Allow inline scripts by hash, never with 'unsafe-inline'#
At the time, our pages ran two small inline scripts: Plausible's analytics starter, which has to define its queue before the tracker loads, and a one-line check that switches "Sign in" to "My account" before the page is drawn. The easy way to let them run is 'unsafe-inline'. MDN's script-src reference explains why not: it allows every inline script, and allowing all inline scripts is considered a security risk. That includes any script an attacker manages to inject, which is the attack a CSP is mostly there to stop.
A hash source allows one script and nothing else. The browser hashes the text between the <script> tags and runs it only if the result is in the policy:
# In nginx, on one line
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'sha256-HASH_OF_SCRIPT_ONE' 'sha256-HASH_OF_SCRIPT_TWO'; object-src 'none'; base-uri 'self'" always;
# Working out a hash by hand: save the exact bytes between the tags to a file first
openssl dgst -sha256 -binary script.txt | openssl base64 -A
Hash a file rather than a shell variable. Capture the script with $(...) and the shell quietly strips its trailing newlines, so the hash comes out wrong and the browser blocks the script without telling anyone why.
The catch is that the hash follows the bytes exactly. MDN notes that capitalisation and whitespace matter, including leading and trailing whitespace, so re-indenting the script counts as changing it. When the hash and the script disagree, the browser blocks the script and logs a console message. The page still renders, so from the outside nothing looks wrong. For our sign-in check, that would mean the header saying "Sign in" to people who already are, indefinitely.
So our build does the checking. After it writes every page, it finds each inline script, hashes it the way a browser does and looks for that hash in the nginx configuration that the deploy ships. A missing hash stops the build:
for body in pattern.findall(html):
digest = base64.b64encode(hashlib.sha256(body.encode()).digest()).decode()
if ("sha256-" + digest) not in conf:
missing.setdefault(digest, (path, " ".join(body.split())[:70]))
# ...then print each missing 'sha256-...' value, where it was found, and exit(1)
That came straight into use on 11 October, when we started serving Plausible through our own domain. The starter script changed, the build refused to run and printed the new hash, and the policy was updated in the same change.
Check the policy at the build, the origin and the edge#
- At build time, every inline script must have its hash in the policy, or nothing is built.
- After any change to the nginx configuration, our deployment notes check the origin with
--resolve, so we see what nginx sends without Cloudflare in the way. - Through Cloudflare, when a header looks wrong, we compare the bare home page with the same page plus a query string, and with a deeper page. If those three disagree, something at the edge is matching on the URL.
- From outside, we run our own website checkup against our own site. It reads what a visitor gets, not what the configuration says.
What we learned#
- Prove where a header changes before you fix it. Fetch the same URL from the origin and through the edge, and compare the full lists. Adding a second CSP somewhere else would have hidden the problem rather than solved it.
- Read the headers that shouldn't be there. A header you never set is a fingerprint of whatever set it. Ours was in the first scan and we read past it until the next morning.
- Test the exact URL, without cache-busters. A query string changes which edge rules and routes match. Our "it's fixed" came from a URL the problem didn't apply to.
- Check Workers Routes early. Trace simulates rules and settings, and it didn't show our Worker. If headers differ between
/and/?x=1, look at the zone's routes before anything else. - Write each nginx header on one line, then confirm it arrived. nginx accepts a multi-line value without an error and sends a malformed header that clients may drop or misread.
- Remember add_header doesn't merge, by default. Any
add_headerin a block discards the inherited ones, so include your security headers wherever you set another, or on nginx 1.29.3 and later setadd_header_inherit merge;. - Use hashes, not 'unsafe-inline', and make the build enforce them. A blocked script fails quietly, so the check has to be somewhere that can't be ignored.
- Clear out what nobody can explain. One old Worker undid a policy we'd written with care. Every rule, route and transform on a zone should have someone who can say why it's there.
If you'd like to see what your own site sends to visitors, our free website checkup reads the headers a browser actually receives, and our guide to the security headers your site should send explains what each one does. If the two disagree with your configuration and you'd like help finding out why, tell us what you're seeing.


