Greywell Cyber

Security

Five Security Headers Every Website Should Send

A handful of response headers decides what a browser will do with your pages. Here is what each one does in plain words and how to check yours in a minute.

4 min read Greywell Cyber

Every time a browser asks a website for a page, the answer comes with a set of headers: short lines the visitor never sees that tell the browser how to treat what follows. A few of them are about safety. They tell the browser to insist on an encrypted connection, to refuse scripts from anywhere you did not name, to keep your pages out of other people’s frames. They cost nothing to send and most sites send none of them.

Here are the five we look for first, what each one does and what to set it to. You can read your own site’s headers with our free security headers checker while you read.

Strict-Transport-Security

Your site has a certificate and serves its pages over an encrypted connection. This header tells the browser to remember that, so the next time the visitor types your address without the https part, the browser goes straight to the encrypted version instead of asking the plain one first. That first plain request is the moment a hostile network can step in. This header removes it.

Set it to a year and include subdomains:

Strict-Transport-Security: max-age=31536000; includeSubDomains

Leave the preload flag off until the site has run cleanly for a few months. Preload puts your domain on a list built into browsers and it is hard to take back.

Content-Security-Policy

This is the one that matters most and the one most often missing. It names where the browser may load scripts, styles, images, fonts and frames from. A script injected into a page by a compromised plugin or a careless advert does nothing if the policy does not allow its source.

A strict policy for a site that serves only its own files looks like this:

Content-Security-Policy: default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'self'; object-src 'none'

Every site’s policy is different, which is why page builders ship without one. Start from nothing and add each source the site really uses. The browser’s developer console will tell you what it blocked. Each line it reports is either a source you forgot or something that should not have been there.

X-Content-Type-Options

A browser given a file with a vague or missing type will try to guess what it is. A browser that guesses can be tricked into running a file that was uploaded as an image. This header turns the guessing off.

X-Content-Type-Options: nosniff

There is only one value and no reason not to send it.

Referrer-Policy

When a visitor follows a link from your site to another, the browser tells the other site where the visitor came from. Left to itself, it sends the whole address, which can include a page name you would rather not share and, on some sites, a token in the address. This header limits what is sent.

Referrer-Policy: strict-origin-when-cross-origin

With that value another site learns only that the visitor came from your domain and nothing at all if the other site is not encrypted. Your own pages still see the full address, so your own analytics are not affected.

Permissions-Policy

A page can ask the browser for the camera, the microphone, the visitor’s location, payment details and a long list of other things. A site that sells plumbing needs none of them. This header says so. It applies to any script or frame on the page, which is the point: a third party script cannot ask for what the policy has already refused.

Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()

List everything the site does not use. The full list is long. A longer header here is a shorter list of things that can go wrong.

One More for Framing

The frame-ancestors line inside the content security policy above stops other sites loading your pages inside a frame, which is how a visitor is tricked into clicking a button they cannot see. Older browsers do not read that line, so send the older header too:

X-Frame-Options: SAMEORIGIN

Checking Your Own Site

Put your address into the checker and read the notes against each header. Most sites we check send one or two of these, often by accident, because the hosting company set them. The rest are a few lines in the hosting configuration or a small file in the site itself. They are an afternoon’s work for whoever looks after it.

Every site we build sends all of them, this one included. A test fails if one of them goes missing. If yours does not and nobody can tell you why, that is a conversation worth having. Ask us about it.

Back to the blog