Guides

How to fix “violates the following Content Security Policy directive”

This error means the page has a Content Security Policy and something on it is not allowed. Find the directive named in the message, then either allow the source or change your code.

Reading the message

In Chrome and Edge:

Loading the script 'https://cdn.example.com/a.js' violates the following Content Security Policy directive: "default-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'default-src' is used as a fallback. The action has been blocked.

It tells you what was blocked, and the directive that blocked it. If that was a fallback, the note names the more specific directive to add. Older versions start with “Refused to load the script” instead, and Firefox says “The page's settings blocked”.

If it ends with “The policy is report-only, so the violation has been logged but no further action has been taken”, nothing was blocked and the fault is somewhere else.

A script from another site

Add its origin to script-src. If you do not have that directive yet, copy what default-src has into it first, because it replaces the fallback.

script-src 'self' https://cdn.example.com

Scripts often load more from other hosts, so expect another message after this one. For common services, see the sources they document.

Executing inline script

The script is written in the HTML. Moving it to a file is simplest. If you cannot, give it a nonce: a random value, new for every response, in both the header and the tag.

Content-Security-Policy: script-src 'self' 'nonce-R4nd0mV4lue'

<script nonce="R4nd0mV4lue">…</script>

If the script never changes, use the hash from the message, such as 'sha256-a4ayc…'. Avoid 'unsafe-inline', which also allows any script an attacker manages to inject.

Executing inline event handler

This is an attribute such as onclick. Nonces do not work on attributes, so attach the handler from a script.

<button id="buy">Buy</button>

document.querySelector("#buy").addEventListener("click", buy);

Applying inline style, or a stylesheet

For a stylesheet on another site, add its origin to style-src. For a style attribute, use a class, or set the property from a script with element.style, which CSP allows. For a <style> block, use a nonce or hash.

Many libraries need style-src 'unsafe-inline'. It is a smaller risk than for scripts, so it is a reasonable place to compromise.

Connecting to an address

A fetch, XMLHttpRequest, EventSource or WebSocket was blocked. Add the origin to connect-src. WebSockets need their own scheme.

connect-src 'self' https://api.example.com wss://live.example.com

Images and fonts

Use img-src and font-src. Images and fonts embedded in the page need data:, which is fine here but not in script-src.

img-src 'self' data: https://images.example.com; font-src 'self' https://fonts.gstatic.com

Framing

If your page embeds another site, add it to frame-src. If another page is trying to embed yours, the directive is frame-ancestors, and it has to be in the header of the page being embedded.

frame-src https://www.youtube.com
frame-ancestors 'self' https://partner.example

Evaluating a string as JavaScript

Something calls eval, new Function, or setTimeout with a string. It is often a template library or a development build, so check whether a production build removes it. For WebAssembly, 'wasm-unsafe-eval' is enough.

Fixes to avoid

  • script-src *, https: or data:, which let an attacker load a script from anywhere
  • turning the policy off because one script broke, when a report-only policy would let you fix it calmly
  • allowing a host you do not recognise: the policy may have just stopped something

Seeing it on a site that is not yours

Only the site's owner can change its policy. If the site is broken for you, try a private window with extensions off, since an extension may be what was blocked.

Last checked 24 November 2026.

Read next