HeaderDial › Guides

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.

Start from

Read locally. Nothing you paste leaves this tab.

    UseDirectiveSources (toggle keywords; type hosts, schemes or hashes separated by spaces)

    Your policy

    
      

      Test it on your site with HeaderDial

      Your browser only. The profile changes the headers your browser receives from the host you name. Visitors keep getting the server's headers until you deploy the change, so a test cannot break the site for anyone else.

      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:

      1. 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.
      2. 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 with addEventListener). Click Copy share link again and re-import, or edit the row in HeaderDial directly.
      3. Enforce it in your browser. Switch the mode to Enforce and check that the pages still work.
      4. Deploy as Report-Only. Send the header from your server with report-to (and report-uri) so you collect violations from real visitors' browsers, extensions and pages you did not visit.
      5. Enforce on the server when the reports are only things you want blocked. Keep reporting on.

      See violations in DevTools

      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:

      Common mistakes

      What this builder does not do

      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.

      Add to ChromeFirefox: coming soon