HTTP Security Headers Cheatsheet
Practical reference for CSP, HSTS (and preload caveats), X-Content-Type-Options, Referrer-Policy, Permissions-Policy, frame-ancestors, and COOP/COEP using Cloudflare Pages _headers.
TL;DR
HTTP security headers instruct browsers to lock down framing, MIME sniffing, downgrades to HTTP, referrer leaks, and script execution. On Cloudflare Pages, you ship them via public/_headers—and this repo does not have a public/_headers file yet (checked public/), so the example below is the target config to add before deploying.
Header reference
| Header | Recommended baseline | Purpose |
|---|---|---|
Strict-Transport-Security | max-age=31536000; includeSubDomains | Forces HTTPS in the browser after first visit. Add ; preload only when every subdomain is permanently HTTPS. |
Content-Security-Policy | default-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self' | Restricts script/style/connect origins and blocks clickjacking (frame-ancestors). |
X-Content-Type-Options | nosniff | Disables browser MIME-type sniffing. |
Referrer-Policy | strict-origin-when-cross-origin | Sends full path on same-origin, origin-only on cross-origin HTTPS, nothing on downgrade. |
Permissions-Policy | camera=(), microphone=(), geolocation=() | Disables powerful browser APIs your site does not use. |
Cross-Origin-Opener-Policy | same-origin | Isolates top-level browsing context from cross-origin popups (Spectre/XS-Leak mitigation). |
Cross-Origin-Embedder-Policy | require-corp | Opts into cross-origin isolation (SharedArrayBuffer); only enable if all subresources send CORP/CORS. |
Cloudflare Pages _headers example
Repo status: Checked
public/in this repository—nopublic/_headersfile exists yet. Add the file below (after testing CSP inContent-Security-Policy-Report-Onlyfirst) when deploying to Cloudflare Pages.
# public/_headers (example — not yet present in this repo)
/*
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
Cross-Origin-Opener-Policy: same-origin
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; object-src 'none'; frame-ancestors 'none'; base-uri 'self'Gotchas
- HSTS
preloadis hard to undo: Submitting to the HSTS preload list bakes your domain into browser binaries. If a legacy internal subdomain still uses plain HTTP,includeSubDomains+preloadwill break it for months. - COEP breaks third-party embeds: Adding
Cross-Origin-Embedder-Policy: require-corpblocks third-party images/iframes that lackCross-Origin-Resource-Policy: cross-originor CORS headers. _headersdoes not apply to Next.jsnext dev: Cloudflare Pages_headersis only read by Cloudflare's edge on deployed static assets—verify headers in staging withcurl -I.
Production checklist
-
X-Content-Type-Options: nosniffandReferrer-Policyare set on all responses. - Clickjacking is blocked with CSP
frame-ancestors 'none'(or'self'). -
Strict-Transport-Securitystarts with a shortmax-age(e.g.300) before ramping to31536000; includeSubDomains. - CSP is tested in
Report-Onlymode before enforcing so inline hydration/styles are not broken.
Check yourself
Check Yourself
1. Why should you test HSTS with a short max-age before enabling includeSubDomains and preload?
2. Which header prevents a browser from executing an uploaded .txt file as HTML or JavaScript?
Sources
Related Notes
CSRF & SSRF Defenses
How Cross-Site Request Forgery (CSRF) and Server-Side Request Forgery (SSRF) work, and how SameSite cookies, anti-CSRF tokens, allowlists, and metadata blocks stop them.
Secure File Uploads & Downloads
How to validate uploaded files by content, enforce size limits, store outside the web root with randomized names, scan for malware, and serve safely with Content-Disposition.