子资源完整性保护在 Chrome 中不起作用
Subresource Integrity Protection doesn't work in Chrome
我根据 Subresource Integrity 在我的脚本中添加了 integrity
和 crossorigin
标签,但现在他们在控制台中给出了一个错误
Script from origin 'http://pagead2.googlesyndication.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:81' is therefore not allowed access.
这是脚本:
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
integrity="sha256-5xwrIw3xU3VvipjVMZNyf6+27C/a1Pxl3U0jl3hTcao="
crossorigin="anonymous"></script> <!-- Banner --> <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-3346556035533863" data-ad-slot="8052306231"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
这是推导:
$ curl -s http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js | openssl dgst -sha256 -binary | openssl base64 -A
5xwrIw3xU3VvipjVMZNyf6+27C/a1Pxl3U0jl3hTcao=
我还向 Amazon 脚本添加了属性,但也失败了。
Script from origin 'http://s3.amazonaws.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:81' is therefore not allowed access.
<script async="async" type="text/javascript" src="//s3.amazonaws.com/cc.silktide.com/cookieconsent.latest.min.js"
integrity="sha256-/8egBZx4+UIzXH8il2CNdL5+npDYgoAsjC76KHO/re4=" crossorigin="anonymous"></script>
Chrome 支持 SRI:http://enable-cors.org/client.html, https://www.chromestatus.com/feature/6183089948590080
我使用规范中的示例验证了哈希是否正确生成:http://www.w3.org/TR/SRI/#integrity-metadata
为什么它不起作用?它在没有两个新属性的情况下工作。
Chrome版本 46.0.2490.86m
我认为是因为服务器没有明确允许CORS,所以Chrome阻止了它。 Silktide cookie 脚本已移至 Cloudflare,它确实具有 Access-Control-Allow-Origin
,因此现在可以使用了。我不知道为什么浏览器会关心服务器是否允许 cross-origin 脚本。似乎只要哈希匹配,它来自哪里并不重要。
<script async="async" type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/1.0.9/cookieconsent.min.js"
integrity="sha256-GQ/ALY6PHdWsxA9I0NYgPmEV6zHj9H9V2ww/B3l9aPA=" crossorigin="anonymous"></script>
服务器headers:
$ curl -I http://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/1.0.9/cookieconsent.min.js
HTTP/1.1 200 OK
Date: Tue, 24 Nov 2015 19:37:10 GMT
Content-Type: application/javascript
Connection: keep-alive
Last-Modified: Mon, 19 Oct 2015 15:05:30 GMT
Expires: Sun, 13 Nov 2016 19:37:10 GMT
Cache-Control: public, max-age=30672000
Access-Control-Allow-Origin: *
CF-Cache-Status: HIT
Server: cloudflare-nginx
CF-RAY: 24a791216c9b03f4-EWR
我根据 Subresource Integrity 在我的脚本中添加了 integrity
和 crossorigin
标签,但现在他们在控制台中给出了一个错误
Script from origin 'http://pagead2.googlesyndication.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:81' is therefore not allowed access.
这是脚本:
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
integrity="sha256-5xwrIw3xU3VvipjVMZNyf6+27C/a1Pxl3U0jl3hTcao="
crossorigin="anonymous"></script> <!-- Banner --> <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-3346556035533863" data-ad-slot="8052306231"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
这是推导:
$ curl -s http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js | openssl dgst -sha256 -binary | openssl base64 -A
5xwrIw3xU3VvipjVMZNyf6+27C/a1Pxl3U0jl3hTcao=
我还向 Amazon 脚本添加了属性,但也失败了。
Script from origin 'http://s3.amazonaws.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:81' is therefore not allowed access.
<script async="async" type="text/javascript" src="//s3.amazonaws.com/cc.silktide.com/cookieconsent.latest.min.js"
integrity="sha256-/8egBZx4+UIzXH8il2CNdL5+npDYgoAsjC76KHO/re4=" crossorigin="anonymous"></script>
Chrome 支持 SRI:http://enable-cors.org/client.html, https://www.chromestatus.com/feature/6183089948590080
我使用规范中的示例验证了哈希是否正确生成:http://www.w3.org/TR/SRI/#integrity-metadata
为什么它不起作用?它在没有两个新属性的情况下工作。
Chrome版本 46.0.2490.86m
我认为是因为服务器没有明确允许CORS,所以Chrome阻止了它。 Silktide cookie 脚本已移至 Cloudflare,它确实具有 Access-Control-Allow-Origin
,因此现在可以使用了。我不知道为什么浏览器会关心服务器是否允许 cross-origin 脚本。似乎只要哈希匹配,它来自哪里并不重要。
<script async="async" type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/1.0.9/cookieconsent.min.js"
integrity="sha256-GQ/ALY6PHdWsxA9I0NYgPmEV6zHj9H9V2ww/B3l9aPA=" crossorigin="anonymous"></script>
服务器headers:
$ curl -I http://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/1.0.9/cookieconsent.min.js
HTTP/1.1 200 OK
Date: Tue, 24 Nov 2015 19:37:10 GMT
Content-Type: application/javascript
Connection: keep-alive
Last-Modified: Mon, 19 Oct 2015 15:05:30 GMT
Expires: Sun, 13 Nov 2016 19:37:10 GMT
Cache-Control: public, max-age=30672000
Access-Control-Allow-Origin: *
CF-Cache-Status: HIT
Server: cloudflare-nginx
CF-RAY: 24a791216c9b03f4-EWR