Skip to content

Security headers at the Cloudflare edge: Transform Rules recipes for HSTS, X-Frame-Options, and CSP

A working recipe set for shipping security headers from Cloudflare without touching application code: which headers to set, the exact rule for each, when HSTS belongs in the dashboard instead, and how not to break your own CSP.

You can attach a complete, defensible security-header set to every response from the Cloudflare dashboard in about twenty minutes, with no application deploy: a Response Header Transform Rule for X-Content-Type-Options, Referrer-Policy, Permissions-Policy, and X-Frame-Options, the dashboard HSTS toggle for Strict-Transport-Security, and a Content-Security-Policy rolled out in Report-Only mode first. This article is the recipes; the Transform Rules reference covers how the rule engine itself works.

What’s happening

Response Header Modification rules run at the edge on the way back to the client, which makes them the right home for headers that are policy, not application logic. Security teams get one place to audit, the header survives replatforming, and a bad value can be reverted in seconds instead of waiting on a deploy. The rules support three operations — set (overwrite), add (append another header of the same name), and remove — and each rule can carry several header operations, so the whole baseline fits in one rule.

Plan limits, plainly: Transform Rules exist on every plan including Free, with a per-phase quota — 10 rules on Free, more on paid plans; the current table is in the Rules documentation. One rule with five header operations counts as one rule, which is why the recipes below pack the baseline together. Also note what you cannot touch: edge-owned headers like cf-ray and server: cloudflare are not yours to remove with a Transform Rule.

What to check first

  • What the origin already sends. Run the verification below before writing any rule — if the application already emits a CSP or X-Frame-Options, adding a second copy at the edge creates duplicates, and duplicate security headers behave unpredictably (browsers take the most restrictive, or reject outright).
  • Whether anything legitimately frames your site (embedded widgets, a customer portal in an iframe) before you ship X-Frame-Options.
  • Whether any subdomain still serves plain HTTP before you touch HSTS with includeSubDomains.

How to verify

# What does the site send today, and through which path?
curl -sI "https://example.com/" | grep -iE \
  "strict-transport|content-security|x-frame|x-content-type|referrer-policy|permissions-policy|cf-ray|cf-cache-status"

# Compare with the origin directly - is a header coming from the app or the edge?
curl -sI --resolve example.com:443:<ORIGIN_IP> "https://example.com/" | grep -iE \
  "strict-transport|content-security|x-frame|x-content-type|referrer-policy|permissions-policy"

# Duplicate check: count occurrences, not just presence
curl -sI "https://example.com/" | grep -ic "content-security-policy"

If a header appears in the proxied response but not the direct one, the edge added it. If it appears twice through the proxy, you have an origin copy and an edge copy — fix that before anything else.

The fix

  1. Ship the baseline in one Response Header Transform Rule. Rules » Overview » create a Response Header Transform Rule (the dashboard groups Transform Rules under Rules), match (http.host eq "example.com"), then add four set operations:

    X-Content-Type-Options: nosniff
    Referrer-Policy: strict-origin-when-cross-origin
    Permissions-Policy: camera=(), microphone=(), geolocation=()
    X-Frame-Options: SAMEORIGIN

    SAMEORIGIN keeps your own admin previews working; use DENY only when nothing frames the site, ever. Modern browsers prefer CSP frame-ancestors, but X-Frame-Options still costs nothing and covers older clients.

  2. Do HSTS in the dashboard, not in a Transform Rule. SSL/TLS » Edge Certificates » HTTP Strict Transport Security makes you acknowledge the lock-in (max-age, includeSubDomains, preload) explicitly, which is the point — a mistyped HSTS header in a Transform Rule locks browsers onto HTTPS just as hard, without the guardrails. Start with a short max-age (one day), confirm nothing broke, then raise it. The full ramp-up is covered in HSTS and Always Use HTTPS at the edge.

  3. Roll CSP out in Report-Only first. Add a second rule (or another operation on the first) setting Content-Security-Policy-Report-Only with a starter policy:

    default-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self' 'unsafe-inline'; frame-ancestors 'self'

    Watch the browser console (or a report-to endpoint) for a week, widen the policy until legitimate assets stop violating it, then rename the header to Content-Security-Policy. Shipping an enforcing CSP on day one is how you take down your own checkout.

  4. Remove the chatty headers while you are in there. A remove operation for X-Powered-By and, if the app leaks it, X-AspNet-Version, costs nothing. Cloudflare also offers a one-toggle Managed Transform for removing X-Powered-By under Rules » Settings if you would rather not spend a rule on it.

  5. Scope exceptions with the filter, not with weaker global policy. An embed path that must be framed by a partner gets its own rule — (http.request.uri.path eq "/embed/widget") with a laxer frame-ancestors — while the site-wide rule stays strict. Order matters: put the specific rule above the general one.

When this fix is wrong

If your application already manages a CSP with per-request nonces, leave CSP at the origin — a static edge header cannot mint nonces, and overwriting the origin’s policy will break every inline script the nonce was protecting. Same logic for SaaS platforms that manage their own headers: adding a second opinion at the edge creates duplicates, not defense. And if you need per-response logic (different policy per user role), that is application territory, not a Transform Rule.

Common mistakes

  • Using set when the origin also sends the header. Set overwrites cleanly — that is usually what you want. The mistake is not deciding: half the headers owned by the app, half by the edge, and nobody sure which wins where.
  • Enforcing CSP without a Report-Only period. The console fills with violations from your own analytics, fonts, and inline styles within minutes.
  • HSTS with includeSubDomains before auditing subdomains. One forgotten intranet host on plain HTTP becomes unreachable for every browser that saw the header.
  • Forgetting cached copies. Responses already in the edge cache were stored before your rule changed; purge the cache after a header change if it must be visible everywhere immediately.

Prevention

Keep the header inventory written down — which header, which value, owned by origin or edge — and re-run the two curl checks from this page after any replatforming. A quarterly look at the browser console on your busiest page catches CSP drift before customers do. If “is it Cloudflare or is it us” keeps eating your evenings, that triage is literally the job of 24/7 Monitoring & Response — book a monitoring and response review.