CSP builder: write a Content Security Policy and test it before you deploy
Build a Content-Security-Policy header from presets and toggles, or paste the one your site sends now to edit it. Each weak spot is explained in one line as you go. Then the step a generator alone cannot give you: the page turns the policy into a HeaderDial profile that adds it to your real site in your own browser, as Content-Security-Policy-Report-Only, so the DevTools Console shows what it would block before you change the server. Everything runs in this page: it makes no network requests after it has loaded, and nothing you paste is sent anywhere.
Read locally. Nothing you paste leaves this tab.
| Use | Directive | Sources (toggle keywords; type hosts, schemes or hashes separated by spaces) |
|---|
Your policy
Test it on your site with HeaderDial
What a Content Security Policy is
A Content Security Policy is a response header that tells the browser which sources a page may load scripts, styles, images, frames and connections from, and whether inline code may run. If an attacker manages to inject a <script> into your page (cross-site scripting), a policy that does not allow it stops it from running. It is defined in the W3C's Content Security Policy Level 3, and all current browsers apply it; the warnings note the few directives where support differs.
A policy is a list of directives separated by semicolons. Each directive names a kind of resource (script-src, img-src…) and lists allowed sources: keywords in single quotes such as 'self' (the page's own origin) and 'none', schemes such as https: or data:, hosts such as https://cdn.example.com or *.example.com, and nonces or hashes for specific inline scripts. A -src directive you leave out falls back to default-src. base-uri, form-action and frame-ancestors do not fall back: without them, nothing is restricted.
Roll out a policy with Report-Only
A new policy almost always blocks something you forgot: an analytics snippet, a font host, an inline onclick. Content-Security-Policy-Report-Only takes the same value but blocks nothing; the browser only reports what the policy would have blocked. The usual order:
- Try it in your own browser. Build the policy above, enter your host, keep Report-Only, and import the profile into HeaderDial. Browse the pages that matter with DevTools open, including logged-in pages and checkout.
- Fix the list. For each report, either add the source you trust to the right directive, or change the page (move inline scripts into files, replace
onclick=""attributes withaddEventListener). Click Copy share link again and re-import, or edit the row in HeaderDial directly. - Enforce it in your browser. Switch the mode to Enforce and check that the pages still work.
- Deploy as Report-Only. Send the header from your server with
report-to(andreport-uri) so you collect violations from real visitors' browsers, extensions and pages you did not visit. - Enforce on the server when the reports are only things you want blocked. Keep reporting on.
See violations in DevTools
- Chrome and Edge: open DevTools (F12), Console panel, and reload. Each violation is a message naming the blocked URL and the directive; for a Report-Only policy, Chrome starts it with
[Report Only]. The Issues panel groups them as Content Security Policy issues. - Firefox: the Console panel also logs a message per violation, naming the blocked resource and the directive. If you see nothing, check that the Console's error and warning filters are on.
- To see the header itself, open the Network panel, select the document request and look under Response Headers. The header added by HeaderDial is listed there.
- Reload rather than navigate inside a single-page app: a policy applies when the document loads. Tick Disable cache in the Network panel, and bypass any service worker (Application › Service workers), or a cached page may not get the new header.
What the HeaderDial test does
The profile sets your policy with a response-header row whose operation is set, limited to your host by a Domain filter (the line under the buttons lists every row it will hold). A set row replaces any header of the same name the server sends (the same behaviour as in modify Content-Security-Policy and other response headers). What that means for each mode:
- Report-Only sets
Content-Security-Policy-Report-Only. If the site already sends an enforcedContent-Security-Policy, that policy keeps applying next to yours: browsers apply every policy they receive, so a resource must pass the site's policy as before, and yours only reports. Tick Also remove the site's own Content-Security-Policy to add a remove row for it, which shows your policy's reports without the old one in the way. While that profile is on, the site runs without its own enforced policy in your browser. - Enforce sets
Content-Security-Policy, which replaces the site's own header of that name, so your policy is enforced instead of the site's. AContent-Security-Policy-Report-Onlyheader the site sends keeps reporting. - A policy in a
<meta http-equiv="Content-Security-Policy">tag is part of the HTML, not a header. A header rule neither removes nor replaces it, so it applies as well. Look in the page source if violations persist. - When the policy uses
report-toand you enter an endpoint URL in that row, the profile also setsReporting-Endpoints, so the browser knows where to send reports. Reports then go from your browser to that URL. - Nonces cannot be tested this way: a header rule sends the same text on every response, while the server writes a new nonce into its
<script nonce>attributes each time. Test a nonce-based policy on a staging server instead, or test the rest of the policy with HeaderDial first.
Common mistakes
- Keywords without quotes.
script-src selfallows a host namedself, not your origin. Keywords take single quotes:'self','none','unsafe-inline'. 'unsafe-inline'inscript-src. Without a nonce or hash it lets any injected inline script run, which removes most of the protection. With a nonce or hash in the same list, browsers ignore'unsafe-inline', so the pair is a normal fallback for old browsers.- Broad script sources.
*,https:ordata:inscript-srclets an attacker load a script from almost anywhere. List hosts. - Forgetting the directives without a fallback.
object-srcfalls back todefault-src, butbase-uri,form-actionandframe-ancestorsdo not.object-src 'none'; base-uri 'self'belongs in almost every policy. 'none'with other sources. It is ignored when anything else is listed in the same directive.- Wildcards and schemes.
*.example.commatches subdomains but notexample.comitself, and'self'does not include subdomains. A host without a scheme uses the page's scheme: on an https page,cdn.example.commeanshttps://cdn.example.com. - Header-only directives in
<meta>. Browsers ignoreframe-ancestors,report-uriandsandboxin a meta tag, and Report-Only cannot be set with one at all. report-toalone. It names a group defined in aReporting-Endpointsheader; without that header no reports are sent. Browsers that do not supportreport-toonly send reports toreport-uri, so many sites set both whilereport-uriis still supported.- Two policies. When a server (or a CDN, or a framework) sends two
Content-Security-Policyheaders, both are enforced and a resource must pass each one. Adding sources to one of them does not loosen the other. - A fixed nonce. A nonce in a static config file is the same on every page, so an attacker can copy it. It has to come from the server, freshly generated per response.
What this builder does not do
- It does not load your site. Which sources your pages need is what the HeaderDial test shows you.
- It checks the weak spots listed above, not the full grammar: a malformed host or a typo in a hash is passed through as typed.
- It does not compute hashes of inline scripts. Chrome's Console message for a blocked inline script includes the
'sha256-…'value to add. - Directives without a row here (
script-src-elem,sandbox, Trusted Types…) are kept in Other directives and only checked for removed or unknown names.
Questions
Can I test a Content Security Policy without deploying it?
Yes. A browser extension that modifies response headers can add the policy to the pages your browser receives from the live site. Sent as Content-Security-Policy-Report-Only, it only logs what it would block in the DevTools Console. Only your browser is affected. HeaderDial does this in Chrome, Edge and Firefox 128 or later; the share link above sets it up.
Is Content-Security-Policy-Report-Only safe to send in production?
It blocks nothing, so it cannot break the page. It costs a console message per violation, and network requests if you set report-to or report-uri. It also protects nothing, so it is a step towards an enforced policy, not a replacement.
Do I need report-uri or report-to to see violations?
No. Browsers log violations in the DevTools Console without them. Reporting directives are for collecting violations from other people's browsers on your server.
Why does the old policy still block things during my test?
In Report-Only mode the site's own enforced policy still applies, and it may block things your new policy would allow. Tick Also remove the site's own Content-Security-Policy, or switch to Enforce, which replaces it. If that does not help, the page has a policy in a <meta> tag, or a cached or service-worker response did not get the new header.
Why can't a nonce-based policy be tested with a fixed header?
A nonce must be a new random value on every response, generated by the server and repeated in each script tag's nonce attribute. A header rule in the browser always sends the same text, so it cannot match the server's nonces.
Sources
The warnings follow these documents; the wording on this page is our own.
- W3C, Content Security Policy Level 3 (directive fallback,
'strict-dynamic', nonces and hashes disabling'unsafe-inline', directives ignored in<meta>) - MDN, Content-Security-Policy, Content-Security-Policy-Report-Only, report-to and Reporting-Endpoints
- W3C, Upgrade Insecure Requests; W3C, Reporting API
- web.dev, Mitigate cross-site scripting with a strict Content Security Policy (the nonce +
'strict-dynamic'preset)
Add to ChromeFirefox: coming soon