问题
Content-Security-Policy 默认情况下应该将脚本和样式解析列入黑名单,并根据各种指令允许它被验证,其中一个是验证预期输出的哈希值。浏览器必须无法实现任何未事先获得匹配哈希的 Javascript 或 CSS。具有匹配哈希的代码应正常执行。Microsoft Edge 拒绝所有 JS/CSS 页面内块。
说明在 Microsoft Edge 以及任何其他浏览器中访问下面的实时演示链接。
现场演示: http: //output.jsbin.com/biqidoqebu
演示原始源代码
<!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-src
将andconnect-src
的值更改为self
而不是none
我想不出还有什么可以尝试的。
24 小时后更新:添加 X-Content-Security-Policy 以确保完整性并更新 JSBin URL,尽管它对这种特殊情况没有影响。