Practical guide · 7 min read
Website security headers explained
What HSTS, CSP and the other headers do, in plain terms, and what each one actually stops.
What a security header is
When someone visits your website, your server sends the page along with a set of headers: short instructions to the browser. Security headers are the ones that tell the browser to behave more safely, for example to refuse an unencrypted connection or to block the page from being embedded elsewhere. They are set once on the server and cost nothing to run.
HSTS: force encrypted connections
HTTP Strict Transport Security (HSTS) tells the browser to only ever connect to your site over HTTPS, and to refuse a plain connection even if someone types http:// or follows an old link. It closes the brief window where an attacker on the same network could downgrade the first request and intercept it. Turn it on only once HTTPS works everywhere on your site, because it is deliberately hard to undo.
Content-Security-Policy: control what can load
A Content-Security-Policy (CSP) lists which sources a page is allowed to load scripts, styles and images from. Its main job is to limit the damage of cross-site scripting (XSS): even if an attacker manages to inject a script, the browser refuses to run it unless it comes from a source you approved. CSP is the most powerful of these headers and the fiddliest to get right, so build it up and test it carefully.
Clickjacking: X-Frame-Options and frame-ancestors
These stop your site being loaded invisibly inside someone else’s page and used to trick your users into clicking something they cannot see, an attack called clickjacking. X-Frame-Options is the older header; the modern replacement is the frame-ancestors directive inside CSP, which does the same job with more control. Allowing only your own site to frame your pages is a safe default.
X-Content-Type-Options: stop MIME sniffing
X-Content-Type-Options with the value nosniff tells the browser to trust the file type your server declares instead of guessing from the content. That guessing, known as MIME sniffing, can be tricked into treating an uploaded image or text file as a script. The header takes one value and has no real downside, so it should simply be on.
Referrer-Policy: do not leak your URLs
When a visitor clicks a link to another site, the browser can tell that site which page they came from. Referrer-Policy controls how much of that address is shared. A sensible value such as strict-origin-when-cross-origin sends the full address within your own site but only the bare domain to others, so private paths and query strings in your URLs are not handed out.
Getting them in place
Add them at the server, CDN or framework level so every page is covered, then check them from the outside and watch for changes, because a deployment or a new plugin can quietly drop a header. Start with the safe ones (nosniff, X-Frame-Options, Referrer-Policy, and HSTS once HTTPS is solid), then invest the time to build a good CSP.
General guidance, not legal or professional advice for your specific situation. Only test systems you own or are authorized to assess.