内容安全策略在 Internet Explorer 11 中不起作用

Content Security Policy does not work in Internet Explorer 11

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

header 值如下所示,其中每个响应的 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。因此,为了修复 inline-script 违规,我在脚本标记中添加了相同的 nonce 值。
<script type="text/javascript" nonce="somerandomvalue-differnt-foreach-reasone">
这里重要的是 nonce 值需要与 header 中的 nonce 值相匹配。 some details here

我实现了中间件 & tag-helper,它分别将随机数添加到 header & 脚本标签中。我确保 nonce 值在页面呈现时匹配。

然后出于测试目的,我在页面上添加了脚本 没有 nonce

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

Google chrome 检测到此违规并按预期阻止上述脚本。然而,在 IE 11 中,上面的脚本在没有任何违规的情况下被执行。同样,我确保 IE 中的 header 是 X-Content-Security-Policy

为什么 IE 11 不阻止脚本?

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

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

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

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

所以我认为对于 IE11,没有办法告诉它只禁止内联脚本。您只能告诉 IE11 要么允许所有脚本,要么允许 none.


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

http://caniuse.com/#feat=contentsecuritypolicy has details on browser support for CSP1 directives:

Partial support in Internet Explorer 10-11 refers to the browser only supporting the 'sandbox' directive by using the X-Content-Security-Policy header.

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

Support for nonce and other CSP2 features was added in Edge 15. So Edge 14 and earlier have no support for nonce or other new-in-CSP2 features. But Edge12+ has full support for all of CSP1.