# Security headers explained

URL: https://ekom.blog/security-headers-explained/
Published: 2024-12-11
Author: Ekom
Topics: How the internet works

> Security headers are short instructions your server sends to browsers. Here is what the main ones do, which to add first, and how to avoid breaking your site.

Every time a browser loads a page, the server sends back more than the page. It sends a set of notes called HTTP headers: how long to cache this, what type of file it is, and so on.

A few of those notes are about safety. They tell the browser how to behave when handling your page, and they close off whole families of attacks. These are security headers, and adding the main ones usually takes minutes.

## The analogy

Think of your website as a shop. The page content is the stock. Security headers are the signs on the door and instructions to the staff: "Don't let anyone put this inside a frame", "Only accept deliveries from these suppliers", "Always use the secure entrance".

The browser is the staff, and it follows the signs.

## The main headers

### Strict-Transport-Security (HSTS)

Tells the browser: "only ever use HTTPS for this site". After a first visit, the browser refuses to load the plain HTTP version for a set time. That blocks a class of downgrade attacks. I have a full post on HSTS and HTTPS preload. Make sure HTTPS works on every part of your site before turning this on, because mistakes are sticky.

### Content-Security-Policy (CSP)

The most powerful and the hardest. It lists which sources a page may load scripts, styles, images and frames from. If an attacker injects a script from an unknown domain, the browser refuses to run it.

The catch: modern sites load lots of things (analytics, fonts, chat widgets, payment scripts), and a strict policy that doesn't list them will break your site. Many people start in "report-only" mode, which logs problems without blocking anything.

### X-Content-Type-Options

One value: nosniff. It stops browsers guessing a file's type and treating a harmless-looking file as a script. Easy and safe on almost every site.

### X-Frame-Options (and frame-ancestors)

Controls whether other sites can embed your page in a frame. Without it, an attacker can load your page invisibly inside theirs and trick people into clicking things (clickjacking). SAMEORIGIN allows only your own site to frame it; DENY allows nobody. The newer way is the frame-ancestors part of CSP. Check before using it, if you have pages that are meant to be embedded elsewhere.

### Referrer-Policy

When someone clicks a link from your site to another, the browser can tell the destination where they came from. This header controls how much to share. A sensible default is strict-origin-when-cross-origin, which sends just your domain to other sites and the full address within your own site.

### Permissions-Policy

Lets you switch browser features on or off for your pages and any embedded content, such as camera, microphone and geolocation. If your site doesn't need the camera, say so, so an embedded script can't ask for it.

### Cross-origin headers

You may see names like Cross-Origin-Opener-Policy and Cross-Origin-Resource-Policy. They isolate your pages from other sites at a deeper level. Useful in specific cases; I'd leave them until the basics are done.

## A quick reference

Header
Protects against
Risk of breaking things

Strict-Transport-Security
Downgrade to HTTP
Medium if HTTPS is incomplete

Content-Security-Policy
Injected scripts, data theft
High, needs testing

X-Content-Type-Options
File type confusion
Very low

X-Frame-Options
Clickjacking
Low, unless you embed your own pages

Referrer-Policy
Leaking URLs to other sites
Low

Permissions-Policy
Misuse of device features
Low

## Why they matter for site owners

Headers aren't a replacement for updates, strong passwords and backups. They are an extra layer that makes some attacks fail even when something else slips. They also show up in security scans, and some clients and compliance checks ask for them.

For SEO, security headers aren't a ranking factor. Don't add them hoping for traffic. Add them because they reduce risk.

## How to check what you have

- Open your site in a browser, open developer tools and go to the Network tab.

- Click the main page request and read the Response Headers.

- Or use an online header scanner: paste your address, get a letter grade and a list of missing headers.

Treat the grade as a prompt for reading, not a score to chase. A perfect grade on a site that broke its checkout is a bad trade.

## How to add them

WordPress. A security plugin can add some headers, or you can set them on the server. My earlier beginner's guide to WordPress security headers shows where to start.

Cloudflare. You can add response headers with rules, and HSTS is a toggle in its SSL settings. See Cloudflare settings that help speed and security.

Shopify and hosted platforms. Most of this is managed by the platform and you can't change it. Ask what they set, instead of fighting it.

Server config. On Apache you can use .htaccess; on Nginx the server config. Here is a typical Apache starter:

<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set X-Frame-Options "SAMEORIGIN"
</IfModule>

Those three rarely cause trouble. Leave HSTS and CSP for later and handle them carefully.

## The order I'd do it in

- Back up the site.

- Add nosniff, Referrer-Policy and X-Frame-Options.

- Test your key pages, the login, the checkout and any embeds.

- Turn on HSTS with a short duration first, then raise it once you're sure.

- Try CSP in report-only mode, fix what it reports, then enforce.

## Mistakes to avoid

- Copying a strict CSP from a blog post. It's written for someone else's site.

- Setting a long HSTS with a broken subdomain. You can lock people out.

- Forgetting that caches and CDNs may serve old headers. Purge after changes.

- Doing it all at once. If something breaks, you won't know which header did it.

## Key takeaways

- Security headers are instructions to browsers that shrink the damage of certain attacks.

- Start with the low-risk ones, then HSTS, then CSP in report-only mode.

- They're not a ranking factor and not a replacement for updates.

- Test your login, checkout and embeds after every change.

## Related reading

- Tor explained

- Redirects explained: 301 vs 302

- VPNs explained: how they work

Do you know what your own site is sending right now? Open the Network tab and look.

---

From Ekom (https://ekom.blog/). More: https://ekom.blog/llms.txt
