Security Headers Explained: CSP, HSTS and What to Set First
Security headers are short instructions your site gives browsers. Here is what the important ones do, a safe order to add them in, and how to check what your site sends today.
๐ก The short version: security headers are small instructions your site sends with every page that tell the browser how strict to be. They cost almost nothing to add and block whole families of attacks. Start with HSTS,
nosniffand a Referrer-Policy, then work towards a Content-Security-Policy.
Think of headers as house rules you print on the door. Your site works without them, in the same way a house works without a lock. The difference is that visitors' browsers will happily follow the rules you give them, so it's worth giving some. ๐ช
๐ Check what you send today
curl -sI https://your-site.com
Look through the response for the headers below. Anything missing is something you can add.
๐งฑ The headers that matter
1. Strict-Transport-Security (HSTS)
Tells the browser to use HTTPS for your site, always, for a set time. It stops a visitor on a bad network being quietly downgraded to http.
Strict-Transport-Security: max-age=31536000; includeSubDomains
Only add includeSubDomains when every subdomain really supports HTTPS, and be careful with the preload option, which is hard to undo.
2. X-Content-Type-Options
Stops the browser guessing a file's type. A file that claims to be an image won't be run as a script.
X-Content-Type-Options: nosniff
3. Referrer-Policy
Controls how much of the page address is shared when a visitor clicks a link to another site. Addresses can contain things you'd rather keep private. Browsers now use this value by default when nothing is set, but stating it yourself makes the behaviour certain.
Referrer-Policy: strict-origin-when-cross-origin
4. Clickjacking protection
Stops other sites showing yours inside an invisible frame and tricking people into clicking. Use the frame-ancestors part of a Content-Security-Policy, or the older header:
X-Frame-Options: DENY
5. Content-Security-Policy (CSP)
The big one. It lists where your page may load scripts, styles, images and more from, so an injected script from somewhere else simply won't run. It also takes the most care, because a strict policy can break things that load from other places.
Content-Security-Policy: default-src 'self'; frame-ancestors 'none'
That line is a starting point, not a finished policy. Real sites need to allow their own fonts, analytics and so on.
๐ช A safe order
- Add HSTS,
nosniffand a Referrer-Policy. Low risk, quick. - Add clickjacking protection.
- Roll out CSP in report-only mode first (
Content-Security-Policy-Report-Only), watch what would have been blocked, fix it, then switch it on.
๐ช Don't forget cookies
Session cookies should carry Secure (sent only over HTTPS), HttpOnly (not readable by page scripts) and SameSite. A session cookie without Secure is treated as a high-severity problem in our reports.
| Header | Protects against, and how we rate it if missing |
|---|---|
| CSP | Injected scripts. Rated medium. |
| HSTS | Downgrade to http. Rated medium. |
| Clickjacking protection | Hidden frames. Rated medium. |
nosniff | Type confusion. Rated low. |
| Referrer-Policy | Address leaks. Rated low. |
Deploying on Vercel? Our v0 and Vercel checklist shows where headers fit in a Next.js project.
๐ Not sure what your site sends? Scan it free and get your headers graded with the fix for each one.