CSP generator
Build a Content-Security-Policy, locally in your browser
- Free
- No account required
Directives
Policy review
No weak values in the policy text. Run it as Report-Only before you enforce it.
Content-Security-Policy: default-src 'self';
script-src 'self';
style-src 'self';
img-src 'self' data:;
font-src 'self';
connect-src 'self';
frame-ancestors 'none';
form-action 'self';
base-uri 'self';
object-src 'none';
upgrade-insecure-requestsThis CSP generator assembles a Content-Security-Policy header from the directives you choose and flags the values that genuinely weaken it: unquoted keywords, unsafe-inline in scripts, wildcards, and missing defensive directives. It reviews the policy text, not your site. Whether a page loads under it is something only a browser can tell you.
What weakens a CSP
The quotes are not cosmetic. script-src self and script-src 'self' are different policies: the first allows a host named "self", which does not exist. The policy still applies, it just means something other than intended. This is the most common silent CSP mistake.
unsafe-inline in script-src defeats the point. CSP mostly protects against injected inline script executing; allowing it back leaves that door open. Where it cannot be avoided, use a nonce or a hash. Modern browsers then ignore unsafe-inline anyway.
Three directives are easy to forget and worth adding: base-uri closes off base-tag injection, frame-ancestors replaces X-Frame-Options, and object-src 'none' shuts down the old plugin vector.
This reviews the policy text. Whether your site loads under it depends on what it actually requests. Find that out with Report-Only before enforcing.
Observe violations first
Build a policy around the resources your site actually uses. Test it in staging as Content-Security-Policy-Report-Only. Exercise login, forms, payments and integrations; loading the homepage alone is not enough.
MDN, Content Security PolicyHow to set up a Content-Security-Policy header
You roll out a Content-Security-Policy in four steps: list your resources, run the policy in report mode, then switch to a policy that actually blocks. The order keeps the site from breaking on launch day.
-
List where the page loads resources from
Open DevTools, go to the Network tab and walk through your key pages. Note the domains for scripts, styles, fonts, images and
fetchcalls, including analytics, chat and maps. -
Build the policy from a baseline
Start with
default-src 'self'and add only the sources you found. Addobject-src 'none',base-uri 'self'andframe-ancestors, because without them the policy leaves known gaps open. -
Send it as Content-Security-Policy-Report-Only
A Report-Only policy blocks nothing and reports violations in the console and to the endpoint named in
report-to. Leave it on for a few days of traffic and add the sources you missed. -
Replace inline scripts with a nonce or a hash
Every
<script>without asrcattribute needs a nonce or a hash instead of'unsafe-inline'. Generate the nonce on the server, fresh for every response. -
Switch to Content-Security-Policy and check the header
Rename the header to the enforcing one and check that it reaches the browser intact. A CDN or proxy can shorten or replace the policy on the way.
Example CSP server configurations
example.com and the third-party sources are placeholders. Replace them with the ones you listed in the first step.
add_header Content-Security-Policy "default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'none'; form-action 'self'" always; The always parameter adds the header to error responses too. Without it, nginx only adds it for the 2xx and 3xx codes listed in its documentation.
Content-Security-Policy: script-src selfContent-Security-Policy: script-src 'self'Without quotes, self is a hostname. The policy still applies, but it allows a server that does not exist instead of your own origin.
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports"
Content-Security-Policy-Report-Only: default-src 'self'; report-to csp-endpoint Reports only go to an endpoint declared in Reporting-Endpoints. Without a report-to directive you will see violations in the console only.
Common CSP rollout mistakes
-
Enforcing without a Report-Only phase
A policy enforced on day one blocks resources nobody remembered, such as a payment script or a reviews widget. A few days in Report-Only shows them with no risk.
-
Keeping 'unsafe-inline' in script-src
'unsafe-inline'lets any script injected into the HTML run, which is exactly what CSP is meant to stop. A nonce or a hash allows only the scripts you marked yourself. -
A fixed nonce in the template
A nonce only works when an attacker cannot guess it. A value hardcoded in a template or stored in a page cache is the same for everyone, so an attacker knows it.
-
A meta policy with directives that do not work there
In a
<meta>tag, browsers skipframe-ancestors,report-uriandsandbox. Framing protection can only be set with an HTTP header. -
A wildcard or bare https: as a script source
script-src *orscript-src https:allows scripts from any domain, including someone else's server. List specific hosts or use a nonce.
Content-Security-Policy in the spec and in the data
| Value | What it means | Source |
|---|---|---|
| 21.9% | Share of pages sending a Content-Security-Policy header in the HTTP Archive crawl of July 2025, up from 18.5% a year earlier. | Web Almanac 2025, Security |
| nonce | When a directive contains a nonce or a hash, browsers ignore 'unsafe-inline' in it. The nonce must differ for every HTTP response and must not be predictable. | MDN, Content Security Policy |
| Report-Only | The Content-Security-Policy-Report-Only header reports violations without blocking and is not supported in a meta element. | MDN, CSP Report-Only |
| report-to | The report-uri directive is deprecated in favour of report-to, which builds on the Reporting API. | W3C, CSP Level 3 |
| 3 directives | frame-ancestors, report-uri and sandbox are ignored when the policy arrives in a meta element instead of a header. | W3C, CSP Level 3, 3.3 |
Sources checked:
Parts of a policy
- ''Keywords
- 'self', 'none' and 'unsafe-inline' need quotes. Unquoted they are read as hostnames.
- NNonce
- A one-time token on a script tag. Its presence makes modern browsers ignore unsafe-inline.
- FAframe-ancestors
- Who may embed your page in a frame. The modern equivalent of X-Frame-Options.
- ROReport-Only
- The policy reports violations without blocking anything. The safe way to roll one out.
Fill in the directives you need, or start from the starter policy, and copy the finished header. The tool flags weakening values and missing defensive directives as you go. Everything is computed locally in your browser.
Because without them the browser reads it as a hostname. script-src self allows a server called "self" rather than the page’s own origin. The policy still applies, it just means something other than what you intended.
Partly. The <meta http-equiv> form works for most directives, but frame-ancestors, report-uri, report-to and sandbox are ignored in it, silently. That is why this generator strips them from the meta output and names what it removed.
With Report-Only. The policy then reports violations without blocking anything, so you can see what would break before you enforce it.
No. It reviews the policy you build here. Whether your site loads under it depends on the resources it requests. That is for a browser to answer, ideally in Report-Only mode.
No. CSP is a second layer of defence against XSS that limits the damage when sanitization fails. MDN recommends both: sanitizing input in the application and sending a CSP header.
A nonce is a random value the server puts in the script-src directive and in the nonce attribute of selected scripts. The browser only runs scripts with a matching nonce, and 'unsafe-inline' in the same directive stops working. The value must be new for every response.
CSP Level 3 marks report-uri as deprecated and replaces it with report-to plus a Reporting-Endpoints header. If you also need reports from older browsers, send both directives, and newer browsers will use report-to.
What else might come in handy
File or code ready? Check the result on the live page
TLS, headers, cookies and exposure, with a score breakdown and a remediation plan.