Microsoft Edge不接受内容安全策略的哈希

时间:2015-07-30 09:30:30

标签: javascript css security content-security-policy microsoft-edge

问题

Content-Security-Policy应该默认将脚本和样式解析列入黑名单,并根据各种指令允许它,其中一个指令被验证为预期输出的散列。浏览器必须无法实现任何未提前匹配哈希的Javascript或CSS。具有匹配哈希的代码应该正常执行。 Microsoft Edge拒绝所有JS / CSS页内阻止。

演示原始源代码

<!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>
  • 预期行为:正文应更改为“已加载样式。”,警告框应显示“已加载脚本”。外部Javascript不应抛出错误。控制台显示没有问题。
  • 实际行为:正在停留在“哈希加载......”上。哈希拒绝,外部Javascript接受。控制台显示错误:

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-STACKOVERFLOW-SHOULD-BE-THE-GOOGLE-API-URL 'sha256-iZzrsbzuGxfOaTdnB/E6RQBssyXQRp7W8YtZD2Wg/Rc='’ in : inline script. Resource will be blocked.

尝试修复

  • 验证哈希值是否正确:双重检查计算是二进制的,就是它。没什么可做的,其他浏览器都接受它们。
  • default-srcconnect-src的值更改为self而不是none

我想不出任何其他事情可以尝试。

24小时后更新:为完整性添加了X-Content-Security-Policy&amp; JSBin URL已更新,但它对这种特定情况没有任何影响。

2 个答案:

答案 0 :(得分:9)

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

IE 11不支持Content-Security-Policy(仅X-Content-Security-Policy),此操作无法打开。 IE 12支持CSP,但不会删除nonce / hashes,它会关闭...除非你还在'unsafe-inline'标题中提供Content-Security-Policy

CSP级别2表示“如果提供了哈希或随机数,请忽略'unsafe-inline'。”这是为了向后兼容,因为旧版浏览器会查看'unsafe-inline'而不是nonces / hashes。见http://www.w3.org/TR/CSP2/#directive-script-src

答案 1 :(得分:3)

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

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

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