jquery 内容安全策略:页面的设置阻止加载资源

gijlo24d  于 2023-01-30  发布在  jQuery
关注(0)|答案(6)|浏览(608)

我在页面加载时使用CAPTCHA,但是由于一些安全原因,它被阻塞了。
我正面临这样的问题:

Content Security Policy: The page's settings blocked the loading
    of a resource at
    http://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit
    ("script-src http://test.com:8080 'unsafe-inline' 'unsafe-eval'").

我使用了以下JavaScript和 meta标签:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<script src="http://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
ilmyapht

ilmyapht1#

你说你只能从你自己的站点(self)加载脚本,然后你试图从另一个站点(www.google.com)加载脚本,因为你限制了它,所以你不能加载,这就是Content Security Policy(CSP)的全部意义。
您可以将第一行更改为:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' http://www.google.com">

或者,在您了解更多关于CSP的信息之前,完全删除这一行是值得的。您当前的CSP无论如何都是相当宽松的(允许unsafe-inlineunsafe-eval*default-src),所以老实说,它可能没有增加太多的价值。

b5buobof

b5buobof2#

当我的**ASP.NET Core Angular**项目在Visual Studio 2019中运行时,有时我会在Firefox控制台中收到以下错误消息:
内容安全策略:页面的设置阻止了在inline("default-src")加载资源。
在Chrome中,错误消息改为:
加载资源失败:服务器以状态404()响应
在我的情况下,它与我的内容安全策略无关,而只是我的TypeScript错误的结果。
检查IDE输出窗口中是否存在TypeScript错误,例如:

> ERROR in src/app/shared/models/person.model.ts(8,20): error TS2304: Cannot find name 'bool'.
>
> i 「wdm」: Failed to compile.

注意:由于此问题是Google上此错误消息的第一个结果。

noj0wjuj

noj0wjuj3#

我也遇到过类似的错误类型,首先,我尝试在代码中添加 meta标签,但是没有成功。
我发现在nginx Web服务器上,您可能有一个安全设置,可能会阻止外部代码运行:

# Security directives
server_tokens off;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'  https://ajax.googleapis.com  https://ssl.google-analytics.com https://assets.zendesk.com https://connect.facebook.net; img-src 'self' https://ssl.google-analytics.com https://s-static.ak.facebook.com https://assets.zendesk.com; style-src 'self' 'unsafe-inline' https://assets.zendesk.com; font-src 'self' https://fonts.gstatic.com  https://themes.googleusercontent.com; frame-src https://player.vimeo.com https://assets.zendesk.com https://www.facebook.com https://s-static.ak.facebook.com https://tautt.zendesk.com; object-src 'none'";

请检查Content-Security-Policy。您可能需要添加源引用。

gwo2fgha

gwo2fgha4#

我设法允许我所有必要的网站与此标题:

header("Content-Security-Policy: default-src *; style-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' stackexchange.com");
x6492ojm

x6492ojm5#

您可以通过在htaccess中添加代码来修复

<IfModule mod_headers.c>
    # Feature-Policy
    Header set Feature-Policy "microphone 'none'"
    # Referrer-Policy
    Header set Referrer-Policy "same-origin"
    # Content-Security-Policy   
    Header set Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval' e.g. https://ajax.googleapis.com https://ssif1.globalsign.com https://malsup.github.io https://seal.globalsign.com https://www.googletagmanager.com https://www.google.com https://www.gstatic.com https://assets.zendesk.com https://chimpstatic.com https://cdn.ywxi.net https://static.hotjar.com https://maxcdn.bootstrapcdn.com https://www.google-analytics.com https://static.zdassets.com https://connect.facebook.net https://script.hotjar.com https://*.livechatinc.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://cdnjs.cloudflare.com https://ajax.googleapis.com;"
    # X-XSS-Protection
    Header set X-XSS-Protection "1; mode=block"
</IfModule>
ee7vknir

ee7vknir6#

!!仅用于调试!!

如果真的有必要,请暂时这样做,因为这会使您的浏览器在所有网站上都容易受到攻击!
您可以在浏览器中禁用它们。
火狐
在Firefox地址栏中输入about:config,找到security.csp.enable并将其设置为false
chrome 合金
您可以安装名为Disable Content-Security-Policy的扩展来禁用CSP。

相关问题