Security headers and Content Security Policy

Use HTTP response headers to tell browsers which scripts, frames and features your site may use.

Intermediate35 min

Learning objectives

  • Set core security headers and explain what each one does
  • Write a Content Security Policy that blocks injected scripts
  • Use nonces or hashes to allow only trusted inline code

Headers are a second line of defence

Response headers instruct the browser to enforce extra protections. They do not fix a bug in your code, but they limit the damage when something slips through, such as an XSS payload.

Header Recommended value Purpose
Content-Security-Policy See below Restricts where scripts, styles and other resources load from
Strict-Transport-Security max-age=31536000; includeSubDomains Forces HTTPS for future visits
X-Content-Type-Options nosniff Stops browsers guessing a file’s type
Referrer-Policy strict-origin-when-cross-origin Limits URL information sent to other sites
Permissions-Policy camera=(), microphone=(), geolocation=() Disables browser features you do not use
Cross-Origin-Opener-Policy same-origin Isolates your window from cross-origin popups

Setting headers in Node

res.setHeader('Content-Security-Policy',
  "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'");
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');

Headers can also be set in your hosting platform’s configuration or in middleware, so every response gets them, including error pages.

Anatomy of a strict CSP

Content-Security-Policy:
  default-src 'self';
  script-src 'self' 'nonce-r4nd0m';
  style-src 'self';
  img-src 'self' data:;
  object-src 'none';
  base-uri 'self';
  frame-ancestors 'none';
  • default-src 'self' allows resources only from your own origin unless another directive overrides it.
  • script-src 'self' 'nonce-…' allows external scripts from your origin, plus inline scripts that carry the matching nonce.
  • object-src 'none' blocks legacy plugins such as Flash.
  • base-uri 'self' stops attackers injecting a <base> tag to redirect relative URLs.
  • frame-ancestors 'none' prevents your pages being framed, which blocks clickjacking. It replaces the older X-Frame-Options header.

Nonces and hashes

A strict policy blocks inline <script> blocks and onclick= attributes, which is exactly what stops most injected XSS. When you need specific inline code, allow it with:

  • A nonce: a random value generated per response and added to both the header and the script tag. It must change on every request.
  • A hash: 'sha256-…' of the exact script contents. It suits static inline code.
<script nonce="r4nd0m">
  console.log('This inline script is allowed');
</script>

Avoid 'unsafe-inline' and 'unsafe-eval' in script-src. They remove most of the protection CSP provides.

Roll out safely

  1. Start with Content-Security-Policy-Report-Only so violations are reported but not blocked.
  2. Review the reports and fix legitimate resources.
  3. Switch to the enforcing Content-Security-Policy header.

Lab: audit your headers

On a site you own, inspect the response headers:

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

Then run the same check on a staging copy after you add the headers above, and confirm the browser console shows no CSP violations for your normal pages.

Key takeaways

  • Set security headers on every response, including errors.
  • A strict CSP with nonces or hashes is one of the most effective XSS defences available.
  • Avoid 'unsafe-inline' in script-src, and deploy new policies in report-only mode first.