Skip to main content
Free tool

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
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-requests

This 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.

About the tool

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.

In practice

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 Policy
Step by step

How 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.

  1. 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 fetch calls, including analytics, chat and maps.

  2. Build the policy from a baseline

    Start with default-src 'self' and add only the sources you found. Add object-src 'none', base-uri 'self' and frame-ancestors, because without them the policy leaves known gaps open.

  3. 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.

  4. Replace inline scripts with a nonce or a hash

    Every <script> without a src attribute needs a nonce or a hash instead of 'unsafe-inline'. Generate the nonce on the server, fresh for every response.

  5. 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.

Examples

Example CSP server configurations

example.com and the third-party sources are placeholders. Replace them with the ones you listed in the first step.

Nginx, baseline policy
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.

Keywords without quotes
Before
Content-Security-Policy: script-src self
After
Content-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.

Report-Only with a reporting endpoint
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 mistakes

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 skip frame-ancestors, report-uri and sandbox. Framing protection can only be set with an HTTP header.

  • A wildcard or bare https: as a script source

    script-src * or script-src https: allows scripts from any domain, including someone else's server. List specific hosts or use a nonce.

Numbers and sources

Content-Security-Policy in the spec and in the data

ValueWhat it meansSource
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
nonceWhen 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-OnlyThe Content-Security-Policy-Report-Only header reports violations without blocking and is not supported in a meta element.MDN, CSP Report-Only
report-toThe report-uri directive is deprecated in favour of report-to, which builds on the Reporting API.W3C, CSP Level 3
3 directivesframe-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:

Glossary

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.
Security module in Insight

File or code ready? Check the result on the live page

TLS, headers, cookies and exposure, with a score breakdown and a remediation plan.