Skip to documentation content

Content security policy

The CSP sources CookieBeam needs, how to load it with a nonce and 'strict-dynamic', why styles need 'unsafe-inline', and how to roll a policy out safely.

Last updated:

If your site sends a Content-Security-Policy header or meta tag, the policy needs to allow what the CookieBeam loader and banner load. A policy that blocks them stops the banner from appearing; the console shows a Refused to load or Refused to connect error naming the blocked URL. Sites without a policy can skip this page.

Sources to allow

DirectiveSourceWhat uses it
script-srchttps://cdn.cookiebeam.comThe loader, the banner script it adds, and the extra bundle for IAB TCF and other frameworks
style-srchttps://cdn.cookiebeam.comThe banner stylesheet
style-src'unsafe-inline'Styles the banner and the blocked-content placeholder add at runtime. See Why styles need 'unsafe-inline'
connect-srchttps://cdn.cookiebeam.comBanner translations, the visitor location lookup for regional rules, and the list of known load-order findings
connect-srchttps://cookiebeam.comConsent records, banner impressions, and drift and load-order reports
img-srchttps://cdn.cookiebeam.comThe banner logo, if you uploaded one

CookieBeam needs no frame-src or font-src entry of its own. Keep the entries your embeds and fonts need: CookieBeam holds embeds back until consent, but it doesn't change where they load from.

Banners with the optional accessibility panel also load the panel module from https://cdn.cookiebeam.com/lib/core/<version>/cookiebeam-a11y-panel.umd.min.js. The existing script-src allowance for https://cdn.cookiebeam.com covers it.

Scripts that CookieBeam blocks and later allows are still subject to your policy. Their hosts must be allowed too, as they would be without CookieBeam.

If you use IAB TCF, the banner also downloads the vendor list. Roll the policy out in report-only mode first (see below) and add any CookieBeam host the reports name.

A minimal policy for a site that loads nothing else:

Content-Security-Policy:
  script-src 'self' https://cdn.cookiebeam.com;
  style-src 'self' 'unsafe-inline' https://cdn.cookiebeam.com;
  connect-src 'self' https://cdn.cookiebeam.com https://cookiebeam.com;
  img-src 'self' https://cdn.cookiebeam.com

With a nonce and 'strict-dynamic'

Many frameworks generate a fresh nonce for every response and allow scripts by nonce instead of by host. CookieBeam works with that pattern, and our own site loads its banner this way.

  1. Give the loader tag the response's nonce:

    <script
      nonce="RANDOM_PER_RESPONSE"
      src="https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js"
      data-cfasync="false" data-no-optimize="1" data-no-defer="1"
      data-no-minify="1" data-noptimize="1" nowprocket="1" data-pagespeed-no-defer="1"
    ></script>
  2. Add 'strict-dynamic' to script-src:

    script-src 'self' 'nonce-RANDOM_PER_RESPONSE' 'strict-dynamic' https://cdn.cookiebeam.com;

With 'strict-dynamic', the browser trusts scripts that an already-trusted script adds to the page. CookieBeam doesn't put a nonce on the scripts it adds: the banner script and bundles the loader fetches, and the external scripts it re-inserts once a visitor consents. 'strict-dynamic' covers those because they load by URL (src). Browsers that support 'strict-dynamic' ignore host sources in script-src, so keep https://cdn.cookiebeam.com in the list for older browsers.

Inline gated scripts are different. A <script type="text/plain" data-category="..."> with its code in the body (no data-src) is re-inserted as an inline script. 'strict-dynamic' doesn't propagate trust to inline scripts, and CookieBeam doesn't add a nonce to the clone. Three options: add a 'sha256-...' hash of the script's content to script-src; add 'unsafe-inline' (this only works when neither a nonce nor 'strict-dynamic' is present, since both cause browsers to ignore 'unsafe-inline'); or convert the inline snippet to an external file and gate it with data-src instead, which 'strict-dynamic' covers.

Without 'strict-dynamic', a nonce only covers the tag that carries it. Allow https://cdn.cookiebeam.com and the host of every script CookieBeam re-inserts after consent in script-src, or those scripts are blocked.

The framework guides show where each framework exposes its nonce, for example %sveltekit.nonce% in SvelteKit or a request header in Next.js middleware. In React-based frameworks that hydrate the whole document, add suppressHydrationWarning to the tag: browsers hide the nonce attribute after parsing, and React would otherwise report a mismatch.

Why styles need 'unsafe-inline'

Scripts load from the CDN as files, so they need no 'unsafe-inline'. Styles are different. The banner and the blocked-content placeholder ("shield") add styles at runtime:

  • The shield builds a <style> element inside its shadow root through innerHTML, and sets sizes and theme colors as inline style attributes.
  • A custom CSS you add to the banner is inserted as a <style> element.

A nonce can't cover these: CookieBeam doesn't add one, and the CSS is generated at runtime, so no hash can be computed ahead of time. Shadow DOM doesn't exempt styles from your policy. Without 'unsafe-inline', the shield renders unstyled and the blocked iframe may stay visible.

So:

  • Include 'unsafe-inline' in style-src. If you set style-src-attr or style-src-elem, include it there too.
  • Don't put a nonce or a hash in style-src. When a directive contains a nonce or hash, browsers ignore its 'unsafe-inline', and the banner's runtime styles are blocked.
  • If you set no style-src, styles fall back to default-src, which then needs 'unsafe-inline' and the CDN host.

Roll out with report-only mode

Test a new or tighter policy without breaking the page:

  1. Send it as Content-Security-Policy-Report-Only instead of Content-Security-Policy. The browser reports violations in the console (and to a reporting endpoint, if you set one) but blocks nothing.
  2. In a private window, load a few page types, accept and reject consent, open the preferences, and visit a page with a blocked embed.
  3. Add every CookieBeam source the reports name, along with your own.
  4. Switch the header to Content-Security-Policy and repeat the check in Verify your installation.