Refused to execute inline script because it violates the Content Security Policy (proofforge.live): 6 causes and the fix that does not turn CSP off
You added a Content-Security-Policy header, or your host or a library did, and now the console says "Refused to execute inline script because it violates the following Content Security Policy directive". Your app breaks,
You added a Content-Security-Policy header, or your host or a library did, and now the console says "Refused to execute inline script because it violates the following Content Security Policy directive". Your app breaks, and the quick fix everyone suggests is to paste 'unsafe-inline'. I'm part of the ProofForge team. Here is how to fix the error without turning the policy off.
What the error means
A CSP tells the browser which script, style, image and connection sources are allowed. The error appears when your page contains something the policy does not allow, most often an inline <script> or <style>, an onclick="..." attribute, eval(), or a third-party domain you did not list. The message names the directive that blocked it, for example script-src or style-src. Read that name first.
6 causes, and the safe fix for each
-
Inline script from your framework. Next.js and similar frameworks inject small inline scripts. The safe fix is a per-request nonce: generate a random value, put it in the header (
script-src 'nonce-abc123') and on each inline script tag. Your framework docs show how to pass the nonce through. -
Inline event handlers like
onclick="...". Move them into your JavaScript withaddEventListener. Nonces do not cover attributes. -
A third-party script or widget domain not listed. Add the exact host to
script-src(andconnect-srcorframe-srcif the error names them). Add only what you use. -
evalornew Functionin a dependency. Some libraries need'unsafe-eval'. Look for a build or version that does not, before you allow it. -
Inline styles. For
style-src, use a nonce or a hash, or move styles into a stylesheet. - A policy set in two places. A meta tag, your host's headers and a middleware can each add a policy, and the browser applies all of them. Check the response headers and the HTML.
The fixes to skip
-
Do not add
'unsafe-inline'toscript-src. It switches off the main protection a CSP gives you against injected scripts. -
Do not use
*orhttps:as a script source. That allows any site. - Do not delete the header to make the error go away. You lose the protection and the next scan will flag it.
- If you must ship before the real fix, use
Content-Security-Policy-Report-Onlyfirst. It logs violations without breaking the page, so you can tighten the policy safely.
How to test the fix
Open DevTools, then Console and Network. Reload and confirm there are no new violation messages. Check the response header on the live domain, not only on localhost.
Quick check
Run the free ProofForge scan on your live URL. It reads your public site, passively and read-only, for missing or weak security headers (including CSP), exposed keys and open data, and says pass, fail or inconclusive. It checks what a stranger can see, so a clean result is not a full audit.
Check your own app from the outside, free. Run the free ProofForge scan on your live URL. No account needed for the first scan. It looks at your public site the way a stranger would and says pass, fail or inconclusive, so you know what is confirmed and what it could not see. One-time reports start at $9: see pricing. Building with an AI agent? There is an MCP server so it can check before it deploys.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.