I recently discovered the following nifty little site for generating SubResource Integrity (SRI) Tags for externally loaded resources. For example, enterring the latest jQuery URL (https://code.jquery.com/jquery-3.3.1.min.js), one gets the following <script>
tag:
<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>
I understand the purpose of SRI hashes, and I know that they can use different hash sizes (256-, 384-, or 512-bit), but I had never seen all three used at once like this before. Digging into the MDN docs, I found that
An integrity value may contain multiple hashes separated by whitespace. A resource will be loaded if it matches one of those hashes.
But how exactly is that matching performed? Time for multiple questions in one SO post...
- Do browsers attempt to match the longest hash first, since its more secure, or the shortest first, since its faster?
- Would one really ever expect for one hash to match and not all three (other than the trivial case of a developer mistyping a hash)?
- Is there any benefit to providing all three hashes instead of just one?
- Similar to #1, If you only provide one hash value, which should you use? 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?
- 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.