Skip to content

Security headers: set HSTS, CSP and the rest, then check them

Missing security headers leave browser defences switched off on an otherwise working site. Here is what each header does, safe values to start with in nginx or Apache, and how to confirm the site sends them.

Offensly Team··3 min read

You check the response headers of your own site and see only this:

$ curl -sI https://example.com
HTTP/2 200
content-type: text/html; charset=utf-8
server: nginx

Only run checks like these against sites you own or are allowed to test.

The page works, but the browser gets no instructions. It will follow a plain HTTP link to your domain, let any site load your pages in a frame, and guess file types from content. Security headers are those instructions. Each one switches on a defence the browser already has.

What each header does

HeaderSafe starting valueWhat it stops
Strict-Transport-Securitymax-age=300 at first, then max-age=31536000The browser connecting over plain HTTP, where traffic can be read or changed
Content-Security-PolicyStart in report-only mode, see belowInjected scripts running on your pages (cross-site scripting)
X-Content-Type-OptionsnosniffThe browser treating an uploaded text file as a script
Content-Security-Policy: frame-ancestors'self'Other sites loading your pages in a hidden frame (clickjacking)
Referrer-Policystrict-origin-when-cross-originFull URLs, with tokens or IDs in them, leaking to other sites
Permissions-Policycamera=(), microphone=(), geolocation=()Embedded third-party code using features you never use

The header meanings follow MDN's HTTP headers reference and the OWASP Secure Headers Project. X-Frame-Options: SAMEORIGIN does the same job as frame-ancestors 'self' for older browsers; sending both does no harm.

These headers cost no money and no new software, only a few lines of server configuration. They do not fix a vulnerable application, but they make common attacks on your visitors harder, and customers check them first on security questionnaires, often with free online tools.

Add the headers in nginx or Apache

nginx. Inside the server block that serves HTTPS:

add_header Strict-Transport-Security "max-age=300" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Content-Security-Policy-Report-Only "default-src 'self'; frame-ancestors 'self'" always;

always makes nginx send the headers on error pages too. One trap: a location block with its own add_header line drops every header inherited from the server block (nginx docs). Repeat the headers there, or keep all add_header lines in one place.

Apache. In the virtual host, with mod_headers enabled:

Header always set Strict-Transport-Security "max-age=300"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
Header always set Content-Security-Policy-Report-Only "default-src 'self'; frame-ancestors 'self'"

Raise HSTS in steps

HSTS is the one header that can lock users out. Once a browser has seen it, it refuses plain HTTP for your domain until max-age runs out, even if your certificate breaks.

  1. Start with max-age=300 (five minutes) and check that every page and asset loads over HTTPS.
  2. Raise it to a week, then to max-age=31536000 (one year).
  3. Add includeSubDomains only when every subdomain, including old ones nobody maintains, serves valid HTTPS.
  4. Add preload and submit the domain only if you want browsers to ship it as HTTPS-only. The list requires max-age of at least one year and includeSubDomains (hstspreload.org), and removal is slow.

Move CSP from report-only to enforcing

A strict policy such as default-src 'self' blocks inline scripts and scripts from other domains: analytics, chat widgets, payment forms. That is why it starts as Content-Security-Policy-Report-Only. The browser then reports violations in the developer console without blocking anything.

  1. Load your main pages with the console open and note every blocked source.
  2. Add the sources you trust, for example script-src 'self' https://www.googletagmanager.com.
  3. When the console stays clean for a week of normal use, rename the header to Content-Security-Policy.

Avoid adding 'unsafe-inline' to script-src to silence errors. It turns the protection against injected scripts off.

Check what the site actually sends

curl -sI https://example.com | grep -iE 'strict-transport|content-security|x-content-type|referrer-policy|permissions-policy|x-frame'

Run it against the home page, a page deep in the site and a URL that returns 404. Headers set in the application rather than the web server often go missing on error pages and static files. A CDN or proxy in front of the server can also add or strip headers, so check the public URL, not the origin. If the site also exposes dotfiles, fix that in the same server block; see exposed .git and .env files.

The Offensly penetration test checks security headers and cookie flags together with TLS, known vulnerabilities and exposed files, on a site you have verified as yours.

See what the penetration test checks

Check your own domain in seconds

The free check reads your SPF, DKIM, DMARC and MX records and tells you exactly what to fix. No sign-up.