Content-Security-Policy explained
CSP is one of the most effective defences against cross-site scripting (XSS) — and one of the most commonly missing headers. Here's how to introduce one safely.
What CSP does
A Content-Security-Policy tells the browser which sources of scripts, styles and content are allowed to load. If an attacker injects a script, a good CSP stops the browser running it.
Start in report-only mode
Deploy Content-Security-Policy-Report-Only first. It reports violations without blocking anything, so you can see what your site actually loads before you enforce.
A starting policy
default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' — then add the specific third-party domains (analytics, fonts, embeds) you genuinely use.
Tightening up
Once report-only is clean, switch the header to Content-Security-Policy to enforce it, and work towards removing 'unsafe-inline' using nonces or hashes.