How does Subresource Integrity check scripts and styles?
Useful first: Same-origin policy · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
YOUR GOALExplain what a content match establishes, and what it does not.
Recognize three different checks
A content expectation travels with the page
A page can attach an integrity value to a script or stylesheet reference. The browser calculates a hash of the fetched content and compares it with the allowed value. Think of a receipt describing an expected delivery: the comparison checks the delivery, not whether you chose a good product.
HTTPS protects the journey
HTTPS uses TLS to protect the connection against unnoticed alteration in transit and to authenticate the server. A compromised server can still send unwanted code over that protected connection. SRI adds a separate content expectation supplied by the page.
[5] [1]CSP sets loading and execution rules
Content Security Policy can restrict which scripts a page may load or execute. Allowing a CDN host does not by itself pin every file that host serves. An SRI match also does not override a blocking CSP rule. These checks can complement each other.
[4] [2]Ask separate questions about transport, expected content and permission to run.
A matching integrity hash proves a script is harmless.
It establishes a content match; the chosen code can still contain bugs or unwanted behavior.
Show the explanation
Reject the script because the integrity check failed. Host permission does not waive the content check. A failed SRI check prevents use of that resource.
Progress is saved only in this browser.
YOUR GOALConnect an exact file, its digest and cross-origin delivery settings.
Prepare a reproducible resource check
Hash the file you will serve
For this worked example, ready-v1.js contains the UTF-8 text below, followed by exactly one LF newline:
window.demoReady = true;
Its SHA-384 digest is encoded as Base64, not hexadecimal. This example reference uses that calculated value; the example domain is not a hosted demo:
<script src="https://cdn.example.com/ready-v1.js"
integrity="sha384-XmKrMR2rwLkfU6W6XQO16FoWZUjZioQ0325cvKcf8TERbWoS0u/V/YLDgBBtg/X0"
crossorigin="anonymous"></script>[1] [2]The server must permit the cross-origin fetch
For this cross-origin classic script, crossorigin="anonymous" selects CORS with same-origin credentials. Credentials such as cookies are omitted on the cross-origin request. The server must also return a suitable Access-Control-Allow-Origin header; a public credential-free resource can use *. A correct hash does not supply that permission.
Deploy the reference and file together
If a release changes the file, generate the digest from the intended release bytes and update its reference together. A minifier or newline conversion can change those bytes. When loading fails, check the actual response, its digest and CORS separately; HTTP 200 alone does not establish browser acceptance. Removing integrity to silence the error removes that check.
[1] [2]Keep the file, digest, reference and cross-origin response settings consistent.
Adding crossorigin="anonymous" makes the CDN permit access.
The attribute configures the browser request; the response must independently satisfy CORS.
Show the explanation
No; the cross-origin integrity check also requires an eligible CORS response. Content matching and cross-origin eligibility are separate requirements. Neither the digest nor the attribute adds a server response header.
Progress is saved only in this browser.
YOUR GOALDiagnose hash selection and identify the trust that SRI still requires.
Reason about integrity failure boundaries
Choose the strongest supported metadata
With valid, supported SHA-256 and SHA-384 entries, the browser uses the SHA-384 set. A matching SHA-256 value does not rescue a SHA-384 mismatch. Several entries using the selected algorithm permit a match against any one of those entries; their order is not a fallback sequence.
[2] [1]A decorative attribute is not a guarantee
Unsupported algorithms and malformed metadata are not a reliable fail-closed policy: parsing can leave no usable integrity requirement. Validate generated markup instead of inventing placeholder digests. For ordinary scripts and styles, hashing uses the content after decoding HTTP content codings such as gzip, rather than the compressed wire bytes.
[1] [2]Protect the page that supplies the expectation
An attacker who can replace the page can also replace its integrity metadata. Delivering that page over HTTPS protects it in transit; SRI does not repair a compromised page publisher. CSP can add independent restrictions, including nonce- or hash-based script policies, but no matching digest establishes that the selected code is appropriate for your application.
[1] [4]Validate the metadata, follow algorithm selection, and protect its source of trust.
Any matching listed hash is sufficient, even when a stronger supported one fails.
The strongest supported metadata set is selected first; weaker matches are not fallback approval.
Show the explanation
The integrity check fails because the selected SHA-384 set has no match. SHA-384 is selected over SHA-256 here. To accept alternative content, an entry in the selected algorithm set must match.
Progress is saved only in this browser.
Keep asking. Check the details.
Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.
No human review is claimed. AI review can miss errors; check the original sources below.
- W3C — Subresource Integrity, Recommendation of 23 June 2016
- MDN — Subresource Integrity
- MDN — crossorigin HTML attribute
- MDN — Content Security Policy guide
- MDN — Transport Layer Security