---
description: Cloudflare Pages / Workers static assets: security headers with the _headers file (placement, rule order, detach, limits, Functions/SSR caveat, testing)
globs: **/_headers, **/wrangler.toml, **/wrangler.json, **/wrangler.jsonc, functions/**, **/_worker.js
alwaysApply: false
---
# Cloudflare _headers rules

This project deploys to Cloudflare Pages (or Workers with static assets). Checked against Cloudflare's Pages and Workers static-assets headers docs on 2026-11-16.
If you are unsure whether a behavior still holds, read https://developers.cloudflare.com/pages/configuration/headers/ instead of guessing.

## Where the file goes
- The file is named exactly `_headers` (no extension). It must end up in the deployed static asset / build output directory.
- With a framework, put it in `public/` (or `static/`) so the build copies it into the output. With no framework, put it straight in the folder that gets deployed.
- Cloudflare parses it and does not serve it. Don't link to it or reference it from code.
- Don't put security headers in `netlify.toml`, `vercel.json` or `next.config` for this project; they do nothing on Cloudflare.

## Syntax
- Each block: first line is a URL or pattern (`/*` = everything), then indented `Name: value` lines. Lines starting with `#` are comments.
- Only one `*` splat per pattern. Named placeholders like `:project` are allowed; each placeholder can be used once.
- Absolute URLs must start with `https` and can't include a port.
- Limits: at most 100 header rules, and each line at most 2,000 characters (spacing, name and value all count). Keep the CSP on one line and under that limit.

## Rule order and repeated headers (the common bug)
- Every rule whose pattern matches applies. Rules run in file order.
- If two matching rules set the same header, Cloudflare joins the values with a comma. Never define the same security header (CSP, X-Frame-Options, Cache-Control, HSTS) in two rules that can match the same URL.
- To give a narrower path a different value: put the narrow rule AFTER the broad one, detach with `! Header-Name`, then set the new value. A detach placed above the broad rule does nothing.
- When a URL matches both `_redirects` and `_headers`, the redirect wins.

## Functions, SSR and Worker code
- `_headers` does NOT apply to responses generated by Pages Functions (`functions/` or an advanced-mode `_worker.js`) or by Worker code (including `assets.run_worker_first`), even if the URL matches.
- If the app renders on the server (SSR adapters, API routes in Functions), set security headers on the `Response` in that code too, including CORS on `OPTIONS` responses.
- Before claiming headers are "done", list which routes are static and which come from Functions/Worker code.

## Default security block (adjust, don't paste blindly)
- Baseline for `/*`: `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, `Referrer-Policy: strict-origin-when-cross-origin`, a `Permissions-Policy` that turns off features the app doesn't use, and a `Content-Security-Policy` with `frame-ancestors 'none'`.
- Add every outside origin the app really loads (analytics, Stripe, Supabase, Firebase, fonts) to the matching CSP directive, or those requests get blocked. Ask the user which services they use instead of guessing.
- Ship a new CSP as `Content-Security-Policy-Report-Only` first, click through the app with devtools open, fix what it reports, then switch to the enforcing header.
- HSTS: add `includeSubDomains; preload` only if every subdomain serves HTTPS. If HSTS is already on in the Cloudflare dashboard, set it in one place only.
- To keep the `*.pages.dev` copy out of search results once a custom domain exists: `https://:project.pages.dev/*` with `X-Robots-Tag: noindex`.

## Verify, don't assume
- After deploy, check real responses: `curl -sI https://<site> | grep -iE "content-security|x-frame|x-content-type|referrer|permissions|strict-transport"`.
- Check one static file and one Functions/SSR route separately. Report any header that shows up twice or comma-joined.
- Never say headers are fixed based on the file alone.
