HTTP security headers explained
Security headers are instructions a site sends to the browser. Here is what each one does, a value to start from, and the ones to stop sending.
Updated
Why they are worth ten minutes
A header does not fix a vulnerability. It limits what an attacker can do with one, and it closes whole classes of attack that need no vulnerability at all, such as loading your page inside somebody else's frame. They are set once, on the server or the CDN, and apply to every visitor.
Strict-Transport-Security
Tells the browser to use HTTPS for this host from now on, even if a link or the user says http://. It removes the unencrypted first request that an attacker on the same network could intercept.
Strict-Transport-Security: max-age=31536000; includeSubDomains
Start with a short max-age if you are unsure every subdomain serves HTTPS, then raise it. Add preload and submit the domain to the browser preload list only when you are certain: it is slow to undo.
Content-Security-Policy
Says where scripts, styles, images and frames may come from. It is the strongest defence against cross-site scripting, because an injected script that the policy does not allow does not run.
A policy based on a per-response nonce is the kind that holds up:
Content-Security-Policy: script-src 'nonce-{random}' 'strict-dynamic'; object-src 'none'; base-uri 'none'; frame-ancestors 'self'- Avoid
'unsafe-inline'for scripts. It allows exactly what the policy exists to block. - Set
object-src 'none'andbase-uri. Both are cheap and both close real bypasses. - Roll out with
Content-Security-Policy-Report-Onlyfirst and read the reports, but do not leave it there: a report-only policy protects nobody. - If a full policy is out of reach on an old site, a small one is still worth sending:
frame-ancestors,object-srcandbase-urialone break almost nothing.
X-Content-Type-Options
X-Content-Type-Options: nosniff
Stops the browser guessing a file's type from its contents. Without it, an uploaded file served as text can be run as a script.
Clickjacking protection
Clickjacking loads your page invisibly inside another site and tricks the visitor into clicking it. The modern control is the frame-ancestors directive of the policy above. X-Frame-Options: DENY (or SAMEORIGIN) does the same for older browsers, and sending both is fine.
Referrer-Policy
Referrer-Policy: strict-origin-when-cross-origin
Controls how much of the current address is sent to the next site. With this value another site learns your domain, not the full path, which matters when a path or query contains an order number or a reset token.
Permissions-Policy
Permissions-Policy: geolocation=(), camera=(), microphone=()
Switches off browser features the site does not use, for the page and for anything it embeds. A script that should never ask for the camera then cannot.
The cross-origin trio
Cross-Origin-Opener-Policy: same-origin cuts the link between your page and windows on other sites that it opens or that open it, which blocks a family of cross-window attacks. It also breaks sign-in and payment flows that rely on a popup talking back to the page; where the site uses one, send same-origin-allow-popups. Cross-Origin-Embedder-Policy is needed only for features such as SharedArrayBuffer, and Cross-Origin-Resource-Policy controls which other sites may embed your files. Test all three before turning them on.
Headers to stop sending
X-XSS-Protection. The filter it controlled was removed from browsers and could itself be abused. Remove it, or send0.Public-Key-PinsandExpect-CT. Both are obsolete.ServerandX-Powered-Bywith exact versions. They save an attacker the work of finding out what to try.
Where to set them, and how to check
Set them at the outermost layer you control: the CDN, the reverse proxy or the web server. Then check that they are present on every kind of response, including redirects and error pages, because a framework often adds them only to its own successful pages.
A header that is present but wrong is worse than one that is absent, since it looks like protection. Scan the site to see each header judged by its value, not just its presence, alongside the rest of what a scan checks.