← Back to scanner
2026-01-22

Content Security Policy: The Header That Stops XSS

CSP is the most powerful security header, but also the hardest to configure. A practical guide.

Content Security Policy (CSP) is the single most effective defense against XSS attacks. But it's also the most complex header to implement.

What CSP does

CSP tells the browser which sources of content are trusted. Anything not on the list is blocked. This means even if an attacker injects a script, the browser won't execute it.

A practical policy

Content-Security-Policy:
  default-src 'self';
  script-src 'self' https://cdn.example.com;
  style-src 'self' 'unsafe-inline';
  img-src 'self' https: data:;
  font-src 'self' https://fonts.gstatic.com;
  connect-src 'self' https://api.example.com;
  frame-ancestors 'none';

Deployment strategy

  1. Start with Content-Security-Policy-Report-Only
  2. Set up a reporting endpoint
  3. Fix violations found in reports
  4. Switch to enforced policy

Don't jump straight to enforcement. You'll break your site.