【问题标题】:Content Security Policy does not work in Internet Explorer 11内容安全策略在 Internet Explorer 11 中不起作用
【发布时间】:2017-08-13 17:04:12
【问题描述】:

在我的每个响应的 asp.net 核心应用程序中,我正在添加内容安全策略标头。我知道对于 IE,标题名称是 X-Content-Security-Policy 而对于其他浏览器,如 chrome,它是 Content-Security-Policy

标头值如下所示,其中nonce 对于每个响应都不同。

default-src 'none';   
script-src 'self' 'nonce-somerandomvalue-differnt-foreach-reasone' 'unsafe-eval';  
style-src 'self' 'unsafe-inline';   
img-src 'self' data:;   
font-src 'self';    
object-src 'self';   
connect-src 'self';   
report-uri /csp/report;   

应用程序在几个页面上使用内联 javascript。因此,为了修复内联脚本违规,我在脚本标签中添加了相同的 nonce 值。
<script type="text/javascript" nonce="somerandomvalue-differnt-foreach-reasone">
这里重要的是 nonce 值需要与 header 中的 nonce 值匹配。 some details here

我实现了中间件和标签助手,它们分别将随机数添加到标题和脚本标签中。而且我确保两个 nonce 值在页面呈现时都匹配。

然后只是为了在页面上进行测试,我添加了脚本没有随机数

<script type="text/javascript">
    $(function () {
        alert('i am hacker');
    })
</script>

谷歌浏览器检测到这种违规行为并按预期阻止上述脚本。但是在 IE 11 中,上面的脚本执行时没有任何违规。同样,我确保 IE 中的标题是 X-Content-Security-Policy

为什么 IE 11 不阻塞脚本?

【问题讨论】:

    标签: internet-explorer asp.net-core internet-explorer-11 content-security-policy coreclr


    【解决方案1】:

    IE 11 根本不支持使用 nonce 属性和 nonce- 源值。

    The only CSP directive IE11 supports is the sandbox directive。它忽略所有其他 CSP 指令。

    因此,您可以完全从您的 X-Content-Security-Policy 标头中删除 'nonce-somerandomvalue-differnt-foreach-reasone' 部分,IE11 仍将允许内联脚本。

    无论您做什么,IE11 都将允许内联脚本,除非您让服务器发送带有X-Content-Security-Policy: sandbox 标头的响应,在这种情况下,它将禁止所有 脚本。唯一放松的方法是发送X-Content-Security-Policy: sandbox allow-scripts,但这将允许所有脚本,包括内联脚本。

    所以我认为对于 IE11,没有办法告诉它只允许内联脚本。你只能告诉 IE11 要么允许所有脚本,要么不允许。


    另请注意:IE11 于 2013 年发布,早于在任何地方指定 nonce 属性。我认为第一个指定 nonce 属性的 CSP 草案规范是在 2014 年的某个时候。

    http://caniuse.com/#feat=contentsecuritypolicy 详细介绍了浏览器对CSP1 directives 的支持:

    Internet Explorer 10-11 中的部分支持是指浏览器仅支持使用 X-Content-Security-Policy 标头的“沙盒”指令。

    nonce 属性是a CSP2 feature。见http://caniuse.com/#feat=contentsecuritypolicy2

    Support for nonce and other CSP2 features was added in Edge 15。因此,Edge 14 及更早版本不支持 nonce 或其他 CSP2 中的新功能。但是 Edge12+ 完全支持all of CSP1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 2019-09-20
      • 2019-09-22
      • 2018-04-22
      • 2021-12-25
      • 2016-10-20
      • 2019-08-12
      相关资源
      最近更新 更多