Security Headers Explained: CSP, HSTS, X-Frame-Options

Learn how CSP, HSTS, and X-Frame-Options protect your site from XSS, downgrade attacks, and clickjacking. Use a security headers checker to spot missing or weak headers and apply practical fixes fast.

Security headers are HTTP response headers that harden browsers against common attacks like XSS, clickjacking, downgrade attacks, and data leakage. They are quick wins that reduce risk without changing application code in many cases.

Table of Contents

Quick checklist

Content-Security-Policy (CSP)

What it protects against: CSP reduces the impact of cross-site scripting (XSS), malicious third-party scripts, and unsafe content loading by restricting where resources can be loaded from.

How it works: You define a policy that browsers enforce. The policy describes allowed sources for scripts, styles, images, fonts, frames, and more.

Good starting policy (example):

Key directives to understand:

Common CSP pitfalls:

Report-only mode (recommended rollout):

Strict-Transport-Security (HSTS)

What it protects against: HSTS blocks protocol downgrade attacks and cookie hijacking by forcing browsers to use HTTPS for your domain after the first secure visit.

How it works: When a browser sees HSTS, it caches the rule and refuses to use HTTP for that domain until the max-age expires.

Recommended header (example):

Preload option (only if you meet requirements):

HSTS deployment warnings:

X-Frame-Options (and frame-ancestors)

What it protects against: clickjacking, where an attacker frames your site and tricks users into clicking UI elements.

X-Frame-Options values:

Recommended modern approach: Prefer CSP frame-ancestors because it is more flexible and widely supported in modern browsers.

Examples:

Note: If you set both X-Frame-Options and CSP frame-ancestors, ensure they do not conflict.

Other high-value security headers

About X-XSS-Protection: This is legacy and not recommended as a security control for modern browsers.

Common mistakes and how to avoid them

Security headers checker: what it does and what to look for

A security headers checker fetches your site and analyzes response headers to identify missing protections, weak configurations, and inconsistencies across endpoints.

What a good checker should verify:

What you should do with results:

Practical fixes (by platform)

Rule of thumb: Set broad, safe defaults at the edge, then tighten CSP per app route if needed.

FAQs

Should I use both X-Frame-Options and CSP frame-ancestors?
Yes, you can, but keep them aligned. If you already use frame-ancestors, X-Frame-Options becomes a compatibility layer for older clients.

Will CSP break my site?
It can if deployed in enforce mode without testing. Start with Report-Only, fix violations, then enforce.

Is HSTS safe to enable immediately?
Only if all content and subdomains you plan to include are available over HTTPS. Start without includeSubDomains if you are unsure.

Do security headers replace secure coding?
No. They reduce browser-side attack surface and impact, but they do not eliminate server-side vulnerabilities.