I'm reading about Subsource Integrity on Mozilla [1]. They analyse the code snippet below and write the following about it:
The anonymous value means that the browser should omit any cookies or authentication that the user may have associated with the domain. This prevents cross-origin data leaks, and also makes the request smaller.
<script src="https://code.jquery.com/jquery-2.1.4.min.js"
integrity="sha384-R4/ztc4ZlRqWjqIuvf6RX5yb/v90qNGx6fS48N0tRxiGkqveZETq72KgDVJCp2TC"
crossorigin="anonymous"></script>
I'm interested in why this prevents cross-origin data leaks [2]. Mozilla writes the following:
Attackers would attempt to load the resource with a known digest, and watch for load failures. If the load fails, the attacker could surmise that the response didn’t match the hash and thereby gain some insight into its contents. This might reveal, for example, whether or not a user is logged into a particular service.
I understand how the integrity attribute can be used to reveal the contents of the resource. This seems interesting if the contents are client-dependent. However, I do not understand two things:
- If it happens to be the case that a hacker is interested in the contents of a resource when the resource is loaded by a particular client, and the hacker can control the HTML and Javascript on this page. Then why would the hacker try to reveal the contents of said resource by using the integrity hash, why not just fetch the resources on the client and send the
responseTextto a malicious website straight away? Unless CORS prevents this, of course, but it seems like CORS blocks SRI just as much as other requests [3]... - Mozilla seems to describe that website-owners need to set
crossorigintoanonymousso that hackers can not "bruteforce" page info by brute-forcing hashes? If the hackers can control theintegrityattribute on the page, they can control thecrossoriginattribute too in most cases, so why does this matter?