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
| Directive | Controls | Covered by default-src? |
|---|---|---|
script-src | JavaScript. The one that matters most. | Yes |
style-src | Stylesheets and inline styles. | Yes |
img-src | Images, including data: URIs. | Yes |
connect-src | fetch, XHR, WebSocket, EventSource, beacons. | Yes |
font-src | Web fonts. | Yes |
frame-src | What you may embed. | Yes |
object-src | Plugins. Set to 'none'. | Yes |
base-uri | The <base> tag. | No |
form-action | Where forms may submit. | No |
frame-ancestors | Who may frame you. | No |
sandbox | Applies iframe-style restrictions. | No |
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:
- '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.
- 'strict-dynamic'Scripts loaded by an already-trusted script inherit trust. Without it, any loader or analytics bootstrapper breaks.
- https:Ignored entirely by browsers that support strict-dynamic. It is a fallback for older browsers that do not.
- 'unsafe-inline'Also ignored whenever a nonce is present. Purely a fallback for browsers predating nonce support. It is not a hole.
- object-src 'none'Removes the legacy plugin attack surface entirely.
- base-uri 'none'Blocks the base-tag hijack that would otherwise re-point relative URLs.
Rolling it out without breaking the site
- Start in Report-OnlySend
Content-Security-Policy-Report-Only. Nothing is blocked and every violation is reported. - Collect for one to two weeksLong enough to catch weekly jobs, campaign scripts and the pages nobody visits on a Tuesday.
- Triage the reportsExpect noise: browser extensions generate violations you cannot fix and should ignore. Look for your own origins and your real vendors.
- Tighten and repeatAdd what is genuinely needed, remove what is not, and let it run again.
- EnforceRename the header. Keep the reporting endpoint — it is now your early warning that someone shipped an inline script.
- 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.
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 security tools
- security.txt Generator — publish a vulnerability disclosure contact.
- X-Robots-Tag Generator — indexing headers for files and directories.
- .htaccess Redirect Generator — Apache rules, including header setup.
- Nginx Redirect Generator — server config for the same stack.
- Random Byte Generator — entropy for nonces and keys.