Learn

HTTPS and security headers: what to set and why

Security headers are short instructions your server sends with every page. They tell the browser to use HTTPS, which scripts to trust and what not to share. This guide covers what each one does, which are worth your time, and how to turn them on safely.

Updated · For agents: Markdown · skill

What a header can and cannot do

Every time a browser asks for a page, your server answers with the page and a list of headers. Most describe the response: its type, its length, how long to cache it. A few are instructions to the browser about how to treat your site. Those are the security headers.

They are a second line of defence. A Content Security Policy does not remove a flaw that lets an attacker inject a script. It stops the browser from running that script. That is worth having, and it is also why a missing header is not, on its own, a hole in your site. Scanners that mark every missing header in red overstate it. The honest question for each header is what it would protect on your site, and what it costs to set.

For most sites the answer is: HTTPS everywhere is required, four headers are one line each and safe, and one (the Content Security Policy) takes real work.

HTTPS first

HTTPS encrypts the connection between the visitor and your server, so nobody on the same wifi can read or change what is sent. Browsers mark plain HTTP pages as not secure, and features such as payments and location only work over HTTPS.

Three things to get right:

The headers, one by one

Header What it tells the browser Effort
Strict-Transport-Security Only ever use HTTPS for this site One line, with care
X-Content-Type-Options Do not guess a file’s type One line
Referrer-Policy How much of the URL to pass to other sites One line
Permissions-Policy Which device features pages may use One line
Content-Security-Policy Where scripts, styles and frames may load from Real work

Strict-Transport-Security (HSTS)

Your redirect from HTTP to HTTPS has one weak moment: the very first request, which is still unencrypted. HSTS closes it. Once a browser has seen the header, it goes straight to HTTPS for as long as max-age says, without asking.

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

Two cautions. includeSubDomains applies to every subdomain, so an old one that only works over HTTP becomes unreachable. And the preload list, which builds your site into browsers so even the first visit is protected, is slow to leave. Start with a short max-age, confirm nothing breaks, then raise it.

X-Content-Type-Options

X-Content-Type-Options: nosniff

Without it a browser may decide that a file you served as text is really a script, and run it. With it, the browser trusts the type you declared. There is no reason not to set this.

Referrer-Policy

When a visitor follows a link from your site, the browser tells the next site where they came from. If your URLs contain search terms, order numbers or reset tokens, that leaks them.

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

This sends the full URL within your own site and only the host name to others. Modern browsers already default to it. Setting it makes the behaviour the same everywhere.

Permissions-Policy

A third-party script on your page can ask for the camera, the microphone or the location in your site’s name. Permissions-Policy switches off what you do not use:

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

Framing: frame-ancestors

Clickjacking puts your page in an invisible frame on another site and tricks the visitor into clicking something on it. It only matters on pages where one click does something that counts: a payment, a delete, a settings change.

Content-Security-Policy: frame-ancestors 'self'

This replaces the older X-Frame-Options: SAMEORIGIN. If your pages are meant to be embedded, such as a video player or a booking widget, leave it off those pages.

Content-Security-Policy (CSP)

CSP is a list of the places your pages may load scripts, styles, images and frames from. A script injected from anywhere else does not run. It is the strongest header and the easiest to get wrong, because one host missing from the list breaks a feature: the payment form, the chat widget, the analytics.

Add it in three steps:

  1. Report only. Send Content-Security-Policy-Report-Only with your first draft. Nothing is blocked. The browser console lists what would have been.
  2. Fix the list. Walk through every page type, including checkout and login, and add each host you really use.
  3. Enforce. Rename the header to Content-Security-Policy, and check the same pages again.

A policy that allows 'unsafe-inline' scripts protects little. If your site relies on inline scripts, a nonce or a hash per script is the proper fix, and it is a development task.

Cookies and cross-origin requests

Two more settings travel in headers:

How to add them

Where the headers go depends on what serves your site:

Add one header at a time, load the key pages, and read the browser console. Then test again after every release, because a new script from a new host is exactly what a CSP blocks. WitnessQA reports a resource blocked by your own policy as an issue, with the page and the host.

Watch

The checklist

Every step says what to do and how to verify it from outside. The tags are the WitnessQA checks that verify it for you in a free audit. Agents can load the same list as a skill file.

  1. Serve everything over HTTPS

    Every page answers on https:// with a valid certificate. Plain http:// redirects there with one permanent redirect.

    Verify: Request the http:// URL of the home page. It answers 301 or 308 to the https:// URL, and that URL answers 200 without a certificate error.

  2. Load nothing over plain HTTP

    Every script, stylesheet, image and frame on an HTTPS page is loaded over HTTPS too.

    Verify: Read the HTML of each key page. No src or href of a loaded resource starts with http://.

  3. Tell browsers to stay on HTTPS

    Send Strict-Transport-Security with a max-age of at least six months. Add includeSubDomains only when every subdomain works over HTTPS.

    Verify: Read the response headers of the home page over HTTPS. strict-transport-security is present with max-age of 15552000 or more.

  4. Stop the browser guessing file types

    Send X-Content-Type-Options: nosniff on every response.

    Verify: Read the response headers. x-content-type-options is nosniff.

  5. Decide who may frame your pages

    Pages with logins, payments or account actions send Content-Security-Policy: frame-ancestors 'self' (or X-Frame-Options: SAMEORIGIN). Pages meant to be embedded do not need it.

    Verify: Read the response headers of the login and checkout pages. One of the two headers is present.

  6. Limit what you tell other sites

    Send Referrer-Policy: strict-origin-when-cross-origin or stricter, and keep tokens and personal data out of URLs.

    Verify: Read the response headers. referrer-policy is present and is not unsafe-url or no-referrer-when-downgrade.

  7. Switch off browser features you do not use

    Send Permissions-Policy that turns off camera, microphone and location unless a page needs them.

    Verify: Read the response headers. permissions-policy is present.

  8. Add a Content Security Policy, in report-only mode first

    Start with Content-Security-Policy-Report-Only, fix what it reports, then enforce it. Avoid unsafe-inline for scripts where you can.

    Verify: Read the response headers for content-security-policy. Load each key page in a browser and confirm the console shows no resource refused by the policy.

    csp
  9. Protect the session cookie

    Cookies that keep a visitor signed in are set with Secure, HttpOnly and SameSite=Lax or Strict.

    Verify: Sign in with a test account and read the set-cookie headers. The session cookie carries all three attributes.

See which of these your site passes, with evidence for each.

Run a free audit