简体   繁体   English

拒绝加载脚本,因为它违反了以下内容安全策略指令

[英]Refused to load the script because it violates the following Content Security Policy directive

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:当我尝试将我的应用程序部署到 Android 系统高于 5.0.0 ( Lollipop ) 的设备时,我不断收到以下错误消息:

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: D/SystemWebChromeClient(9132): file:///android_asset/www/index.html: Line 0 : 拒绝加载脚本“http://xxxxx”,因为它违反了以下内容安全策略指令:“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'". 07-03 18:39:21.621: I/chromium(9132): [INFO:CONSOLE(0)]“拒绝加载脚本‘http://xxx’,因为它违反了以下内容安全策略指令:”脚本- 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:但是,如果我将其部署到 Android 系统为 4.4.x ( KitKat ) 的移动设备,则安全策略适用于默认策略:

<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?我该如何解决这个问题?

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:当我尝试将应用程序部署到5.0.0( Lollipop )以上的Android系统的设备上时,我不断收到以下错误消息:

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:D / SystemWebChromeClient(9132):file:///android_asset/www/index.html:第0行:拒绝加载脚本“ http:// xxxxx”,因为它违反了以下内容安全策略指令:“ 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'". 07-03 18:39:21.621:I / chromium(9132):[INFO:CONSOLE(0)]“拒绝加载脚本'http:// xxx',因为它违反了以下内容安全策略指令:” 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:但是,如果我将其部署到Android系统为4.4.x( KitKat )的移动设备上,则安全策略可与默认策略一起使用:

<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?我该如何解决这个问题?

The self answer given by MagngooSasa did the trick, but for anyone else trying to understand the answer, here are a few bit more details: MagngooSasa 给出自我回答解决了这个问题,但对于试图理解答案的其他人来说,这里有一些更多的细节:

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.使用 Visual Studio 开发Cordova应用程序时,我尝试导入远程 JavaScript 文件 [位于 http://Guess.What.com/MyScript.js],但出现标题中提到的错误。

Here is the meta tag before , in the index.html file of the project:这是项目的index.html文件中,之前meta标签:

<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!没有更多的错误!

它是通过以下方式解决的:

script-src 'self' http://xxxx 'unsafe-inline' 'unsafe-eval';

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/ .对于任何寻求完整解释的人,我建议您查看内容安全政策: 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" “来自https://mybank.com 的代码应该只能访问 https://mybank.com 的数据,而https://evil.example.com绝对不应该被允许访问。每个来源都与其他来源保持隔离网络”

XSS attacks are based on the browser's inability to distinguish your app's code from code downloaded from another website. XSS 攻击基于浏览器无法区分您的应用程序代码和从其他网站下载的代码。 So you must whitelist the content origins that you consider safe to download content from, using the Content-Security-Policy HTTP header.因此,您必须使用Content-Security-Policy HTTP 标头将您认为可以安全下载内容的内容来源列入白名单。

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.您的策略应该包含一个 default-src 策略指令,当其他资源类型没有自己的策略时,它是其他资源类型的后备。

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' .您是说您正在授权执行来自'self'http://onlineerp.solution.quebec'unsafe-inline''unsafe-eval'的 JavaScript 代码 ( script-src )。

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: 'unsafe-line''unsafe-eval'会造成安全问题,因此除非您对它们有非常特殊的需求,否则不应使用它们:

"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." “如果 eval 及其文本到 JavaScript 的兄弟对您的应用程序完全必要,您可以通过在 script-src 指令中添加'unsafe-eval'作为允许的源来启用它们。但是,再次,请不要。禁止执行字符串的能力使攻击者更难在您的网站上执行未经授权的代码。” (Mike West, Google) (迈克·韦斯特,谷歌)

Full permission string完整的权限字符串

The previous answers did not fix my issue, because they don't include blob: data: gap: keywords at the same time;以前的答案没有解决我的问题,因为它们没有同时包含blob: data: gap: Keywords; 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.确保防止用户在控制台中执行代码或在封闭环境中(如Cordova应用程序)。

我们使用了这个:

<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.修复错误。

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.添加元标记以忽略此策略对我们没有帮助,因为我们的网络服务器在响应中注入了Content-Security-Policy标头。

In our case we are using Ngnix as the web server for a Tomcat 9 Java-based application.在我们的例子中,我们使用 Ngnix 作为基于 Java 的Tomcat 9 应用程序的 Web 服务器。 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.从 Web 服务器,它指示浏览器不允许inline scripts ,因此为了临时测试,我们通过评论关闭了Content-Security-Policy

How to turn it off in ngnix如何在 ngnix 中关闭它

  • By default, ngnix ssl.conf file will have this adding a header to the response:默认情况下,ngnix ssl.conf文件将在响应中添加一个标头:

    #> 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.如果您只是注释此行并重新启动 ngnix,则不应将标头添加到响应中。

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:对于像我这样使用 Apache/Debian 服务器的傻瓜,他们试图将其添加到 index.html 文件中(因此损失了几个小时),答案可能是这样的:

Edit: /etc/apache2/sites-available/yourwebsiteconfig.com-ssl.conf编辑: /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>

if you are using helmet package then just pass contentSecurityPolicy: false, into helment functions option like this如果您使用的是头盔包,则只需将 contentSecurityPolicy: false 传递到头盔功能选项中,如下所示

app.use(
  helmet({
    contentSecurityPolicy: false,
  })
);

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.出现此错误的可能原因可能是因为您已将/build文件夹添加到 .gitignore 文件中,或者通常没有将其检入 Git。

So when you Git push Heroku master, the build folder you're referencing don't get pushed to Heroku.因此,当您通过 Git 推送Heroku master 时,您引用的构建文件夹不会被推送到 Heroku。 And that's why it shows this error.这就是它显示此错误的原因。

That's the reason it works properly locally, but not when you deployed to Heroku.这就是它在本地正常工作的原因,但在您部署到 Heroku 时则不然。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 拒绝加载脚本,因为它违反了以下内容安全策略指令 - Refused to load scripts because it violates the following Content Security Policy directive Chrome扩展程序“拒绝加载脚本,因为它违反了以下内容安全策略指令” - Chrome Extension “Refused to load the script because it violates the following Content Security Policy directive” Chrome 扩展“拒绝加载脚本,因为它违反了以下内容安全策略指令” - Chrome Extension “Refused to load the script because it violates the following Content Security Policy directive” 拒绝加载脚本,因为它违反了以下内容安全策略指令:“style-src 'self' 'unsafe-inline' - Refused to load the script because it violates the following Content Security Policy directive: "style-src 'self' 'unsafe-inline' 拒绝加载样式表,因为它违反了以下“内容安全策略”指令(即刻) - Refused to load the stylesheet because it violates the following Content Security Policy directive (nonce) Cordova 错误:拒绝执行内联脚本,因为它违反了以下内容安全策略指令 - Cordova error: Refused to execute inline script because it violates the following Content Security Policy directive 拒绝执行内联脚本,因为它违反了以下内容安全策略指令 - Refused to execute inline script because it violates the following Content Security Policy directive Firebase Chrome 扩展 - 拒绝执行内联脚本,因为它违反了以下内容安全策略指令 - Firebase Chrome Extension - Refused to execute inline script because it violates the following Content Security Policy directive 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“default-src 'self'” - Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'" Chrome 扩展拒绝加载,因为它违反了以下内容安全政策 - Chrome Extension Refused to Load because it violates the following Content Security Policy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM