Home / Guides / HTTP security headers explained: HSTS, CSP and more
HTTP security headers explained: HSTS, CSP and more
When your browser loads a page, the server sends back a set of HTTP response headers along with the page. Some of them switch on protections that are built into the browser. They cost nothing to add, and they block entire categories of attack. Adding them is one of the cheapest security improvements you can make.
This guide covers the headers that matter most, what each one does, and how to set them up without breaking your site.
Strict-Transport-Security (HSTS)
HSTS tells the browser to always use HTTPS for your site and never to try plain HTTP. It stops attackers from downgrading a connection or intercepting the first insecure request.
Strict-Transport-Security: max-age=31536000; includeSubDomains
max-ageis how long, in seconds, the browser remembers the rule. 31536000 is one year.includeSubDomainsapplies the rule to every subdomain. Only add it when all subdomains support HTTPS.preloadasks to be included in the browsers’ built-in list. It is hard to undo, so only use it when you are sure.
Start with a short max-age such as 300 while testing, then raise it.
Content-Security-Policy (CSP)
CSP is the strongest defence against cross-site scripting (XSS). It tells the browser exactly where scripts, styles, images and other resources may load from. If an attacker injects a script, the browser refuses to run it.
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'
Key directives:
default-srcis the fallback for anything not listed.script-srccontrols JavaScript. This is the most important one.object-src 'none'blocks old plugin content.frame-ancestorscontrols who may embed your page in a frame.base-uristops attackers from changing how relative URLs resolve.
Be wary of 'unsafe-inline' and 'unsafe-eval' in script-src. They largely cancel the protection. If you need inline scripts, use a nonce or a hash instead. Wildcards such as * or https: in script-src are also too permissive.
CSP is easy to get wrong, so roll it out carefully. Use Content-Security-Policy-Report-Only first. It reports violations without blocking anything, and you can fix them before you switch to enforcement.
X-Content-Type-Options
X-Content-Type-Options: nosniff
Without it, some browsers try to guess a file’s type and can treat an uploaded file as a script. nosniff makes them respect the declared type. There is only one valid value, and you should always send it.
Clickjacking protection
Clickjacking tricks users into clicking something hidden inside a frame on another site. Prevent it with the CSP frame-ancestors directive:
Content-Security-Policy: frame-ancestors 'none'
The older header X-Frame-Options: DENY (or SAMEORIGIN) does the same job and is still worth sending for older browsers.
Referrer-Policy
When a user follows a link, the browser may send the previous page’s address in the Referer header. This can leak private details, such as tokens or identifiers in URLs.
Referrer-Policy: strict-origin-when-cross-origin
This is a sensible default: it sends only the site name to other sites and the full address within your own site. Avoid unsafe-url.
Permissions-Policy
This lets you switch off browser features your site does not use, so that injected code cannot use them either:
Permissions-Policy: camera=(), microphone=(), geolocation=()
Cross-origin headers
These add defence in depth and are optional for most sites:
Cross-Origin-Opener-Policy: same-originisolates your window from pop-ups opened by other sites.Cross-Origin-Resource-Policy: same-originstops other sites from loading your resources.
Cookie flags
Cookies are sent with Set-Cookie headers, and their flags matter as much as the headers above:
Secure: the cookie is only sent over HTTPS.HttpOnly: JavaScript cannot read it, which limits the damage of XSS. Use it for session cookies.SameSite=LaxorStrict: limits cross-site sending and reduces cross-site request forgery (CSRF).
Headers to remove
Some headers reveal what software you run, which helps attackers choose known exploits:
X-Powered-By: PHP/8.1.2Server: nginx/1.18.0(the version number is the problem)X-AspNet-Version
Remove them or reduce them to a generic name.
How to set them
Cloudflare Pages or Netlify (_headers file in your published folder):
/*
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
Content-Security-Policy: default-src 'self'; frame-ancestors 'none'
Nginx (inside server or location):
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
Apache (needs mod_headers):
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
A sensible order to adopt them
- Make sure the whole site works over HTTPS and redirects HTTP to HTTPS.
- Add
X-Content-Type-Options,Referrer-PolicyandPermissions-Policy. They rarely cause problems. - Add HSTS with a short
max-age, then raise it. - Add clickjacking protection.
- Add CSP in report-only mode, fix violations, then enforce it.
- Check your cookies for
Secure,HttpOnlyandSameSite.
Check your own site
Enter your address in our HTTP Security Headers Checker. It reads your public response headers, explains what is missing, and gives an overall grade from A to F. Only scan sites you own or have permission to test. If you also want to see how your domain is configured at the DNS level, the DNS Lookup shows your records, including CAA and DNSSEC status.