Content Security Policy (CSP)
In one line
CSP is an HTTP header that restricts which resources (scripts, styles, images) a browser can load on a page, preventing XSS attacks.
In simple words
Content Security Policy (CSP) is a security header that lets you control which sources the browser is allowed to load resources from. It is a powerful defense against XSS and data injection attacks.
A CSP header specifies allowed sources for each resource type: script-src (JavaScript), style-src (CSS), img-src (images), font-src (fonts), and more. For example, script-src 'self' https://cdn.com allows scripts only from your own domain and cdn.com.
A strict CSP like default-src 'self' blocks all external resources and inline scripts by default — a strong security baseline. CSP violations are reported to a specified URL for monitoring. Implementing CSP is one of the most effective XSS mitigations available.
Example
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; img-src 'self' data: https:; style-src 'self' 'unsafe-inline'A CSP that allows own-origin resources by default, scripts from self and one CDN, images from any HTTPS source, and inline styles.
Related terms
Related Resources
Related Lessons
- Web Security Basics
Web security including CSP.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.