Google Chrome 从脚本标签中去除现时值

Google Chrome Stripping nonce values from script tags

我正在尝试将随机数值添加到我的内联脚本中以满足更严格的 CSP。但是,我 运行 遇到了一个奇怪的问题,其中 chrome 正在从 nonce 属性中剥离值。当我卷曲页面时,会出现 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 (Hide nonce content attribute values); see https://github.com/whatwg/html/issues/2369 的规范更新中添加的。

明确一点:规范要求的行为是,如果您通过网络提供的标记源具有:

<script nonce=DhcnhD3khTMePgXw>...</script>

...然后如果你打开浏览器开发工具并使用 DOM 检查器,你会看到的是:

<script nonce>...</script>

也就是说,DOM 检查器将不显示 script 元素上 nonce 属性的值。

更准确地说:如果文档使用 Content-Security-Policy header 和浏览器,您将看不到 scriptnonce 属性的值正在应用 header.

中的政策

如果您使用 Content-Security-Policy header 提供文档,或者浏览器不应用其中的策略,您将查看 nonce=DhcnhD3khTMePgXw 查看检查器中的 script 元素。

因此 DOM 检查器中缺少 nonce 属性的值实际上表明一切正常。也就是说,它表示浏览器正在检查 Content-Security-Policy header.

中任何 nonce-* 源表达式的匹配值

它在浏览器中的工作方式是:浏览器将 nonce 属性的值移动到“内部插槽”以供浏览器自己使用。因此它对浏览器保持可用,但对 DOM.

隐藏

您尚未在 Safari 中看到与 Chrome 相同的行为的原因是,Safari 尚未赶上规范更新并实现要求。但是有一个open Safari bug.


要检查您的浏览器是否符合规范行为,您可以使用此处的测试:

主要浏览器中这些测试的当前结果如下: