Home/Security & Business/Content Security Policy Generator

Content Security Policy Generator

Build a Content Security Policy that actually stops XSS — nonces and strict-dynamic rather than an allowlist you can bypass, with an analysis pass that flags the directives a policy usually forgets.

Free Browser-based No signup Copy-ready
Generator Settings
Used for report endpoints and absolute examples. 'self' covers your own origin in the policy itself.
Third-party services you use
Each one adds the exact origins that service needs — nothing wider.
One per line as directive origin. Anything you add here is merged into that directive.
Hardening
Deployment
Generated Policy ready
Content-Security-Policy-Report-Only

      

How it works

1

Pick a strategy

Strict nonce-based, a host allowlist, or a permissive report-only starter.

2

Add your third parties

Each service contributes only the origins it genuinely needs.

3

Ship Report-Only first

Collect real violations for a week or two, then flip to enforcing.

Why allowlists fail and nonces do not

The intuitive way to write a CSP is to list the origins you trust: script-src 'self' https://cdn.example.com. Research on real-world policies found the overwhelming majority of allowlist-based policies were trivially bypassable, and the reason is uncomfortable: you are not trusting an origin, you are trusting every file on it.

The JSONP problem

Large CDNs and API hosts often expose a JSONP endpoint that reflects a caller-supplied callback name into executable JavaScript. If that host is on your allowlist, an attacker points a script tag at it and runs arbitrary code — fully permitted by your policy. One friendly-looking CDN entry can undo the whole thing.

Allowlists also rot

Every new vendor means another origin, and nobody ever removes one. Policies drift towards permissive over time because tightening them risks breaking a page nobody owns any more.

What a nonce changes

A nonce is a fresh random token per response, echoed on each script tag you intended to include. An injected script has no way to know it, so it cannot execute — regardless of which host it came from. Trust becomes a property of your markup rather than of somebody else's domain.

strict-dynamic makes it practical

Nonces alone break loaders that inject further scripts. 'strict-dynamic' propagates trust: a script you nonced may load others. That is what makes a strict policy survive contact with real analytics and tag libraries.

The nonce must be cryptographically random and regenerated on every request. A nonce baked into a static HTML file, or reused across a cached page, is public knowledge and provides no protection at all — it is the most common way a strict-looking policy turns out to be decorative.

Frequently Asked Questions

View all FAQs
What is a Content Security Policy?
An HTTP response header that tells the browser which sources of script, style, images and other resources are permitted on your pages. Anything not permitted is blocked, which turns most cross-site scripting bugs from an exploit into a console error.
Should I start with enforcing or Report-Only?
Always Report-Only. It blocks nothing and reports everything, so you discover what a real policy would break using real traffic rather than guesswork. Run it for one to two weeks, fix the reports, then rename the header to enforce.
Why is unsafe-inline in my strict policy?
As a deliberate fallback. When a nonce is present, modern browsers ignore 'unsafe-inline' entirely; only older browsers that do not understand nonces use it. Removing it makes no difference to modern security and breaks legacy clients.
Does default-src cover everything?
No, and this is the most common mistake. base-uri, form-action, frame-ancestors, report-uri and sandbox all ignore default-src and must be set explicitly.
Can I put CSP in a meta tag?
Partly. A meta tag works for most fetch directives but browsers ignore frame-ancestors, report-uri, report-to and sandbox there, and it only applies from the point it is parsed. Use a real header whenever you control the server.
Will CSP break Google Tag Manager?
Potentially, and by design — GTM exists to inject arbitrary tags. Containers using custom HTML tags need 'unsafe-inline', which defeats the policy. Either use GTM's nonce support with a strict policy, or restrict the container to native tag templates.

Content Security Policy: nonces, strict-dynamic and a rollout that works

This CSP generator builds a strict, nonce-based Content Security Policy — or a host allowlist if you need one — with third-party presets and an analysis pass that catches the directives most policies forget. Below: how CSP stops XSS, why allowlists usually fail, and how to deploy without breaking your site.

What CSP actually does

A Content Security Policy is a response header listing where the browser may load each type of resource from. Anything outside the policy is refused and reported.

Its main job is turning cross-site scripting from a compromise into a logged error. An XSS bug lets an attacker place a script into your page; a good CSP means the browser declines to execute it. The bug still exists and still needs fixing — but it is no longer an incident.

It is defence in depth, not a substitute for escaping output. A policy makes exploitation hard; correct encoding makes injection impossible. You want both.

The directives, and what falls through

DirectiveControlsCovered by default-src?
script-srcJavaScript. The one that matters most.Yes
style-srcStylesheets and inline styles.Yes
img-srcImages, including data: URIs.Yes
connect-srcfetch, XHR, WebSocket, EventSource, beacons.Yes
font-srcWeb fonts.Yes
frame-srcWhat you may embed.Yes
object-srcPlugins. Set to 'none'.Yes
base-uriThe <base> tag.No
form-actionWhere forms may submit.No
frame-ancestorsWho may frame you.No
sandboxApplies iframe-style restrictions.No
The bottom four are the gap in most hand-written policies. People set a tight default-src and assume it covers everything. It does not: without base-uri, an injected <base href> silently re-points every relative URL on the page, including your script tags.

Why host allowlists usually fail

The obvious policy names the origins you trust. The problem is that an origin is not a unit of trust — it is thousands of files maintained by someone else.

  • JSONP endpoints. Many large hosts expose an endpoint that reflects a callback parameter into executable JavaScript. Allowlist that host and an attacker has arbitrary code execution, fully within your policy.
  • Bundled libraries. Public CDNs host frameworks with known gadget chains that convert injected markup into script execution.
  • User content. Any allowlisted origin that serves user-uploaded files is an open door.
  • Drift. Origins accumulate as vendors are added and are never removed, because removing one risks breaking a page nobody owns.

Studies of real deployed policies have repeatedly found the large majority of allowlist-based CSPs to be bypassable in practice. This is why Google's own guidance moved to nonce-based policies.

The strict policy, explained line by line

The modern recommendation looks like this:

script-src 'nonce-{RANDOM}' 'strict-dynamic' https: 'unsafe-inline'; object-src 'none'; base-uri 'none';

Each part earns its place, and two of them look wrong until you know why they are there:

  1. 'nonce-{RANDOM}'A fresh random token per response, echoed on every script tag you meant to include. Injected scripts cannot guess it, so they do not run.
  2. 'strict-dynamic'Scripts loaded by an already-trusted script inherit trust. Without it, any loader or analytics bootstrapper breaks.
  3. https:Ignored entirely by browsers that support strict-dynamic. It is a fallback for older browsers that do not.
  4. 'unsafe-inline'Also ignored whenever a nonce is present. Purely a fallback for browsers predating nonce support. It is not a hole.
  5. object-src 'none'Removes the legacy plugin attack surface entirely.
  6. base-uri 'none'Blocks the base-tag hijack that would otherwise re-point relative URLs.
The nonce must be cryptographically random and different on every response. Generating it at build time, or caching a page that contains it, makes it public and the policy decorative. If your pages are served from a CDN cache, the nonce must be injected at the edge.

Rolling it out without breaking the site

  1. Start in Report-OnlySend Content-Security-Policy-Report-Only. Nothing is blocked and every violation is reported.
  2. Collect for one to two weeksLong enough to catch weekly jobs, campaign scripts and the pages nobody visits on a Tuesday.
  3. Triage the reportsExpect noise: browser extensions generate violations you cannot fix and should ignore. Look for your own origins and your real vendors.
  4. Tighten and repeatAdd what is genuinely needed, remove what is not, and let it run again.
  5. EnforceRename the header. Keep the reporting endpoint — it is now your early warning that someone shipped an inline script.
  6. Watch the first 24 hoursThis is when a missed third party surfaces, usually on checkout or a form nobody tested.

Reporting endpoints

A policy without reporting tells you nothing. You find out it is broken when a customer emails.

There are two mechanisms, and the sensible move is to send both. report-uri is deprecated but is what Safari and older browsers understand. report-to is the modern replacement and requires a matching Reporting-Endpoints response header naming the group.

Whatever receives the reports must tolerate volume and nonsense. Browser extensions, injected ISP scripts and antivirus tooling all generate violations that are not your bug, and a busy site can produce a lot of them. Aggregate by directive and blocked URI rather than reading them individually.

Keep reporting switched on after you enforce. It becomes a regression alarm: the day a developer adds an inline script or marketing pastes in a new tag, you hear about it from the report stream rather than from a blank page.

What commonly breaks

Inline event handlers

onclick="…" attributes are blocked by any policy without 'unsafe-inline', and a nonce cannot rescue them. They must move into external scripts.

javascript: URLs

href="javascript:…" is blocked. Usually found in legacy templates and third-party widgets.

eval in libraries

Some older templating and date libraries call eval or new Function. They need replacing, not exempting.

Inline styles

Many frameworks set style attributes at runtime. 'unsafe-inline' in style-src is a far smaller risk than in script-src and is usually the pragmatic call.

Tag managers

Custom HTML tags in GTM are arbitrary script injection by design. Use nonce support or restrict the container to native templates.

Browser extensions

They generate violation reports constantly. Not your bug, and not fixable from your side — filter them out of your triage.

Related searches

content security policy generatorcsp generatorcsp header examplestrict-dynamic nonce cspcsp report-onlyscript-src unsafe-inlineframe-ancestorscsp base-urihow to implement cspcsp nginx headercsp meta tagcsp google tag manager