What is Content Security Policy (CSP)?
Content Security Policy (CSP) is an HTTP response header that tells the browser which sources of scripts, styles, images and frames a page may use. It does not fix a bug, but it limits the damage of cross-site scripting by blocking code that does not come from an allowed source.
How it works
The server sends a Content-Security-Policy header with directives such as script-src and frame-ancestors. When a page tries to load a script from a source that is not listed, or to run an inline script, the browser refuses. The strongest policies avoid unsafe-inline and use a per-response nonce or hash so only your own scripts run. A policy full of unsafe-inline and unsafe-eval keeps the header but removes most of its protection.
Content-Security-Policy:
default-src 'self';
script-src 'self' 'nonce-r4nd0m';
img-src 'self' data: https:;
frame-ancestors 'none';
base-uri 'self'How to tell if your site is affected
- The response has no Content-Security-Policy header. Open the browser dev tools, Network tab, and look at the page response.
- The policy contains script-src with unsafe-inline or unsafe-eval, or a bare wildcard *.
- Scripts from many third-party hosts are allowed, so one compromised host can serve code to your pages.
- The header is only set on some routes, or only on responses your app generates but not on files served by a CDN or nginx.
How to fix it
Start in report-only mode with Content-Security-Policy-Report-Only and watch the console for violations before enforcing anything.
Move inline scripts into files, or allow them with nonces or hashes instead of unsafe-inline.
In Next.js generate a nonce in middleware and pass it to your scripts, following the framework's CSP guide; in WordPress use a plugin that adds nonces, since many themes rely on inline code.
Add frame-ancestors, base-uri and object-src 'none' even if the rest of the policy stays loose at first.
Remove third-party hosts you no longer use, then enforce the policy and retest key pages such as login and checkout.
How ScanAnt checks for it
The free quick scan reads the header from your public page. It reports a missing policy, and a weak one that still allows inline or eval scripts, together with the directives that matter most. It cannot know whether your site would break under a stricter policy, so test changes in report-only mode.
Questions
Does CSP replace input escaping?
No. It is a second layer. Escape output properly and use CSP so a missed case does not become a working attack.
What does unsafe-inline mean?
It allows any inline script or style on the page. Because XSS is usually an injected inline script, allowing it defeats most of the point of the policy.
Will CSP break my site?
It can, if you load scripts from sources you did not list. That is why report-only mode exists: it logs what would be blocked without blocking it.
Should I use a meta tag or the header?
Prefer the header. A meta tag cannot set frame-ancestors or report-only, and applies only after the tag is parsed.
Is CSP needed for a small static site?
It is cheap to add a basic one such as default-src 'self'. For sites without user content it is lower priority than HTTPS and HSTS.
Related terms
XSS
Untrusted text runs as script in your visitors' browsers.
Read →Clickjacking
Your site hidden in a frame so users click without knowing.
Read →HSTS
Tells browsers to use only HTTPS for your domain.
Read →Check your own site
Is your CSP, HSTS, framing and referrer policy set up properly?
Open the matching check →
See if your site is affected
Free for any public site, no signup. Need the verified or deep scan? Choose the level on the scan page.
Choose a scan level →