admin管理员组文章数量:1129645
When I tried to deploy my app onto devices with Android system above 5.0.0 (Lollipop), I kept getting these kind of error messages:
07-03 18:39:21.621: D/SystemWebChromeClient(9132): file:///android_asset/www/index.html: Line 0 : Refused to load the script 'http://xxxxx' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval' 'unsafe-inline'". 07-03 18:39:21.621: I/chromium(9132): [INFO:CONSOLE(0)] "Refused to load the script 'http://xxx' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval' 'unsafe-inline'".
However, if I deployed it to mobile device with Android system of 4.4.x (KitKat), the security policy works with the default ones:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
Then I thought, maybe, I should change to something like this:
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-eval' 'unsafe-inline'; object-src 'self'; style-src 'self' 'unsafe-inline'; media-src *">
Basically, both options don't work for for me. How can I solve this issue?
When I tried to deploy my app onto devices with Android system above 5.0.0 (Lollipop), I kept getting these kind of error messages:
07-03 18:39:21.621: D/SystemWebChromeClient(9132): file:///android_asset/www/index.html: Line 0 : Refused to load the script 'http://xxxxx' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval' 'unsafe-inline'". 07-03 18:39:21.621: I/chromium(9132): [INFO:CONSOLE(0)] "Refused to load the script 'http://xxx' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval' 'unsafe-inline'".
However, if I deployed it to mobile device with Android system of 4.4.x (KitKat), the security policy works with the default ones:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
Then I thought, maybe, I should change to something like this:
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-eval' 'unsafe-inline'; object-src 'self'; style-src 'self' 'unsafe-inline'; media-src *">
Basically, both options don't work for for me. How can I solve this issue?
Share Improve this question edited Feb 19, 2023 at 13:23 Liam 29.5k28 gold badges137 silver badges200 bronze badges asked Jul 3, 2015 at 16:51 MangooSaSaMangooSaSa 3,7153 gold badges17 silver badges17 bronze badges 5- 1 Very similar to my issue. I am unable to retrieve a JSON file, "because it violates the following Content Security Policy directive: "connect-src 'self'"" – Michael R Commented Jan 18, 2017 at 22:27
- 2 @MichaelR If You want to retrieve some JSON information from API through JS like tampermonkey addon or everything else You can use this plugin chrome.google.com/webstore/detail/disable-content-security/… and disable CSP check while You want obtain something. It is altough not safe but in some cases it might work. I am posting this answer here because I was looking for my error and this topic shows first in Google. – Eryk Wróbel Commented Nov 7, 2018 at 7:26
- Does this answer your question? How does Content Security Policy (CSP) work? – Liam Commented Oct 27, 2022 at 11:48
- @ErykWróbel The extension you suggested basically removes the CSP headers, In this case, CSP is implemented with Meta tags so any browser extension that removed CSP headers won't work. This requires an additional effort of parsing the page and removing the Meta tags. Any extension that injects a script and remove the Meta tags should be useful. – Sachin Jain Commented Nov 16, 2022 at 7:56
- It is now 2025 and Humana insurance portal password reset script somehow managed to fail checking they followed anti XSS vulnerabilities. Does anyone working nowadays know about OWASP even?! – Elysiumplain Commented Dec 30, 2024 at 21:02
12 Answers
Reset to default 70The self answer given by MagngooSasa did the trick, but for anyone else trying to understand the answer, here are a few bit more details:
When developing Cordova apps with Visual Studio, I tried to import a remote JavaScript file [located here http://Guess.What.com/MyScript.js], but I have the error mentioned in the title.
Here is the meta tag before, in the index.html file of the project:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
Here is the corrected meta tag, to allow importing a remote script:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *;**script-src 'self' http://onlineerp.solution.quebec 'unsafe-inline' 'unsafe-eval';** ">
And no more error!
Full permission string
The previous answers did not fix my issue, because they don't include blob: data: gap:
keywords at the same time; so here is a string that does:
<meta http-equiv="Content-Security-Policy" content="default-src * self blob: data: gap:; style-src * self 'unsafe-inline' blob: data: gap:; script-src * 'self' 'unsafe-eval' 'unsafe-inline' blob: data: gap:; object-src * 'self' blob: data: gap:; img-src * self 'unsafe-inline' blob: data: gap:; connect-src self * 'unsafe-inline' blob: data: gap:; frame-src * self blob: data: gap:;">
Warning: This exposes the document to many exploits. Be sure to prevent users from executing code in the console or to be in a closed environment like a Cordova application.
For anyone looking for a complete explanation, I recommend you to take a look at Content Security Policy: https://www.html5rocks.com/en/tutorials/security/content-security-policy/.
"Code from https://mybank.com should only have access to https://mybank.com’s data, and https://evil.example.com should certainly never be allowed access. Each origin is kept isolated from the rest of the web"
XSS attacks are based on the browser's inability to distinguish your app's code from code downloaded from another website. So you must whitelist the content origins that you consider safe to download content from, using the Content-Security-Policy
HTTP header.
This policy is described using a series of policy directives, each of which describes the policy for a certain resource type or policy area. Your policy should include a default-src policy directive, which is a fallback for other resource types when they don't have policies of their own.
So, if you modify your tag to:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *;**script-src 'self' http://onlineerp.solution.quebec 'unsafe-inline' 'unsafe-eval';** ">
You are saying that you are authorizing the execution of JavaScript code (script-src
)
from the origins 'self'
, http://onlineerp.solution.quebec
, 'unsafe-inline'
, 'unsafe-eval'
.
I guess that the first two are perfectly valid for your use case, I am a bit unsure about the other ones. 'unsafe-line'
and 'unsafe-eval'
pose a security problem, so you should not be using them unless you have a very specific need for them:
"If eval and its text-to-JavaScript brethren are completely essential to your application, you can enable them by adding 'unsafe-eval' as an allowed source in a script-src directive. But, again, please don’t. Banning the ability to execute strings makes it much more difficult for an attacker to execute unauthorized code on your site." (Mike West, Google)
It was solved with:
script-src 'self' http://xxxx 'unsafe-inline' 'unsafe-eval';
if you are using helmet package then just pass contentSecurityPolicy: false, into helment functions option like this
app.use(
helmet({
contentSecurityPolicy: false,
})
);
We used this:
<meta http-equiv="Content-Security-Policy" content="default-src gap://ready file://* *; style-src 'self' http://* https://* 'unsafe-inline'; script-src 'self' http://* https://* 'unsafe-inline' 'unsafe-eval'">
To elaborate some more on this, adding
script-src 'self' http://somedomain 'unsafe-inline' 'unsafe-eval';
to the meta tag like so,
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; script-src 'self' https://somedomain.com/ 'unsafe-inline' 'unsafe-eval'; media-src *">
fixes the error.
The following helped me to get rid of CSP errors, when using Stripe in my NodeJS app:
app.use(helmet());
const helmet = require("helmet");
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
fontSrc: ["'self'"],
imgSrc: ["'self'"],
scriptSrc: ["'self'"],
styleSrc: ["'self'"],
frameSrc: ["'self'"],
},
reportOnly: true, // Set to 'true' to enable report-only mode
})
);
You may find very helpful tutorial here or read more about it here
For me, error was
because it violates the following Content Security Policy directive: "connect-src 'self' wss: https://www.google-analytics.com".
and my CSP configured was:
"connect-src 'self' ws: wss: https://www.google-analytics.com",
And this worked for me as Google maps api should be in connect-src
CSP value as well so browser knows about it:
"connect-src 'self' ws: wss: https://www.google-analytics.com https://maps.googleapis.com https://www.googletagmanager.com",
Adding the meta tag to ignore this policy was not helping us, because our webserver was injecting the Content-Security-Policy
header in the response.
In our case we are using Ngnix as the web server for a Tomcat 9 Java-based application. From the web server, it is directing the browser not to allow inline scripts
, so for a temporary testing we have turned off Content-Security-Policy
by commenting.
How to turn it off in ngnix
By default, ngnix ssl.conf file will have this adding a header to the response:
#> grep 'Content-Security' -ir /etc/nginx/global/ssl.conf add_header Content-Security-Policy "default-src 'none'; frame-ancestors 'none'; script-src 'self'; img-src 'self'; style-src 'self'; base-uri 'self'; form-action 'self';";
If you just comment this line and restart ngnix, it should not be adding the header to the response.
If you are concerned about security or in production please do not follow this, use these steps as only for testing purpose and moving on.
For dummies like me with Apache/Debian server, who tried to add this into the index.html file(and lost couple of hours because of this), the answer would be sometnig like this:
Edit: /etc/apache2/sites-available/yourwebsiteconfig.com-ssl.conf
add or modify the followng line:
Header always set Content-Security-Policy: "script-src 'self' 'unsafe-inline' 'unsafe-eval' data: https://www.googletagmanager.com"
here:
<IfModule mod_headers.c>
Header always append X-Frame-Options SAMEORIGIN
Header always set Content-Security-Policy: "script-src 'self' 'unsafe-inline' 'unsafe-eval' data: https://www.googletagmanager.com"
</IfModule>
The probable reason why you get this error is likely because you've added the /build folder to your .gitignore file or generally haven't checked it into Git.
So when you Git push Heroku master, the build folder you're referencing don't get pushed to Heroku. And that's why it shows this error.
That's the reason it works properly locally, but not when you deployed to Heroku.
本文标签:
版权声明:本文标题:javascript - Refused to load the script because it violates the following Content Security Policy directive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736746737a1950818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论