gpt4 book ai didi

javascript - 谷歌浏览器从脚本标签中剥离 nonce 值

转载 作者:行者123 更新时间:2023-12-05 00:45:48 24 4
gpt4 key购买 nike

我正在尝试将 nonce 值添加到我的内联脚本中以满足更严格的 CSP。但是,我遇到了一个奇怪的问题,即 chrome 正在从 nonce 属性中剥离值。当我 curl 页面时,会出现 nonce 值。这导致脚本无法执行,因为它现在未通过 CSP 测试。我认为这可能是由于流氓扩展,但它在完全干净的 chrome 版本上失败了。 (OSX 上的版本 73.0.3683.103)nonce 的值是一个随机的 256 位基本编码字符串,因此它应该满足 nonce 的所有要求。

有谁知道发生了什么?难道我做错了什么?

最佳答案

问题中描述的实际上是预期的行为——根据 HTML 规范要求:
https://html.spec.whatwg.org/multipage/#nonce-attributes:attr-nonce

Elements that have a nonce content attribute ensure that the crytographic nonce is only exposed to script (and not to side-channels like CSS attribute selectors) by extracting the value from the content attribute, moving it into an internal slot named [[CryptographicNonce]]


https://html.spec.whatwg.org/multipage/#nonce-attributes:dom-noncedelement-nonce

…the setter for the nonce IDL attribute does not update the corresponding content attribute. This, as well as the below setting of the nonce content attribute to the empty string when an element becomes browsing-context connected, is meant to prevent exfiltration of the nonce value through mechanisms that can easily read content attributes, such as selectors.


此行为是在规范的更新中添加的 https://github.com/whatwg/html/pull/2373 (隐藏 nonce 内容属性值);见 https://github.com/whatwg/html/issues/2369 .
需要明确的是:规范要求的行为是,如果您通过网络提供的标记源具有:
<script nonce=DhcnhD3khTMePgXw>...</script>
…然后如果你打开浏览器开发工具并使用 DOM 检查器,你会看到的是:
<script nonce>...</script>
也就是说,DOM 检查器不会显示 nonce 的值。属性 script元素。
更准确地说: nonce 没有任何值(value)。属性 script如果文档提供了 Content-Security-Policy header ,并且浏览器正在该 header 中应用策略。
如果您不使用 Content-Security-Policy 提供文档 header ,或者浏览器不应用其中的策略,您将看到 nonce=DhcnhD3khTMePgXw对于 script检查器中的元素。
所以缺少 nonce 的值DOM 检查器中的属性实际上表明事情正在按预期工作。也就是说,它表示浏览器正在检查该值是否与任何 nonce-* 匹配。 Content-Security-Policy 中的源表达式标题。
它在浏览器中的工作方式是:浏览器移动 nonce属性的值到一个“内部槽”供浏览器自己使用。所以它对浏览器仍然可用,但对 DOM 是隐藏的。
您在 Safari 中还没有看到与 Chrome 相同的行为的原因是,Safari 没有 catch 规范更新并实现了要求。但是有一个 open Safari bug .

要检查您的浏览器是否符合规范行为,您可以在此处使用测试:
  • https://web-platform-tests.live/content-security-policy/nonce-hiding/

  • 这些测试在主要浏览器中的当前结果如下:
  • https://wpt.fyi/results/content-security-policy/nonce-hiding
  • 关于javascript - 谷歌浏览器从脚本标签中剥离 nonce 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55670985/

    24 4 0