What is a Content-Security-Policy (CSP)? XSS Defense Explained
A Content-Security-Policy (CSP) is an HTTP response header that whitelists where a page may load scripts, styles, and other resources from — the single most effective defense against cross-site scripting (XSS). This guide explains how CSP works, the key directives, nonces vs hashes, and how to roll one out safely.
Last updated:
Core Concepts
- What CSP does
- CSP is a response header that tells the browser which sources are allowed for each type of resource — scripts, styles, images, fonts, frames, connections. If an attacker injects a <script> from an untrusted origin, a good CSP means the browser simply refuses to run it. It turns XSS from 'arbitrary code executes' into 'the injected code is blocked', which is why CSP is considered a defense-in-depth cornerstone for web apps.
- Key directives
- default-src is the fallback for any resource type you don't set explicitly — start with 'self'. script-src and style-src control scripts and styles; img-src, font-src, connect-src (fetch/XHR/WebSocket), and frame-src control their respective resources. frame-ancestors controls who may embed your page (the modern replacement for X-Frame-Options against clickjacking). base-uri and object-src 'none' close off common attack vectors.
- Nonces and hashes vs unsafe-inline
- The quickest way to break CSP's protection is 'unsafe-inline', which re-allows inline scripts and the exact injection CSP is meant to stop. Instead, allow specific inline scripts with a nonce (a per-response random token added to script-src and the <script nonce=...> tag) or a hash of the script's contents. These let your legitimate inline scripts run while still blocking injected ones.
- Rolling it out safely
- Deploy the policy first as Content-Security-Policy-Report-Only: the browser logs violations to a report endpoint but blocks nothing, so you can see what a strict policy would break. Review the reports, add the legitimate sources you actually use, then switch to the enforcing Content-Security-Policy header. Flipping straight to a strict enforced policy usually breaks the site.
Frequently Asked Questions
Does CSP stop all XSS?
No single control stops all XSS, but a strict CSP is the most effective defense-in-depth layer: even if an injection happens, the browser refuses to execute scripts from disallowed sources. Combine it with output encoding and input validation. Avoid 'unsafe-inline', which reopens the hole CSP is meant to close.
Should I use a nonce or a hash in my CSP?
Use a nonce when your server renders pages dynamically — generate a fresh random nonce per response, add it to script-src and to each legitimate <script nonce=...>. Use a hash for static inline scripts that don't change, so you can allow them by their content hash without a per-request token. Both avoid 'unsafe-inline'.
How do I add a CSP without breaking my site?
Start with Content-Security-Policy-Report-Only so violations are logged but nothing is blocked. Watch the reports, add the sources you legitimately use, then switch to the enforcing header. Use our CSP Generator to build the policy directive by directive.
Try these related tools
CSP Generator →
Build a Content-Security-Policy header by picking directives and sources. Generate a ready-to-paste CSP for script-src, style-src, img-src, connect-src and more — with sensible strict defaults. 100% local.
Meta Tag Generator →
Generate meta tags for your website — title, description, Open Graph (Facebook/LinkedIn), Twitter Card, favicon, and viewport. Copy the complete HTML snippet to paste into your <head> section. 100% local.
HTTP Status Codes →
Complete reference of HTTP status codes with meanings, use cases, and RFC citations. Search 60+ codes from 100 Continue to 511 Network Authentication Required.