CSP - Content Security Policy
About 2 min read
CSP (Content Security Policy) is a security mechanism that declares, via an HTTP response header, the permitted origins of resources allowed to execute on a web page. It is a defensive layer that reduces the damage from cross-site scripting (XSS). By having the server instruct the browser that "only scripts from this domain may run on this page," it blocks the execution of malicious scripts injected by an attacker at the browser level.
Its Role as an XSS Countermeasure
The basics of XSS prevention are input sanitization and output escaping, but developer mistakes can leave gaps in these measures. CSP functions as a "last line of defense" that complements these measures. Even if an escaping oversight allows attack code to slip into the HTML, a correctly configured CSP causes the browser to refuse to execute that script. Combining secure coding with CSP achieves defense in depth.
Key Directives
| Directive | Controls | Example |
|---|---|---|
| default-src | Fallback for other directives | 'self' |
| script-src | Loading and executing JavaScript | 'self' 'nonce-abc123' |
| style-src | Loading and applying CSS | 'self' 'unsafe-inline' |
| img-src | Sources from which images load | 'self' data: https://cdn.example.com |
| connect-src | Destinations for fetch / XHR / WebSocket | 'self' https://api.example.com |
| frame-ancestors | Origins allowed to embed in an iframe | 'none' |
| form-action | Form submission destinations | 'self' |
Nonce-Based and Hash-Based Allowlisting
There are two ways to allow inline scripts: the nonce method and the hash method.
The server generates a random value (nonce) for each request and embeds the same value in both the CSP header and the script tag. Only scripts whose values match are executed. This suits dynamic pages.
A hash value such as SHA-256 is computed from the contents of the inline script and listed in the CSP header. If even one byte of the script content changes, the hash will not match and it will not execute. This suits static scripts, but the hash must be recomputed each time the content changes.
Monitoring with report-uri / report-to
CSP includes a mechanism for reporting policy violations to the server. report-uri (legacy spec) or report-to (new spec) directive lets you specify the report destination, after which the browser sends a JSON-format report each time it detects a policy violation. First, collect reports only with the Content-Security-Policy-Report-Only header and confirm that existing functionality is not broken before migrating to production enforcement; this is the safe deployment procedure.
The CSP Deployment Flow
Common Mistakes When Deploying
Adding 'unsafe-inline' just because inline scripts won't run nearly nullifies CSP's XSS protection. The nonce method can be used instead.
Specifying * in script-src allows scripts to be loaded from any domain. Enumerate the permitted domains individually.
Forgetting third-party scripts such as ads, analytics, and chat widgets will cause features to break in production.
CSP is a foundational web security technology alongside WAF and SSL/TLS. Also see Browser Extension Security, Browser Password Safety, and the Startup Security Checklist.
Was this article helpful?
Related Terms
- CORS - Cross-Origin Resource SharingCORS is a browser mechanism that controls which origins can access resources on another domain. Misconfigured CORS headers can expose APIs to unauthorized cross-origin requests.
- Clickjacking - Hidden Clicks on Invisible LayersClickjacking tricks users into clicking hidden elements by overlaying transparent frames on legitimate pages. X-Frame-Options and CSP frame-ancestors directives are the primary defenses.
- Pretexting - Fabricated Scenarios for Data TheftPretexting is a social-engineering technique that fabricates a scenario to extract information. Impersonating IT support or HR is typical, and it often serves as the opening move in a BEC attack.
- Double Extortion Ransomware - Encrypt and LeakDouble extortion ransomware encrypts files and threatens to publish stolen data. Even organizations with backups face pressure, as the data-leak threat adds reputational and regulatory risk.