Cross-site scripting (XSS) — injecting malicious JavaScript into a page — remains one of the most common web vulnerabilities. A Content-Security-Policy tells the browser exactly which sources of code are trusted, so injected scripts simply do not run.
A starter policy
default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'
This allows resources only from your own origin and blocks plugins and framing. From there you add the specific third parties you genuinely use (analytics, fonts, ad networks).
Deploy without breaking things
- Start with
Content-Security-Policy-Report-Onlyso violations are reported but nothing is blocked. - Collect reports, identify legitimate sources, and add them explicitly.
- Avoid
unsafe-inlinefor scripts — use nonces or hashes instead. - Once clean, switch the header to enforcing mode.
CSP rewards patience: a tight, tested policy is worth far more than a permissive one rushed into production.