HTTP headers tell the browser how to behave; a few key security headers can block many common attacks outright.

The essential headers

  • Content-Security-Policy: restricts which sources can load scripts/styles, slowing down XSS.
  • Strict-Transport-Security: forces the browser to always use HTTPS.
  • X-Frame-Options: prevents your site from being framed (clickjacking).
  • X-Content-Type-Options: stops the browser from guessing file types.
  • Referrer-Policy: controls what gets sent to the destination site when a link is clicked.

A note on CSP

Using unsafe-inline in your CSP defeats much of its protection; use nonces or external files where possible instead.

Set CSP, HSTS, and X-Frame-Options on your responses.