The browser blocks a script because of your Content Security Policy
Refused to load the script 'https://cdn.example.com/lib.js' because it violates the following Content Security Policy directive: "script-src 'self'".Short answer
The page's Content-Security-Policy does not allow what it tried to run or load. The console names the directive that blocked it. Fix it by allowing that exact source, giving each legitimate inline script a per-response nonce or a hash, or moving inline handlers into script files. Test changes in report-only mode, and do not add unsafe-inline.
Fix it fast
1Find out which cause you have
Read the directive named in the console message, then look at the policy the server actually sends. Compare the two: the blocked source must be missing from that directive, or from default-src when the directive is not listed.
curl -sI https://example.com/ | grep -i "content-security-policy" # the page may also carry a policy in a meta tag curl -s https://example.com/ | grep -i "http-equiv=.content-security-policy"2Match what you see, then jump to its fix
- 1
Refused to execute inline script, or Refused to execute inline event handler, and the directive is script-src or script-src-elem.Inline script or an inline event handler, with no nonce or hashFix - 2
Refused to load the script 'https://cdn.example.com/lib.js', and the directive in the message is script-src, script-src-elem or default-src.A script comes from a host the policy does not listFix - 3
The message names connect-src, img-src, style-src, frame-src or font-src, for example Refused to connect to an API URL.The blocked thing is not a script: another directive is the one that failsFix - 4
You added the source to the policy you control and the same Refused message still appears, or curl shows two Content-Security-Policy headers, or both a header and a meta tag exist.Two policies are sent, and the stricter one winsFix - 5
A new policy broke several pages at once and you cannot tell which sources are legitimate.You are enforcing a policy before you know what the site loadsFix
- 1
What does the error look like?
Refused to load the script 'https://cdn.example.com/lib.js' because it violates the following Content Security Policy directive: "script-src 'self'".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.Refused to connect to 'https://api.example.com/v1' because it violates the following Content Security Policy directive: "connect-src 'self'".Content-Security-Policy: The page's settings blocked the loading of a resource (script-src-elem) at inline because it violates the following directive.
Cause 1 of 5
Inline script or an inline event handler, with no nonce or hash
1Check
When a policy has script-src or default-src, inline code is blocked: script bodies, onclick-style attributes and javascript: URLs. The allowed ways are a nonce or a hash for inline script elements; event-handler attributes have to move into script files.
curl -s https://example.com/ | grep -oE "<script[^>]*>|on[a-z]+=\"" | sort | uniq -c | sort -rn | head2Fix
Generate a fresh random nonce for every response, send it in the header and put the same value on each trusted script. Once a directive contains a nonce or hash, browsers ignore unsafe-inline in it, so do not rely on it. For a script that never changes, a sha256 hash of its exact contents works without templating. Replace onclick attributes with addEventListener.
// Express 5
import crypto from 'node:crypto'
app.use((req, res, next) => {
res.locals.nonce = crypto.randomBytes(16).toString('base64')
res.setHeader(
'Content-Security-Policy',
`default-src 'self'; script-src 'self' 'nonce-${res.locals.nonce}'; object-src 'none'; base-uri 'self'`
)
next()
})
// template: <script nonce="<%= nonce %>"> ... </script>
// hash instead, for one static inline script
// echo -n 'console.log(1)' | openssl dgst -sha256 -binary | openssl base64
// script-src 'self' 'sha256-<that value>'3Verify
The inline script runs, the console shows no new Refused line, and two page loads carry different nonce values.
curl -sI https://example.com/ | grep -i content-security-policy
curl -sI https://example.com/ | grep -i content-security-policyConfirmed by developer.mozilla.org
Cause 2 of 5
A script comes from a host the policy does not list
1Check
Match the blocked URL's origin (scheme, host, port) against the directive. script-src-elem covers script elements and falls back to script-src, then to default-src. Only the directive that actually applies matters.
curl -sI https://example.com/ | grep -i "content-security-policy" | tr ';' '\n' | grep -iE "script-src|default-src"2Fix
Add that one origin to the directive that blocked it, not a wildcard for the whole internet. For a third-party file that does not change, also add an integrity attribute so a tampered copy is refused. If a loader script you trust pulls in many others, a nonce plus strict-dynamic lets it load them, but only use that if your own scripts do not create script elements from untrusted input.
# nginx
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://cdn.example.com; object-src 'none'; base-uri 'self'" always;
# page
# <script src="https://cdn.example.com/lib.js" integrity="sha384-..." crossorigin="anonymous"></script>3Verify
Reload nginx, hard-reload the page and confirm the script loads with no new console message.
sudo nginx -t && sudo systemctl reload nginx
curl -sI https://example.com/ | grep -i content-security-policyConfirmed by developer.mozilla.org
Cause 3 of 5
The blocked thing is not a script: another directive is the one that fails
1Check
Each fetch directive covers its own kind of request. fetch and XHR calls fall under connect-src, images under img-src, and so on; any directive you did not write falls back to default-src. Adding the host to script-src does nothing for these.
curl -sI https://example.com/ | grep -i "content-security-policy" | tr ';' '\n'2Fix
Add the origin to the exact directive named in the message, and nowhere else.
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-RANDOM'; connect-src 'self' https://api.example.com; img-src 'self' data: https://images.example.com3Verify
The request succeeds in the Network tab and the console is quiet.
curl -sI https://example.com/ | grep -i content-security-policyConfirmed by developer.mozilla.org
Cause 4 of 5
Two policies are sent, and the stricter one wins
1Check
Every policy that arrives is enforced. The allowed result is what all of them permit together, so a source added in one place is still blocked by another. A CDN, proxy, framework or plugin can add its own header.
curl -sI https://example.com/ | grep -ic "^content-security-policy:"
curl -s https://example.com/ | grep -ic "http-equiv=.content-security-policy"
grep -RnE "Content-Security-Policy" /etc/nginx/ 2>/dev/null2Fix
Pick one place to own the policy and remove the others, or edit each so they agree. A policy can only be tightened by adding another, never loosened.
# keep one add_header for CSP in nginx, remove duplicates from the app or the CDN rules
sudo nginx -t && sudo systemctl reload nginx3Verify
curl reports exactly one policy header, and the page loads with it.
curl -sI https://example.com/ | grep -ic '^content-security-policy:' # expect 1Confirmed by developer.mozilla.org
Cause 5 of 5
You are enforcing a policy before you know what the site loads
1Check
Run the same policy in report-only mode. The browser reports violations without blocking anything, and it can run beside an enforced policy. The recommended way to receive the reports is report-to with a Reporting-Endpoints header; report-uri is deprecated, though MDN suggests sending both until every browser supports report-to.
curl -sI https://example.com/ | grep -iE "content-security-policy-report-only|reporting-endpoints"2Fix
Send the candidate policy as Content-Security-Policy-Report-Only, collect reports for a few days of real traffic, add the legitimate sources, then switch the header name to Content-Security-Policy.
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports"
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'nonce-RANDOM'; report-to csp-endpoint3Verify
Reports arrive as JSON posts of type csp-violation, and after fixing the sources no new ones appear.
curl -sI https://example.com/ | grep -i content-security-policy-report-onlyConfirmed by developer.mozilla.org
How do I stop it coming back?
- Build the policy in report-only mode first and enforce it only when the reports are clean.
- Avoid inline handlers and inline scripts in new code so the policy can stay free of unsafe-inline.
- Keep one owner for the header (app, proxy or CDN) and review it when you add a third-party script.
- Add integrity attributes to third-party scripts that you load from a CDN.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Security
An HTTPS page is blocked from loading an insecure http:// resource
Mixed Content: The page at 'https://example.com/' was loaded over HTTPS, but attempted to connect to the insecure endpoint 'http://api.example.com/items'. This request has been blocked.
Your page came over HTTPS but something on it points at an http:// address, and the browser refuses the risky kinds (scripts, styles, frames, fetch calls).
4 causes, each with a fixSecurity
Inline base64 images do not show because of the Content Security Policy
Refused to load the image 'data:image/svg+xml;base64,...' because it violates the following Content Security Policy directive: "default-src 'self'".
A base64 image has a data: URL, and a policy such as default-src 'self' does not allow that scheme.
3 causes, each with a fixSecurity
Video from a MediaSource is blocked: 'Content at ... may not load data from blob:'
Content-Security-Policy: The page's settings blocked the loading of a resource (media-src) at blob:http://localhost:3000/...
A MediaSource video plays from a blob: URL.
3 causes, each with a fixSecurity
The browser refuses to load a font because of your Content Security Policy
Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.
A font is blocked when the font-src directive, or default-src when font-src is missing, does not allow where the font comes from.
3 causes, each with a fixSecurity
jsonwebtoken rejects a token: invalid signature, jwt expired or jwt malformed
JsonWebTokenError: invalid signature
Each message points at a different step.
5 causes, each with a fixSecurity
Keycloak blocks your single-page app with a CORS error
Request requires preflight, which is disallowed to follow cross-origin redirects.
Keycloak checks the Origin header of a browser call against the Web origins configured on that client and refuses it with 403 when it does not match.
2 causes, each with a fix