What security headers are and why they are a free win

Every response your server sends includes a few lines of metadata, the HTTP headers. Some of them tell the browser how to protect the page: use HTTPS only, refuse to be embedded elsewhere, run scripts only from these origins. They require no application code, are usually added in a single configuration file, and block whole classes of common attacks on the browser side. Below are the five headers that deliver the most value, each with a safe starting value.

Strict-Transport-Security (HSTS)

HSTS tells the browser to open your site over HTTPS only, for the period you specify. Even if a user types http://, the browser upgrades the connection before anything leaves the device, which removes the window where a first unencrypted request could be intercepted.

Starting value: Strict-Transport-Security: max-age=31536000; includeSubDomains. The caveat is includeSubDomains: it covers every subdomain. If one of them has no HTTPS (an old admin panel, a staging host), that subdomain becomes unreachable. Start with a short max-age such as one day, confirm nothing breaks, then raise it.

Content-Security-Policy (CSP)

CSP declares which origins the page may load scripts, styles, images and frames from. It is the most effective browser-side defence against cross-site scripting, and also the header that is most often configured wrong: a policy that is too strict blocks your own scripts.

The safe approach is to begin with Content-Security-Policy-Report-Only. In this mode the browser blocks nothing; it only logs what it would have blocked. Watch real traffic for a few days, fix the policy, then switch to the enforcing header. A reasonable starting point for a simple site:

default-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self' 'unsafe-inline'; frame-ancestors 'none'

If you rely on inline scripts, either move them into files or adopt nonces. Leaving 'unsafe-inline' in script-src cancels most of what CSP is for.

X-Frame-Options and frame-ancestors

This header stops your site from being displayed inside an iframe on another site. In a clickjacking attack, the attacker loads your page in an invisible frame and tricks the user into pressing your button while they believe they are clicking something else. Use DENY (never embed) or SAMEORIGIN (embed only on your own origin).

Modern browsers achieve the same result through the frame-ancestors directive in CSP, which is more flexible because you can allow a specific partner domain. Sending both does no harm and covers older browsers.

X-Content-Type-Options: nosniff

Browsers sometimes distrust the content type the server declares and guess from the file's bytes instead. That guess can cause a harmless-looking uploaded file to be executed as a script. X-Content-Type-Options: nosniff turns the guessing off, so the browser trusts only the Content-Type header. It has no side effects and should be on everywhere. The only requirement is that your server sends correct content types for its files.

Referrer-Policy

When a visitor clicks an external link on your site, the browser tells the destination where the visitor came from. The default may include the full path and query string, which can leak order numbers, search terms or tokens to third parties. Referrer-Policy: strict-origin-when-cross-origin sends only your domain to external sites while preserving the full URL within your own site. Your analytics keep working; the leak stops.

Rollout order and checklist

  1. Add X-Content-Type-Options: nosniff; no risk.
  2. Add Referrer-Policy: strict-origin-when-cross-origin; no risk.
  3. Add X-Frame-Options: SAMEORIGIN; if you intentionally embed the site somewhere, allow that origin with frame-ancestors.
  4. Enable HSTS with a short max-age, confirm every subdomain serves HTTPS, then raise it to one year.
  5. Run CSP in Report-Only mode for a week, then enforce it.
  6. After each step, inspect the headers from the outside. The HTTP headers tool lists what your server actually sends. A cache or CDN layer can strip a header you set, so an external check is more reliable than your own browser console.

Conclusion

These five headers take fewer than ten lines in a configuration file and can be rolled out on most small sites in an afternoon. The rule is simple: start with the harmless ones, enable HSTS and CSP gradually, and verify each step from outside. If you use your own domain for branded short links or a link-in-bio page, run the same check on that domain too; the features page covers custom domain setup in detail.