javascript - 在 Firefox 中使用 csp sha-256 将内联脚本列入白名单

标签 javascript firefox content-security-policy

我无法通过校验和获得白名单以在 Firefox(52.0.2,Windows)中工作。根据 caniuse,Firefox 支持内容安全策略版本 2,因此应该支持校验和。

当 chrome 阻止内联脚本时,它会将所需的 sha-256 打印到控制台。 将其添加到 csp 规则成功将脚本列入白名单。 校验和也与计算的相同 https://report-uri.io/home/hash

但是firefox不接受。

我注意到 MDN 文档中的示例使用 base-16 而不是 base-64 编码作为校验和。 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src

但即使使用 MDN 示例,我也会得到相同的结果。 (Chrome 也拒绝使用 base-16 编码)。我在以下方面尝试了多种变体:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Security-Policy"
          content="script-src 'sha256-076c8f1ca6979ef156b510a121b69b6265011597557ca2971db5ad5a2743545f'">
    <title>Hello CSP</title>
</head>
<body>
    <script type="text/javascript">var inline = 1;</script>
</body>
</html>

Content Security Policy: The page’s settings blocked the loading of a resource at self (“script-src 'sha256-076c8f1ca6979ef156b510a121b69b6265011597557ca2971db5ad5a2743545f'”). Source: var inline = 1;.

最佳答案

如果您更改哈希值,它将起作用,如下所示:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="Content-Security-Policy"
        content="script-src 'sha256-B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8='">
  <title>Hello CSP</title>
</head>
<body>
  <script type="text/javascript">var inline = 1;</script>
</body>
</html>

不确定您为什么会在 Chrome 中看到您所描述的行为;当我在 Chrome 中测试问题中的示例时,它会阻止脚本并发出一条错误消息,指出使用哈希值 sha256-B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=

https://report-uri.io/home/hash在给定 var inline = 1; 时也会输出该值。

关于javascript - 在 Firefox 中使用 csp sha-256 将内联脚本列入白名单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108890/

相关文章:

javascript - 如何使用 JavaScript 从 URL 获取变量并将其显示在屏幕上?

javascript - 设置 react 路由器

javascript - 如何测试销毁范围

javascript - 我可以在 Firefox 扩展弹出通知中设置背景颜色吗?

firefox - 如何在 Robot Framework 中运行 headless (headless)测试

javascript - 使用 Content-Security-Policy 和 JavaScript 避免 `script-src ' unsafe-inline'`

javascript - 推送到 dom-repeat 中使用的数据绑定(bind)数组( polymer )

javascript - Chrome 和 Firefox 中的 nicEditor 链接问题

html - 将谷歌字体 (fonts.googleapis.com) 添加到 CSP header

javascript - 事件内容安全策略 (CSP) 和 Rails :back link