Security Response Headers: HSTS, CSP and X-Frame-Options Explained
Adding a handful of response headers is the highest return-on-effort hardening step a site can take: it costs almost nothing and removes entire classes of common attacks — downgrade attacks, XSS, clickjacking and MIME sniffing.
Below, each header is grouped by priority: must have, strongly recommended, and optional — followed by how to check whether you already have them.
The three you must set
- Strict-Transport-Security (HSTS): tells browsers to use HTTPS only from now on, eliminating downgrade attacks on first visit. Use a max-age of at least 15552000 (180 days).
- X-Content-Type-Options: nosniff: stops browsers from guessing a file's type, which prevents a text file being executed as a script.
- X-Frame-Options (or CSP frame-ancestors): blocks your pages from being embedded in an iframe, preventing clickjacking.
Strongly recommended: CSP
Content-Security-Policy declares which origins a page may load scripts, styles, images and APIs from. Done right, even if an attacker injects a script into your page, it simply will not load.
Roll it out with Content-Security-Policy-Report-Only first, watch the reports for collateral damage, then switch to enforcement — otherwise you risk blocking your own legitimate resources on day one.
A few more worth adding
- Referrer-Policy: controls how much origin information leaks on cross-site navigation.
- Permissions-Policy: switches off browser features you do not use, such as camera or geolocation.
- Cross-Origin-Opener-Policy / Cross-Origin-Resource-Policy: isolates cross-origin contexts and shrinks the side-channel surface.
How to verify what you have
Open the Network tab in your browser's developer tools and read the response headers, or request the site from a command line and inspect what comes back.
CyberTrust's public scan reads the security-headers dimension and flags the important ones you are missing, so the A–F grade shows the gap at a glance.
A header set you can paste today
The three that matter most are small enough to fit on one line each, and they belong on every response, not only on the HTML document.
Strict-Transport-Security: max-age=31536000; includeSubDomains tells the browser to speak HTTPS to this host for a year, and never to let the user click past a certificate warning. Include the subdomains only if all of them are HTTPS-only — one http-only subdomain with HSTS is an outage you create for yourself.
X-Content-Type-Options: nosniff stops the browser from guessing a content type and re-interpreting your response as something executable. X-Frame-Options: DENY stops your pages being framed, which removes clickjacking. CSP's frame-ancestors does the same job in newer browsers, and is the option to use if you also want a real content policy.
Referrer-Policy: strict-origin-when-cross-origin keeps full URLs out of the Referer header when you link off-site, which is a small but real leak of internal structure.
Rolling out CSP without breaking the site
CSP is the one header that can take a site down, because a policy that is slightly wrong blocks your own scripts and styles. Deploy it in report-only mode first: the browser logs what would have been blocked and does nothing. That report is the inventory of every third-party script, inline handler and font your site actually depends on.
Then write the policy from that inventory, start with 'self' and add sources as you go, and only turn on enforcement once the report is empty. A CSP that blocks nothing is worse than no CSP, because it creates the impression of protection that is not there.
The same discipline applies to the rest: add one header, verify it on the real site rather than on a staging copy, and keep the configuration in version control with the rest of your deployment. Headers are code, and they break like code.
FAQ
- Q: Which HTTP security headers are mandatory?
- A: At least three: HSTS (force HTTPS), X-Content-Type-Options: nosniff (block MIME sniffing), and X-Frame-Options or CSP's frame-ancestors (clickjacking). Strongly recommended on top of that: CSP against XSS.
- Q: Can configuring CSP break my site?
- A: It can block legitimate resources. The safe path is to run Content-Security-Policy-Report-Only for a while, correct the policy based on the reports, and only then switch it to enforcing mode.
- Q: How do I see which headers I am missing?
- A: Use the browser developer tools to read response headers, or run CyberTrust's public scan on your domain — the report lists exactly which security headers are absent.