Add a Content Security Policy (CSP)
Updated 1 Dec 2026
What it means
There is no Content-Security-Policy header (or it allows unsafe inline scripts). CSP tells the browser which sources of scripts, styles and frames are allowed, which limits the damage of cross-site scripting (XSS).
Why it matters
If an attacker manages to inject a script (through a vulnerable plugin, comment field or third-party library), nothing stops it from stealing customer sessions or card details typed into your pages.
How to fix it
- Inventory the domains your pages load scripts, styles, fonts, images and frames from.
- Deploy the policy first as Content-Security-Policy-Report-Only and collect violations for a week.
- Fix or allow-list legitimate sources, avoid 'unsafe-inline' and 'unsafe-eval' (use nonces or hashes instead).
- Switch the header to enforcing mode (Content-Security-Policy).
Careful: A strict CSP applied without testing can break analytics, chat widgets and payment iframes. Start in Report-Only mode.
Configuration examples
nginx
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'" always;
Apache
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'"
IIS (web.config)
<add name="Content-Security-Policy" value="default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'" />
Cloudflare
Rules -> Transform Rules -> Modify Response Header -> Set static: Content-Security-Policy = default-src 'self'; object-src 'none'; base-uri 'self'
Node.js / Express
app.use(helmet.contentSecurityPolicy({
directives: { defaultSrc: ["'self'"], objectSrc: ["'none'"], baseUri: ["'self'"], frameAncestors: ["'self'"] },
}));
Django (settings.py (django-csp))
CONTENT_SECURITY_POLICY = {
'DIRECTIVES': {'default-src': ["'self'"], 'object-src': ["'none'"], 'base-uri': ["'self'"]},
}
Laravel / PHP
$response->headers->set('Content-Security-Policy', "default-src 'self'; object-src 'none'; base-uri 'self'");
Any platform
Content-Security-Policy: default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'
Check your fix
curl -sI https://YOUR_DOMAIN | grep -i content-security-policyCheck the browser console for CSP violations on key pages.
References
- https://developer.mozilla.org/docs/Web/HTTP/CSP
- https://csp-evaluator.withgoogle.com/