A browser does a lot of security work on a site’s behalf, but only when the site asks for it. Security headers are how it asks. They cost nothing to send, and together they close several classes of attack that application code alone cannot.

This site sends every header below. You can see them in the panel on the home page, which reads them live from the server’s response.

Check What a Site Sends Today

You need nothing more than curl:

curl -sI https://example.com/

-I requests the headers only and -s hides the progress bar. Look for the names below in the output.

Content-Security-Policy

The most powerful header and the one that takes the most care. It lists where the page may load scripts, styles, images, fonts and connections from. A script injected into the page through a cross-site scripting bug will not run if it is not on that list.

A strict starting point for a site that serves all of its own assets:

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

It is shown on three lines here for reading; send it as a single line.

The value of a policy depends on what it leaves out. Avoid 'unsafe-inline' for scripts: it allows exactly the injected code the policy exists to stop. If the site has inline scripts or style="" attributes, move them into files or use nonces or hashes.

To introduce a policy on an existing site without breaking it, send it first as Content-Security-Policy-Report-Only. The browser reports violations in the console without blocking anything.

Strict-Transport-Security

Tells the browser to use HTTPS for this domain for a set time, even when someone types http:// or follows an old link. It removes the first unencrypted request that an attacker on the network could intercept.

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

includeSubDomains applies the rule to every subdomain, so make sure all of them serve HTTPS first. Adding preload and submitting the domain to the browsers’ preload list makes the rule permanent in browsers before the first visit. It is hard to undo, so add it last.

X-Content-Type-Options

X-Content-Type-Options: nosniff

Stops browsers from guessing a file’s type from its content. Without it, a file uploaded as an image could be run as a script if its content looked like one.

Frame Protection

Content-Security-Policy: frame-ancestors 'none'
X-Frame-Options: DENY

Prevents other sites from loading yours in a frame, which is how clickjacking works: an invisible copy of your page placed under a visitor’s cursor. frame-ancestors is the modern control. X-Frame-Options covers older browsers and costs nothing to keep.

Referrer-Policy

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

Controls how much of the current address is sent to other sites when a visitor follows a link. With this value, other sites see only your domain, never the full path, which may contain identifiers or search terms.

Permissions-Policy

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

Turns off browser features the site does not use, for the page and for anything embedded in it. If a third-party script is ever compromised, it cannot ask for the camera.

Cross-Origin-Opener-Policy

Cross-Origin-Opener-Policy: same-origin

Puts the page in its own browsing context group, so a window opened from another site cannot keep a reference to it. This blocks a group of cross-window attacks and is required for some isolation features.

Cross-Origin-Resource-Policy

Cross-Origin-Resource-Policy: same-origin

Tells the browser that other sites may not load this response as an image, script or other subresource. Files that only your own pages use cannot be pulled into someone else’s page, which also limits side-channel attacks that measure a cross-origin response. Use same-site or cross-origin for assets that other sites are meant to embed.

Headers to Leave Out

  • X-XSS-Protection controlled a filter that browsers have removed. Older versions of that filter could be abused, so do not send it with a value of 1.
  • Server and X-Powered-By with version numbers tell attackers which exploits to try. Remove them or hide the version.

Verify After Every Change

Run the curl command again after a deployment and confirm that each header is still present. It is easy to lose them when a proxy, CDN or framework update changes how responses are built. A short automated test that requests a page and checks the headers catches this before visitors do.