Subresource Integrity, Done Properly
A browser feature that checks a script or stylesheet loaded from a CDN against a cryptographic hash, refusing to run it if the file has been altered.
On this page
What is at stake
If a third-party CDN is compromised, integrity checks prevent the altered files from running on your site.
The playbook
- Add integrity hashes to third-party library files
- Pin exact versions of CDN resources
- Generate hashes during the build
- Update hashes when upgrading libraries
- Combine with a Content Security Policy
Where it goes wrong
Avoid:
- Loading latest-version URLs that change
- Omitting crossorigin with integrity
- Hand-copying hashes that drift out of date
- Assuming SRI protects dynamic scripts
The numbers behind it
| Measure | Figure |
|---|---|
| Attribute | integrity, containing a hash such as sha384 |
| crossorigin | required for cross-origin resources |
| Behavior | the browser blocks the resource if the hash does not match |
| Limitation | works only for files that do not change |
Getting outside help
When to hand it over: Bring in help when a site loads critical scripts from third-party CDNs, or when hardening payment pages.
Where this comes from
- MDN Web Docs — Subresource Integrity
- W3C — Subresource Integrity specification
The figures and practices above come from the sources listed.
Working on something like this?
We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.
Where to go next
Spotted something wrong? Report an error on this page. We correct on the page and say what changed.