Microsoft Edge 不接受 Content-Security 策略的哈希

Microsoft Edge not accepting hashes for Content-Security Policy

问题

Content-Security-Policy 应该默认将脚本和样式解析列入黑名单,并允许它基于各种指令来验证预期输出的哈希值。浏览器必须无法实现任何未预先提供匹配哈希的 Javascript 或 CSS。具有匹配散列的代码应正常执行。 Microsoft Edge 拒绝所有 JS/CSS in-page 块。

演示原版源码

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; style-src 'sha256-JtUhvM7uQO2KX5IEGWxN+rhEyzzsyFelfO2gXvYEuWA='; script-src https://ajax.googleapis.com 'sha256-iZzrsbzuGxfOaTdnB/E6RQBssyXQRp7W8YtZD2Wg/Rc=';" />
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; style-src 'sha256-JtUhvM7uQO2KX5IEGWxN+rhEyzzsyFelfO2gXvYEuWA='; script-src https://ajax.googleapis.com 'sha256-iZzrsbzuGxfOaTdnB/E6RQBssyXQRp7W8YtZD2Wg/Rc=';" />
<style>#loading{color:transparent}#loading:after{color:green;content:"Style loaded."}</style>
</head>
<body>
<span id="loading">Hashes loading...</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>alert("Script loaded.")</script>

CSP14304: Unknown source ‘'sha256-JtUhvM7uQO2KX5IEGWxN+rhEyzzsyFelfO2gXvYEuWA='’ for directive ‘style-src’ in - source will be ignored.

CSP14306: No sources given for directive ‘style-src’ for - this is equivalent to using ‘none’ and will prevent the downloading of all resources of this type.

CSP14304: Unknown source ‘'sha256-iZzrsbzuGxfOaTdnB/E6RQBssyXQRp7W8YtZD2Wg/Rc='’ for directive ‘script-src’ in - source will be ignored.

CSP14312: Resource violated directive ‘style-src 'sha256-JtUhvM7uQO2KX5IEGWxN+rhEyzzsyFelfO2gXvYEuWA='’ in : inline style. Resource will be blocked.

CSP14312: Resource violated directive ‘script-src LINK-REMOVED-INSUFFICIENT-REPUTATION-ON-Whosebug-SHOULD-BE-THE-GOOGLE-API-URL 'sha256-iZzrsbzuGxfOaTdnB/E6RQBssyXQRp7W8YtZD2Wg/Rc='’ in : inline script. Resource will be blocked.

已尝试修复

我想不出还有什么可以尝试的。

24 小时后更新: 为完整性添加了 X-Content-Security-Policy & JSBin URL 已更新,尽管它对这个特定的没有影响情况。

http://caniuse.com/#feat=contentsecuritypolicy

http://caniuse.com/#feat=contentsecuritypolicy2

IE Edge 不支持内容安全策略级别 2,hash-source 属于级别 2。

编辑:这可能不正确。见上面的评论。

IE 11 不支持Content-Security-Policy(只支持X-Content-Security-Policy),打开失败。 IE 12 支持 CSP,但不 grok nonces/hashes,它无法关闭...除非您还在 Content-Security-Policy header 中提供 'unsafe-inline'

CSP 级别 2 说 "if a hash or nonce is supplied, ignore 'unsafe-inline'." 这是为了向后兼容,因为旧的浏览器会理解 'unsafe-inline' 而不是 nonces/hashes。参见 http://www.w3.org/TR/CSP2/#directive-script-src