Home / Knowledge hub / Subresource integrity
Knowledge hub
Subresource integrity
A hash check that stops a tampered third-party script or stylesheet from loading.
What it means
Subresource integrity, or SRI, lets a page say what a file should look like before it loads it. You add an integrity attribute to a script or stylesheet tag, holding a base64 hash (SHA-256, SHA-384 or SHA-512) of the expected content. The browser fetches the file, hashes it and runs it only if the two match. Nothing else on the page changes.
If a CDN is compromised or someone swaps the file, the hash no longer matches and the browser refuses it. For files on another host, the tag also needs the crossorigin attribute so the browser can check the content.
Why it matters
A third-party script runs with the same rights as your own. If it is changed at the source, every site that loads it runs the attacker's code, with your customers' sessions and the page's forms in reach. That is a supply chain attack, and payment-page skimmers have used this route.
SRI turns a silent swap into a blocked load and a visible error. That is a better failure than an unnoticed one. It does not make a third party trustworthy, but it makes sure you only run the version you reviewed.
How it shows up
You meet it on script and link tags that point to a CDN, and in build tools that add hashes for you. You must update the hash when you update the file, so it suits versioned files that never change. A URL that serves different code over time will break the page.
Mistakes: no hash on tags that need one, a hash that is never updated, a missing crossorigin so the check fails, and scripts that load further scripts which SRI does not cover. It is also no help for tag managers, which change by design. For those, use a content security policy and review changes.
Questions people ask
Subresource integrity, answered
Which tags support the integrity attribute?
Browsers support it on script and link tags for stylesheets, and some other link types. It does not cover images or scripts loaded by other scripts.
What happens if the hash does not match?
The browser blocks the file and logs an error in the console. The page carries on without it, so test that your page degrades in a way you can accept.
Can I use SRI with a script that updates often?
Not easily. Every change breaks the hash. Host a pinned copy yourself, or pin a versioned URL, and update the hash deliberately.
Is SRI the same as a content security policy?
No. A policy limits where scripts may come from. SRI checks that a file from an allowed place has the exact content you expect. They work well together.
How do we find third-party scripts without SRI?
List every script and style tag in your pages and compare what loads at runtime with what is in your templates. Tools can flag missing integrity attributes, and a browser test shows what actually loads.
Sources
Where this comes from
Related
Terms that sit next to this one
Supply chain attack
Reaching you through a library, a build tool or a vendor you trust.
Content Security Policy
A header telling the browser which scripts a page may load.
Third-party script risk
Code from outside vendors running on your pages with the same powers as yours.
CSP nonce
A one-time value that marks which inline scripts a content security policy allows.
Hashing
A one-way transformation, so a password can be checked but never read back.
What else loads into your pages?
We look at what your running pages load from outside and whether a changed file would be caught, and we report only what we proved. Ask us to run it on one of your applications.
Or start with a $199 pilot on one application: thirty days, success criteria agreed before day one, credited against the annual if you convert.