What Is Content Security Policy (CSP)
Content Security Policy is an HTTP header that helps prevent cross-site scripting (XSS), clickjacking, and other code injection attacks by specifying which content sources are allowed to load.
89% of the 101 most-visited sites we scan fail this check. See the full data.
Results in 1–3 minutes · Free · No signup required
Want the result for your own site? Free CSP Checker — paste your domain, get the report.
Why It Matters
XSS attacks remain one of the most common web vulnerabilities. CSP acts as a defense-in-depth layer that can block malicious scripts even if your application has XSS vulnerabilities. Without CSP, attackers can inject scripts that steal user data, hijack sessions, or deface your website.
How We Check
We analyze your Content-Security-Policy header for proper directives including default-src, script-src, style-src, and frame-ancestors. We check for unsafe patterns like 'unsafe-inline' and 'unsafe-eval' that weaken protection.
Good vs Bad Values
| Value | Verdict | Why |
|---|---|---|
default-src 'self'; frame-ancestors 'none'; object-src 'none'; base-uri 'self' | Good | A solid baseline for sites without third-party scripts: everything same-origin, no framing, no plugins. |
script-src 'nonce-R4nd0m' 'strict-dynamic'; object-src 'none'; base-uri 'none' | Good | The nonce-based strict policy recommended by Google. A fresh random nonce per response; injected scripts without it do not run. |
script-src 'self' 'unsafe-inline' | Bad | Allows inline scripts and event handlers, which is exactly what an XSS payload is. The policy gives almost no protection. |
script-src * or https: | Bad | Any host on the internet (or any HTTPS host) can supply scripts, so an attacker simply loads theirs. |
script-src 'self' 'unsafe-eval' | Weak | Permits eval() and friends, which turn string injection into code execution. Remove it unless a library truly needs it. |
script-src https://cdn.example.com (host allow-list only) | Weak | Bypassable if that host serves JSONP endpoints or old framework builds. Prefer nonces or hashes. |
Content-Security-Policy-Report-Only only | Weak | Reports violations but blocks nothing. Good for a trial run, not a final state. |
No Content-Security-Policy header | Bad | The browser applies no restriction on where scripts, frames or form posts come from. |
How to Fix
Add a Content-Security-Policy header starting with a restrictive policy like default-src 'self'. Use nonces or hashes instead of 'unsafe-inline' for inline scripts. Start with report-only mode to identify issues before enforcing.
Nginx (static policy, no inline scripts):
add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'" always;
Apache (requires mod_headers):
Header always set Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'"
Next.js — a static policy in next.config.js:
module.exports = {
async headers() {
return [{
source: "/(.*)",
headers: [{
key: "Content-Security-Policy",
value: "default-src 'self'; frame-ancestors 'none'; base-uri 'self'; object-src 'none'",
}],
}];
},
};
Next.js with inline scripts — generate a nonce per request in middleware and put it in the header:
const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
const csp = [
"default-src 'self'",
"script-src 'self' 'nonce-" + nonce + "' 'strict-dynamic'",
"object-src 'none'",
"base-uri 'none'",
].join("; ");
// set Content-Security-Policy on both the request and the response headers;
// Next.js reads the nonce from the request header and applies it to its own scripts
Cloudflare — Rules → Transform Rules → Modify Response Header → set a static Content-Security-Policy value for your hostname.
Common Mistakes
- Setting the policy in a `<meta http-equiv>` tag. It works for most directives, but `frame-ancestors` and `report-uri` are ignored there, so send a real header.
- Shipping `unsafe-inline` “temporarily” and never removing it. Add nonces to the few inline scripts you have and delete it.
- Forgetting `base-uri` and `object-src`. Without them an injected `<base>` tag or `<object>` can bypass an otherwise tight `script-src`.
- Reusing one static nonce. A nonce must be unpredictable and different on every response, or it is just another allow-listed string.
Frequently Asked Questions
What is Content Security Policy (CSP)?
Content Security Policy (CSP) is an HTTP response header that tells browsers which sources are allowed to load scripts, styles, images, and other resources on your page. It's the most effective technical defense against XSS (Cross-Site Scripting) attacks — even if an attacker injects malicious HTML, CSP prevents unauthorized scripts from executing.
What does 'unsafe-inline' mean in CSP and why is it dangerous?
'unsafe-inline' allows inline JavaScript and CSS to execute — meaning any `<script>` tag or event handler in your HTML runs without restriction. This completely defeats CSP's XSS protection because injected inline scripts would also execute. Replace unsafe-inline with nonces (`'nonce-{random}'`) or hashes (`'sha256-{hash}'`) that allow only your specific inline code.
How do I add CSP to my website without breaking it?
Start with `Content-Security-Policy-Report-Only` (not `Content-Security-Policy`) so violations are reported but not blocked. Check violation reports to see what your site loads. Then iteratively add directives for each resource type, switch to enforcement mode, and tighten further. Never start with blocking mode on an untested policy.
What is the minimum CSP I should set?
At minimum: `default-src 'self'; frame-ancestors 'none'`. This restricts all resources to same-origin and prevents your page from being embedded in iframes. Add `script-src` with nonces for inline scripts, `style-src`, `img-src` (usually including `data:` and your CDN), and `connect-src` for API calls.
Does CSP prevent all XSS attacks?
CSP significantly reduces XSS risk but isn't a complete prevention if misconfigured. Policies with 'unsafe-inline', wildcards (`*`), or `unsafe-eval` leave gaps. A properly configured strict-dynamic + nonce policy provides strong protection. CSP should be one layer in a defense-in-depth approach that also includes input validation and output encoding.
Related Security Checks
Related Tool
CSP Checker
Run all 5 related checks with our free csp checker
Check Your Website Now
Run a free security scan to check for Content Security Policy (CSP) issues and 61 other security checks.
Scan Your Website Free