Guides

Why CSP blocks my own scripts — three causes and how to fix each

Illustration: a guard turning away a courier from his own warehouse

The symptom is recognizable: you added a security header, and buttons stopped working, blocks disappeared, maybe the page is white altogether. The console is red:

Refused to execute inline script because it violates the following
Content Security Policy directive: "script-src 'self'"

The key word in that error is inline. Not "malicious," not "foreign." CSP only looks at where code came from, and code written straight into the HTML has no source. Let's go through the causes by frequency — it's almost always the first one.

Cause 1. The script is written directly in the HTML

The most common one, especially if an AI helped write the markup: it happily puts handlers into attributes and small scripts right into the page.

How to check. The error contains the word inline, and the line number points at the HTML document, not at a .js file. Look for two things in your markup: script tags without a src attribute, and handler attributes like onclick=, onload=, onsubmit=.

How to fix, in descending order of reliability:

  1. Move the code into a separate file and include it via src. Now it has an address, and 'self' lets it through. This fixes most cases with no policy changes.
  2. Convert attribute handlers to addEventListener in that same file.
  3. If extraction is impossible, have the server issue a one-time number (nonce) and put it in the header and in each of your scripts. Note: the number must be new on every response, so this path doesn't work on purely static hosting.

The same applies to styles: style-src blocks style tags and style="..." attributes by exactly the same logic.

Cause 2. The directive in the error isn't the one you assume

Second most common: your scripts are fine, but fonts don't load, images are missing, or API calls fail.

How to check. Read which directive the error names. There are several, each responsible for its own thing:

  • script-src-elem — loading script files;
  • style-src — styles, including from other domains;
  • font-src — fonts (a frequent surprise: Google fonts live on a separate domain);
  • img-src — images, including avatars from an external service;
  • connect-src — fetch calls, WebSockets, analytics beacons.

The main default-src rule bites here: if you only set that one, it becomes the fallback for every resource type at once. Allowing default-src 'self' also forbade third-party fonts and calls to an external API.

How to fix. Add domains surgically, one directive at a time, guided by the console. Don't dump everything into default-src, and definitely don't write default-src * — that puts the page back to "run whatever."

By the way, if an API call fails but the error says blocked by CORS policy rather than mentioning CSP, that's a different story, fixed on the server: what is CORS.

Cause 3. You added 'unsafe-inline' and it does nothing

The most frustrating one. You gave up, added 'unsafe-inline' to the policy — and nothing changed.

Why. If the same directive also contains a 'nonce-...' or a hash, the browser deliberately ignores 'unsafe-inline'. That's not a bug: 'unsafe-inline' is meant there as a fallback for old browsers that don't understand nonces. A modern browser sees the nonce and demands one from every inline script.

How to check. Look at the full header in devtools: Network → the document request → Response Headers. If script-src has a nonce (or sha256-...) sitting next to 'unsafe-inline', that's your answer.

How to fix. Pick one scheme. Either nonces/hashes — and then you tag every inline script of yours. Or files plus 'self' — and then there's no inline code at all. Mixing them is pointless.

Two more traps if none of the causes fit

There are two CSP headers. You set one in the app, and your host, proxy or CDN added another. The browser enforces each policy separately and a resource must satisfy both — in practice the strictest wins. Check Response Headers: if Content-Security-Policy appears twice, fix the one coming from above.

You put the policy in a meta tag and expect report mode. Report-Only doesn't work through meta at all — only as a real response header. And a policy from meta starts applying at the point in the document where it appears, so everything loaded above it is out of scope. The header wins on both counts.

What to do if you have no time to dig

  1. Switch the header to Content-Security-Policy-Report-Only — the site comes back to life instantly, while violations keep printing in the console.
  2. Walk every page and collect the violations: which directive, which domain.
  3. Move inline scripts into files — that removes most of the list.
  4. Add the remaining domains surgically.
  5. Switch back to the enforcing header and re-check.

How the directives work and the three ways to allow your own code are covered in the basics piece what is CSP; where to physically put the header on Vercel, Netlify and nginx is in the security headers guide.

Why does it work locally but not in production?

Usually because the header is set by the host or a proxy that doesn't exist on your machine. Check production with curl -sI and compare it with local — the difference shows up immediately.

Can I just remove CSP and move on?

You can, and the site will work. But you're removing the second layer of protection exactly where it helps most — a public page with third-party widgets. Keeping Report-Only and tightening gradually is the saner move.

A library fails with an eval error — is that CSP too?

Yes. As soon as the policy contains default-src or script-src, eval() and friends are disabled automatically. The right move is to update or replace the library; weakening the policy for it is a last resort. For approaching an error you didn't write, see how to debug with AI.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →