CSP Policy Builder

Build a Content-Security-Policy header to reduce the risk of cross-site scripting (XSS).

Advertisement

What is a Content-Security-Policy?

CSP is an HTTP response header that tells the browser which sources of scripts, styles, images and other resources are allowed to load. A good policy blocks injected malicious scripts, making cross-site scripting attacks much harder to exploit.

Common source values

  • 'self' — only your own domain.
  • 'none' — block everything for that directive.
  • https: — any HTTPS source.
  • 'unsafe-inline' — allow inline scripts/styles (avoid if possible).
  • A specific domain like https://cdn.example.com.

How to deploy

  1. Build the policy above and add it as a Content-Security-Policy response header on your server.
  2. Test first with Content-Security-Policy-Report-Only so nothing breaks while you tune it.
  3. Watch the browser console for blocked resources and add trusted sources as needed.
Start strict (default-src 'self') and loosen only what you must. Avoid 'unsafe-inline' for scripts — it defeats much of CSP's protection.
Advertisement