HTTP security headers are the browser-enforced defence layer running between the network and your application code. Nine headers form the modern stack. Each closes a specific class of vulnerability — XSS, clickjacking, downgrade, MIME confusion, information leakage, cross-origin attacks.
This guide covers what each header does, safe defaults, deployment order, and the common ways they get misconfigured.
The Nine Headers
| Header | Attack it blocks | Difficulty |
|---|---|---|
| HSTS | HTTPS downgrade | Easy |
| CSP | XSS, data injection | Hard |
| X-Frame-Options | Clickjacking | Easy |
| X-Content-Type-Options | MIME sniffing | Trivial |
| Referrer-Policy | URL token leakage | Easy |
| Permissions-Policy | Unauthorized API access | Medium |
| SRI | CDN tampering | Medium |
| CORS | Cross-origin data theft | Medium |
| COOP + COEP | Spectre-class attacks | Hard |
Deployment Order
Deploy in this order — easiest and lowest risk first:
X-Content-Type-Options: nosniff— no configuration, no side effects, ship todayStrict-Transport-Security: max-age=31536000; includeSubDomains— verify HTTPS everywhere firstReferrer-Policy: strict-origin-when-cross-origin— modern default, minor observability changesX-Frame-Options: DENY— unless you actually get embedded in iframesPermissions-Policy— deny every feature you don’t useContent-Security-Policy— hardest to get right; deploy in Report-Only mode firstCOOP + COEP— only if you need SharedArrayBuffer
The Minimum Viable Stack
If you ship nothing else, ship these five:
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()
Five lines. Zero configuration. Blocks the majority of easy-to-exploit browser-side vulnerabilities.
The CSP Question
Content-Security-Policy is the highest-leverage header — and the one most likely to break your site on first deployment. Two-phase rollout:
Phase 1: Report-Only
Content-Security-Policy-Report-Only:
default-src 'self';
script-src 'self' https://cdn.example.com;
style-src 'self' 'unsafe-inline';
img-src 'self' data: https:;
frame-ancestors 'none';
report-uri /csp-report;
Run this for a week. Every violation reports to /csp-report. Review reports. Add legitimate sources. Remove or self-host the rest.
Phase 2: Enforce
Change the header to Content-Security-Policy: (drop -Report-Only). Test in staging. Monitor for a day. Ship to production.
HSTS Preload
If you’re serving HTTPS reliably on every subdomain, add preload to HSTS and submit to hstspreload.org:
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
Preloaded domains ship HSTS-enabled in Chrome, Firefox, Safari, and Edge — no first visit needed. Once you’re on the list, you’re committed for a long time; removing is slow.
Cookies Are Part of This
Two attributes go on every cookie:
Set-Cookie: session=abc; Secure; HttpOnly; SameSite=Lax
Secure— only send over HTTPSHttpOnly— hide from JavaScriptSameSite=Lax— modern CSRF defence (Strict for high-security)
See the Cookie SameSite glossary entry for the trade-offs.
Common Mistakes
'unsafe-inline'inscript-src— kills the majority of CSP’s XSS protection. If you’re stuck with inline scripts, use nonces ('nonce-abc123') instead.X-Frame-Options: SAMEORIGINwith cross-origin domains — breaks legitimate embeds silently.- HSTS
max-age=0to disable — users who already cached the policy keep it until it expires. HSTS is a one-way street. - CSP without
report-uri/report-to— you’ll never know what’s blocked. - Setting security headers only on some routes — attackers hit the routes without them.
Testing
Run the security headers audit to see:
- Which headers are set on your domain
- Which values are safe / recommended / dangerous
- A prioritized fix list
The tool covers every header in this guide plus modern additions (COOP, COEP, Reporting-Endpoints).
Related
Check the HSTS glossary entry, the CSP entry, and the X-Frame-Options entry for the three most-load-bearing headers.
Common Questions
Which security headers should every site set?
Minimum viable stack: (1) Strict-Transport-Security with max-age=31536000, (2) X-Content-Type-Options: nosniff, (3) Referrer-Policy: strict-origin-when-cross-origin, (4) X-Frame-Options: DENY or CSP frame-ancestors 'none'. Add Content-Security-Policy once you know your resource origins. Add Permissions-Policy denying unused features (camera, microphone, geolocation). This 5-header stack blocks 80% of the common browser-side attack classes.
What is the difference between X-Frame-Options and CSP frame-ancestors?
Both control whether the page can be embedded in an iframe. X-Frame-Options (2013) is simpler but limited to DENY, SAMEORIGIN, or (deprecated) ALLOW-FROM one origin. CSP frame-ancestors (2016) allows multiple origins, wildcards, and integrates with the rest of your CSP. Modern deployments ship both — CSP for browsers that support it, X-Frame-Options as a fallback.
Will CSP break my site?
Almost certainly, on first deployment. Every third-party script, style, image, or font hosted on a domain not listed in your CSP will be blocked. Deploy CSP in Report-Only mode first (Content-Security-Policy-Report-Only header) for at least a week. Review violation reports, add legitimate sources to the policy, remove or self-host the rest. Only then flip to enforcing Content-Security-Policy.
Do I need HTTPS to use security headers?
HSTS explicitly requires HTTPS. CSP works over both HTTP and HTTPS but is much less useful over HTTP since an on-path attacker could strip it. Cookies with Secure attribute require HTTPS. In practice: security headers assume HTTPS, and any site not on HTTPS in 2026 has a bigger problem than missing security headers.
How do I test my security headers without breaking production?
Three options: (1) Report-Only variants — Content-Security-Policy-Report-Only, Cross-Origin-Opener-Policy-Report-Only — send violation reports without blocking. (2) Roll out per-endpoint — test on a low-traffic route first. (3) Use our security headers checker to inspect current state before and after deployment. Never ship a new CSP straight to production without a Report-Only bake period.