Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Strapi admin blocks uploaded images and media because of its CSP

ErrorRefused to load the image 'https://res.cloudinary.com/...' because it violates the following Content Security Policy directive: "img-src 'self' data: blob: market-assets.strapi.io".

Short answer

Strapi's default strapi::security middleware limits images and media to 'self'. When uploads live on S3, Cloudinary or another host, the admin panel blocks them. Replace the plain string with an object in config/middlewares and add the provider's domain to img-src and media-src. Check no environment file overrides it.

  • Applies to Strapi 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the host named in the console violation, then look at how the security middleware is listed in your config. A bare string means the defaults are in force.

    grep -n "security" config/middlewares.js config/middlewares.ts config/env/*/middlewares.* 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The blocked URL is your S3 bucket or Cloudinary host, and the directive is img-src or media-src.The upload provider's domain is not in img-src and media-srcFix
    2. 2The edit looks right but production still blocks, while development works.An environment middleware file replaces your changeFix

What does the error look like?

  • Refused to load the image 'https://res.cloudinary.com/...' because it violates the following Content Security Policy directive: "img-src 'self' data: blob: market-assets.strapi.io".
  • Refused to load media from 'https://bucket.s3.eu-west-1.amazonaws.com/...' because it violates the following Content Security Policy directive: "media-src 'self' data: blob:".

Cause 1 of 2

The upload provider's domain is not in img-src and media-src

1Check

The Strapi docs say the default security middleware has a strict contentSecurityPolicy that limits images and media to 'self', and that an external upload provider's domain must be allowed. Your config/middlewares file probably still has the string 'strapi::security'.

grep -n "strapi::security" config/middlewares.*

2Fix

Replace the string with an object that keeps useDefaults and lists your provider host in both directives. The Strapi example for S3 also keeps data: and blob: and sets connect-src to 'self' and https:.

// config/middlewares.js
module.exports = [
  'strapi::logger',
  'strapi::errors',
  {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          'connect-src': ["'self'", 'https:'],
          'img-src': ["'self'", 'data:', 'blob:', 'market-assets.strapi.io', 'res.cloudinary.com'],
          'media-src': ["'self'", 'data:', 'blob:', 'market-assets.strapi.io', 'res.cloudinary.com'],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];

3Verify

Restart Strapi, open the Media Library and confirm the previews load with no violation in the console.

npm run develop

Confirmed by docs.strapi.io

Did this fix it?

Cause 2 of 2

An environment middleware file replaces your change

1Check

The Strapi Cloud docs state that a config/env/production/middlewares file fully replaces the global middleware array. If it exists, your edit in config/middlewares is not used in production.

ls config/env/production/

2Fix

Put the same security object in the environment file, and keep the whole middleware array there, because it replaces the global one entirely.

// config/env/production/middlewares.js: same array as above,
// with the 'strapi::security' object including your provider domain

3Verify

Deploy, then open the Media Library in production and read the console.

npm run build

Confirmed by docs.strapi.io

Did this fix it?

How do I stop it coming back?

  • Whenever you add an upload provider, update the security middleware in the same commit.
  • Keep one middleware list per environment in version control, and diff them.
  • Keep market-assets.strapi.io in the lists; the Strapi docs say it is safe to keep.
  • Read the browser console after every provider change.

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?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 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.