简体   繁体   English

拒绝加载脚本,因为它违反了以下内容安全策略指令:“style-src 'self' 'unsafe-inline'

[英]Refused to load the script because it violates the following Content Security Policy directive: "style-src 'self' 'unsafe-inline'

I am using MVC6 (asp.net 5) using angular and trying to load scripts from CDN locations when my code is running in release mode, but for some reason the scripts NEVER load.当我的代码在发布模式下运行时,我正在使用 MVC6 (asp.net 5) 并尝试从 CDN 位置加载脚本,但由于某种原因,脚本永远不会加载。

I have read that you need to add a meta tag to your HTML file, which I have done, like so.我读到您需要在 HTML 文件中添加一个元标记,我已经这样做了,就像这样。

<meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com; style-src 'self' https://ajax.aspnetcdn.com; font-src 'self' http://netdna.bootstrapcdn.com" />

And on my Index.cshtml, I have got this.在我的 Index.cshtml 上,我有这个。

<environment names="Staging,Production">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"
        asp-fallback-src="~/lib/angular/angular.min.js"
        asp-fallback-test="window.angular">
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.min.js"
        asp-fallback-src="~/lib/angular-ui-router/release/angular-ui-router.js"
        asp-fallback-test="window.angular && window.angularUiRouter">
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-local-storage/0.2.2/angular-local-storage.min.js"
        asp-fallback-src="~/lib/angular-local-storage/dist/angular-local-storage.js"
        asp-fallback-test="window.angular && window.localStorage">
    </script>

But they never load.但他们从不加载。 I have tried running the code using IISExpress and also using the DNX Web command.我已经尝试使用 IISExpress 和DNX Web命令运行代码。

I have this post which is how I come to creating the META tag, but not sure why it's not working.我有这篇文章,这就是我创建 META 标签的方式,但不确定为什么它不起作用。 I have tried this in Chrome, and under the console, I just get errors like so我在 Chrome 中试过这个,在控制台下,我只是得到这样的错误

在此处输入图像描述

Put the following in the web page header section:将以下内容放在网页标题部分:

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

More details about Content Security Policy you can read here and here .您可以在此处此处阅读有关内容安全策略的更多详细信息。

In my case, this policy is set via SecurityHeadersAttribute (this attribute is set in AccountController and some others).就我而言,此策略是通过SecurityHeadersAttribute设置的(此属性在 AccountController 和其他一些属性中设置)。

Basically, this adds default policy in the headers that overwrite your meta tag.基本上,这会在覆盖meta标记的headers中添加默认策略。 So you need to change this policy or remove the attribute from Controller.因此,您需要更改此策略或从 Controller 中删除该属性。

If you want to workaround the issue, add the following in your application.conf file.如果您想解决此问题,请在您的application.conf文件中添加以下内容。

play.filters.disabled += "play.filters.headers.SecurityHeadersFilter"

Adding on the Accepted answer , I included the script-src-elem which was making my scripts not to load.添加已接受的答案,我包括了 script-src-elem ,它使我的脚本无法加载。

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

Manikandan C Why bother with a CDN? Manikandan C 为什么要使用 CDN? Do you really need it?你真的需要它吗? What type of app/site is it?它是什么类型的应用程序/网站? Are you dealing with GBs/TBs of Data where you cannot store the files locally?您是否正在处理无法在本地存储文件的 GB/TB 数据? Are these static files heavy loading?这些静态文件负载重吗? I already noticed in your markup that you have a back up source if the CDN cannot be hit.我已经在您的标记中注意到,如果无法访问 CDN,您有一个备份源。

asp-fallback-src="~/lib/angular/angular.min.js" asp-fallback-src="~/lib/angular/angular.min.js"

So if your project is small, a local site or not load heavy, then, in my opinion, you really don't need a CDN.因此,如果您的项目很小,是本地站点或负载不重,那么在我看来,您真的不需要 CDN。 I think it'll just cause more problems for you like its doing now.As a solution, I would remove the meta tags.我认为它会像现在这样给您带来更多问题。作为解决方案,我会删除元标记。

I've worked on and continue to maintain multiple MVC-MVC5 applications and a few have CDNs in them but mostly to make sure if our local files aren't available for some reason, the CDN gets hit, although you do need to be diligent about the CDN getting compromised, which is another reason a CDN is not 100% the way to go.我已经致力于并继续维护多个 MVC-MVC5 应用程序,其中一些应用程序中有 CDN,但主要是为了确保如果我们的本地文件由于某种原因不可用,CDN 会受到攻击,尽管你确实需要勤奋关于 CDN 受到威胁,这是 CDN 不是 100% 可行的另一个原因。 We never have any console errors like the ones that you posted and we also never put META tags in our Views.我们从来没有像您发布的那样出现任何控制台错误,我们也从来没有在我们的视图中放置 META 标签。 Dont believe everything you read.不要相信你读到的一切。 If you have nothing but static files, then a CDN makes sense.如果您只有静态文件,那么 CDN 就很有意义。 Important rule to remember is, if you have inline code in your html or any dynamic portions of code then the CDN gets called multiple times, therefore using it as a major resource doesnt make sense.要记住的重要规则是,如果您的 html 中有内联代码或代码的任何动态部分,那么 CDN 会被多次调用,因此将其用作主要资源没有意义。

暂无
暂无

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

相关问题 因为它违反了以下内容安全策略指令:“style-src&#39;self&#39;” - because it violates the following Content Security Policy directive: “style-src 'self'” 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“default-src 'self'” - Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'" Javascript抛出:拒绝执行内联事件处理程序,因为它违反了以下内容安全策略指令:“script-src'self' - Javascript throwing : Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' 内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self'” - Inline script because it violates the following Content Security Policy directive: “script-src 'self'” 拒绝加载脚本,因为它违反了以下内容安全策略指令 - Refused to load the script because it violates the following Content Security Policy directive 拒绝应用内联样式,因为它违反了以下内容安全策略指令 - Refused to apply inline style because it violates the following Content Security Policy directive jQuery:拒绝应用内联样式,因为它违反了以下内容安全策略指令 - jQuery: Refused to apply inline style because it violates the following Content Security Policy directive 拒绝加载脚本,因为它违反了以下内容安全策略指令 - Refused to load scripts 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM