Skip to Content
Mengty Learns
Security

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.

referencebeginnerTested with asvs 5.0.0 (V3, V12)Updated Nov 26, 2026

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

HeaderRecommended baselinePurpose
Strict-Transport-Securitymax-age=31536000; includeSubDomainsForces HTTPS in the browser after first visit. Add ; preload only when every subdomain is permanently HTTPS.
Content-Security-Policydefault-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'Restricts script/style/connect origins and blocks clickjacking (frame-ancestors).
X-Content-Type-OptionsnosniffDisables browser MIME-type sniffing.
Referrer-Policystrict-origin-when-cross-originSends full path on same-origin, origin-only on cross-origin HTTPS, nothing on downgrade.
Permissions-Policycamera=(), microphone=(), geolocation=()Disables powerful browser APIs your site does not use.
Cross-Origin-Opener-Policysame-originIsolates top-level browsing context from cross-origin popups (Spectre/XS-Leak mitigation).
Cross-Origin-Embedder-Policyrequire-corpOpts into cross-origin isolation (SharedArrayBuffer); only enable if all subresources send CORP/CORS.

Cloudflare Pages _headers example

Repo status: Checked public/ in this repository—no public/_headers file exists yet. Add the file below (after testing CSP in Content-Security-Policy-Report-Only first) 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 preload is 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 + preload will break it for months.
  • COEP breaks third-party embeds: Adding Cross-Origin-Embedder-Policy: require-corp blocks third-party images/iframes that lack Cross-Origin-Resource-Policy: cross-origin or CORS headers.
  • _headers does not apply to Next.js next dev: Cloudflare Pages _headers is only read by Cloudflare's edge on deployed static assets—verify headers in staging with curl -I.

Production checklist

  • X-Content-Type-Options: nosniff and Referrer-Policy are set on all responses.
  • Clickjacking is blocked with CSP frame-ancestors 'none' (or 'self').
  • Strict-Transport-Security starts with a short max-age (e.g. 300) before ramping to 31536000; includeSubDomains.
  • CSP is tested in Report-Only mode 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