Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Inline base64 images do not show because of the Content Security Policy

ErrorRefused to load the image 'data:image/png;base64,iVBORw0KGgo...' because it violates the following Content Security Policy directive: "img-src 'self'".

Short answer

A base64 image has a data: URL, and a policy such as default-src 'self' does not allow that scheme. Images are governed by img-src, and by default-src when img-src is absent. Add data: to img-src, check for a second policy that also blocks it, and keep the list of other sources short.

  • Applies to Chrome current release, Firefox current release
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find which directive is named in the console message, then look for img-src in the real response headers. If it is absent, default-src is the one in force.

    curl -sI https://example.com/ | grep -i "content-security-policy" | tr ';' '\n' | grep -iE "default-src|img-src"
  2. 2Match what you see, then jump to its fix

    1. 1The message ends with img-src 'self' or img-src https://cdn.example.com.img-src lists sources but not data:Fix
    2. 2The message names default-src, not img-src.There is no img-src, and default-src decidesFix
    3. 3You changed the header, yet the same message appears.A second policy still blocks data:Fix

What does the error look like?

  • Refused to load the image 'data:image/png;base64,iVBORw0KGgo...' because it violates the following Content Security Policy directive: "img-src 'self'".
  • Refused to load the image 'data:image/svg+xml;base64,...' because it violates the following Content Security Policy directive: "default-src 'self'".

Cause 1 of 3

img-src lists sources but not data:

1Check

MDN describes img-src as the directive for images and favicons, and it accepts scheme sources. The CSP specification says non-HTTP schemes have to appear in the source list, so an image whose URL starts with data: is blocked unless data: is there.

curl -sI https://example.com/ | grep -i "content-security-policy" | tr ';' '\n' | grep -i "img-src"

2Fix

Add the data: scheme to img-src. Keep your other sources as they are.

Content-Security-Policy: default-src 'self'; img-src 'self' data:

3Verify

Reload without cache. The inline images should render and the console should be quiet.

curl -sI https://example.com/ | grep -i "img-src"

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

There is no img-src, and default-src decides

1Check

MDN says that when img-src is absent the browser uses default-src. With default-src 'self' every image that is not from your own origin is refused, data: ones included.

curl -sI https://example.com/ | grep -i "content-security-policy" | tr ';' '\n' | grep -iE "default-src|img-src"

2Fix

Add a separate img-src. That way you open only images and leave scripts, frames and the rest on default-src.

Content-Security-Policy: default-src 'self'; img-src 'self' data:

3Verify

Reload and check the console for the violation message.

curl -sI https://example.com/ | grep -i "content-security-policy"

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

A second policy still blocks data:

1Check

The CSP specification says several policies can apply to one resource, and each enforced policy is checked. A meta tag in the HTML, a CDN rule or a framework middleware may add one next to yours.

curl -s https://example.com/ | grep -i "http-equiv=.content-security-policy"
curl -sI https://example.com/ | grep -ic "content-security-policy"

2Fix

Change the policy where it really comes from, or make every enforced policy allow data: for images. Prefer one policy.

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src 'self' data:">

3Verify

Reload the page and confirm only the expected policy is sent.

curl -sI https://example.com/ | grep -i "content-security-policy"

Confirmed by w3.org

Did this fix it?

How do I stop it coming back?

  • Write img-src explicitly, with the data: scheme only if you really inline images.
  • Prefer files served from your own origin for large images; base64 inflates the HTML.
  • Trial each policy change with the Report-Only header before it is enforced.
  • Keep one place that sets the policy.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Security

The browser blocks a script because of your Content Security Policy

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

The page's Content-Security-Policy does not allow what it tried to run or load.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.