HTTP security headers: what each one does and the values to use
Content-Security-Policy, Strict-Transport-Security, X-Frame-Options, Referrer-Policy and Permissions-Policy explained, with safe starting values for each.
Why security headers matter
HTTP response headers tell the browser how strictly to treat your pages: whether to always use HTTPS, which scripts may run, whether another site may embed you in a frame. They cost nothing to add, they protect every visitor, and missing ones are among the first things penetration testers, security questionnaires and enterprise procurement teams flag.
They do not replace fixing vulnerabilities in your code. They limit how much damage a mistake can do.
How to check your headers
Fetch only the headers of a page with curl:
curl -sI https://example.com | sort Check more than the home page. Headers are often set by one application and missing on others: the blog on a different platform, the help centre, the login page served by an identity provider, or error pages produced by the CDN. A one-off check also misses regressions: a new deploy or a CDN rule change can drop a header silently.
Strict-Transport-Security (HSTS)
Tells the browser to use HTTPS for your domain for a set time, even if someone types http:// or follows an old link. This closes the window in which a first plain-HTTP request can be intercepted.
Strict-Transport-Security: max-age=31536000; includeSubDomains - Start with a short
max-age(for example 300) if you are unsure every subdomain serves HTTPS, then raise it to one year. - Only add
includeSubDomainswhen every subdomain supports HTTPS. - Add
preloadonly if you intend to submit the domain to the browser preload list; removal is slow.
Content-Security-Policy (CSP)
The most powerful header and the hardest to get right. CSP lists where scripts, styles, images, frames and connections may come from. A good policy turns most cross-site scripting bugs into harmless errors.
Do not write a strict policy and ship it on a Friday. Roll it out in report-only mode first:
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' https://www.googletagmanager.com; object-src 'none'; base-uri 'self'; frame-ancestors 'self' Watch what would have been blocked, add the sources you really need, then switch the header name to Content-Security-Policy. Avoid 'unsafe-inline' for scripts; use nonces or hashes instead. object-src 'none' and base-uri 'self' are safe on almost every site and worth adding on day one.
Clickjacking: frame-ancestors and X-Frame-Options
Stops other sites from loading your pages in an invisible frame and tricking visitors into clicking. The modern way is the CSP directive:
Content-Security-Policy: frame-ancestors 'self' Keep the older header too, for older browsers and scanners that still look for it:
X-Frame-Options: SAMEORIGIN X-Content-Type-Options
Stops browsers from guessing a file's type, so an uploaded file served as text cannot be run as a script. There is only one value and no downside:
X-Content-Type-Options: nosniff Referrer-Policy
Controls how much of the current URL is sent to other sites when a visitor follows a link. Full URLs can leak search terms, reset tokens or internal paths.
Referrer-Policy: strict-origin-when-cross-origin This sends the full URL within your own site, only the origin to other HTTPS sites, and nothing to plain HTTP. It is the browser default today, but setting it explicitly makes the intent clear and passes audits.
Permissions-Policy
Turns off browser features your site does not use, so an injected script or embedded third party cannot ask for them.
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=() Only list features you are sure you do not need. If your checkout uses the Payment Request API, keep payment=(self).
Headers to remove
Some headers only help attackers by advertising your stack and version:
Server: nginx/1.18.0: keep the product name if you must, drop the version.X-Powered-By: PHP/8.1orExpress: remove entirely.X-AspNet-Version,X-AspNetMvc-Version: remove entirely.
X-XSS-Protection is obsolete. Modern browsers ignore it; if you send it, use 0.
A safe starting set
| Header | Starting value |
|---|---|
| Strict-Transport-Security | max-age=31536000; includeSubDomains |
| Content-Security-Policy | Start in report-only; at minimum object-src 'none'; base-uri 'self'; frame-ancestors 'self' |
| X-Frame-Options | SAMEORIGIN |
| X-Content-Type-Options | nosniff |
| Referrer-Policy | strict-origin-when-cross-origin |
| Permissions-Policy | camera=(), microphone=(), geolocation=() |
Keep them from drifting. Headers are easy to add and easy to lose in the next deploy. SonarKite checks your security headers and TLS on a schedule and alerts you the moment one disappears.