html - 最佳 SRI 哈希大小是多少?

标签 html hash subresource-integrity

我最近发现了以下 nifty little site用于为外部加载的资源生成 SubResource Integrity (SRI) 标签。例如,输入最新的 jQuery URL ( https://code.jquery.com/jquery-3.3.1.min.js ),得到以下 <script>标签:

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8= sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT sha512-+NqPlbbtM1QqiK8ZAo4Yrj2c4lNQoGv8P79DPtKzj++l5jnN39rHA/xsqn8zE9l0uSoxaCdrOgFs6yjyfbBxSg==" crossorigin="anonymous"></script>

我了解 SRI 哈希的用途,并且我知道它们可以使用不同的哈希大小(256 位、384 位或 512 位),但我以前从未见过像这样同时使用这三种哈希值。深入研究 MDN docs , 我发现

An integrity value may contain multiple hashes separated by whitespace. A resource will be loaded if it matches one of those hashes.

但是这种匹配究竟是如何进行的呢?在一个 SO 帖子中提出多个问题的时间......

  1. 浏览器是先匹配最长的哈希值,因为它更安全,还是先匹配最短的哈希值,因为它更快?
  2. 人们真的会期望一个 哈希匹配而不是所有三个(除了开发人员错误输入哈希的微不足道的情况)吗?
  3. 提供所有三个哈希值而不是一个哈希值有什么好处吗?
  4. 与 #1 类似,如果您只提供一个哈希值,您应该使用哪个?我通常会看到网站(例如 Bootstrap)在其示例代码中提供 sha384 值。是不是因为它就在中间,不太大也不太小?
  5. 出于好奇,integrity 可以吗?属性可用于 <script> 旁边的任何标签和 <link> .我特别想知道像 <img> 这样的多媒体标签, <source>

最佳答案

  1. Do browsers attempt to match the longest hash first, since its more secure, or the shortest first, since its faster?

根据 https://w3c.github.io/webappsec-subresource-integrity/#agility , “用户代理将选择列表中最强的哈希函数”

  1. Would one really ever expect for one hash to match and not all three?

没有。但就浏览器行为而言:如果最强的哈希值匹配,浏览器将使用它并忽略其余的(因此不管其他是否匹配也无所谓)。

  1. Is there any benefit to providing all three hashes instead of just one?

实践中没有当前的好处。那是因为根据 https://w3c.github.io/webappsec-subresource-integrity/#hash-functions , “符合标准的用户代理必须支持 SHA-256、SHA-384 和 SHA-512 加密哈希函数”

所以目前,如果您只指定一个 SHA-512 哈希值,所有支持 SRI 的浏览器都会使用它。

但根据 https://w3c.github.io/webappsec-subresource-integrity/#agility指定多个散列的目的是“在面对 future 的密码学发现时提供敏捷性……鼓励作者在可用时开始迁移到更强大的散列函数”

换句话说,在未来的某个时候,浏览器将开始添加对更强大的哈希函数(基于 SHA-3 的 https://en.wikipedia.org/wiki/SHA-3 或其他)的支持。

因此,由于您需要继续针对较旧的浏览器和较新的浏览器,因此在一段时间内,您会针对某些 SHA-512 是最强哈希函数的浏览器,同时还针对届时出现的新浏览器将增加对某些 SHA-3(或其他)哈希函数的支持。

所以在这种情况下,您需要在 integrity 中指定多个哈希值值(value)。

  1. Similar to #1, If you only provide one hash value, which should you use?

SHA-512 值。

I typically see sites (e.g., Bootstrap) providing sha384-values in their example code. Is that because its right in the middle, not too big, not too small?

我不知道他们为什么选择那样做。但是,由于浏览器需要支持 SHA-512 哈希,因此您通过指定 SHA-384 哈希不会获得任何好处 — 事实上,您只是失去了拥有可用的最强哈希函数的值(value)。

  1. Out of curiosity, can the integrity attribute be used on any tags beside <script> and <link>.

不,还不能。

I'm particularly wondering about multimedia tags like <img>, <source>, etc.

作为https://w3c.github.io/webappsec-subresource-integrity/#verification-of-html-document-subresources解释说,SRI 的计划一直是最终也用于那些 —

Note: A future revision of this specification is likely to include integrity support for all possible subresources, i.e., a, audio, embed, iframe, img, link, object, script, source, track, and video elements.

……但我们还没有进入那个 future 。

关于html - 最佳 SRI 哈希大小是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52101501/

相关文章:

html - 使用位置 :fixed 修复了 div 中的 div

javascript - 使用getuikit框架扩展第二列

javascript - 根据字符串获取永久数组项

html - href 属性中带有哈希的 anchor 正在打开一个新页面

html - CDN 上包含 CSS 的字体的子资源完整性(例如 font-awesome)

javascript - 为使用 insertAdjacentHTML 创建的 HTML 元素分配唯一 ID

html - 在不扭曲图像的情况下动画div高度

ruby-on-rails - rails : form_for with json: undefined method to_model for Hash

html - 图像和其他媒体的子资源完整性?

javascript - 指定来自不受信任主机的脚本的散列