【问题标题】:Subresource Integrity Protection doesn't work in Chrome子资源完整性保护在 Chrome 中不起作用
【发布时间】:2016-02-15 13:19:19
【问题描述】:

我根据Subresource Integrity在我的脚本中添加了integritycrossorigin标签,但现在他们在控制台中给出了一个错误提示

来自“http://pagead2.googlesyndication.com”的脚本已被跨域资源共享策略阻止加载:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'http://localhost:81'。

这是脚本:

<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 脚本中,但也失败了。

来自“http://s3.amazonaws.com”的脚本已被跨域资源共享策略阻止加载:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'http://localhost:81'。

<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.htmlhttps://www.chromestatus.com/feature/6183089948590080

我使用规范中的示例验证了哈希是否正确生成:http://www.w3.org/TR/SRI/#integrity-metadata

那么为什么它不起作用?它可以在没有两个新属性的情况下工作。

参考:https://www.srihash.org/

Chrome 版本 46.0.2490.86 m

【问题讨论】:

    标签: javascript google-chrome security subresource-integrity


    【解决方案1】:

    我认为是因为服务器没有明确允许 CORS,所以 Chrome 阻止了它。 Silktide cookie 脚本已移至 Cloudflare,它确实有 Access-Control-Allow-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>
    

    服务器标头:

    $ 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
    

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 1970-01-01
      • 2016-07-05
      • 2016-11-02
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      相关资源
      最近更新 更多