Subresource Integrity explained: pinning a CDN script to its hash
When you load a script from a CDN, you trust that CDN with full execution rights on your page. You also trust every future version of that file. If the CDN is compromised, or someone with access swaps the file, the new code runs with the same access to your DOM, cookies and forms as your own code.
Subresource Integrity, usually shortened to SRI, closes that gap. You declare the hash of the exact file you expect, and the browser refuses to execute anything that does not match.
How it works
SRI is an integrity attribute on a <script> or <link rel="stylesheet"> tag. The value is an algorithm name, a hyphen, and a base64 digest of the file:
<script
src="https://cdn.example.com/lib/4.2.1/lib.min.js"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+..."
crossorigin="anonymous"></script>
The browser downloads the file, hashes the bytes it received, and compares the result to the value you declared. If they match, the file runs. If they do not, it is discarded.
Three algorithms are permitted: sha256, sha384 and sha512. sha384 is the common choice. All three are strong enough for this job, and the differences between hash families are covered in MD5 vs SHA-1 vs SHA-256.
Generating the value
The digest is the raw hash bytes encoded in base64. It is not hex. Hex is what most command line hashing tools print by default, so the conversion is the step people get wrong. Pasting a hex digest after sha384- produces a value that looks plausible and never matches.
On the command line, hash the file in binary mode and pipe the bytes straight into a base64 encoder:
openssl dgst -sha384 -binary lib.min.js | openssl base64 -A
Prefix the output with sha384- and you have the attribute value. The -binary flag is the important part. Without it, openssl prints hex text, and you end up encoding the hex characters instead of the bytes they represent. If the difference between the two encodings is hazy, how Base64 encoding works walks through it.
Hash the file you actually serve. A single changed byte, such as a trailing newline added by an editor or a different minifier setting, produces a completely different digest. You can watch that happen by pasting two nearly identical strings into the Hash Generator and comparing the SHA-384 output. Keep in mind that it prints hex, which is right for comparing digests by eye and still needs converting before it goes into an integrity attribute.
The crossorigin attribute
For a resource on another origin, integrity needs crossorigin="anonymous" beside it. To hash the file, the browser has to read the response body, and it is only allowed to do that for a cross origin response when the server has approved it through CORS. The attribute makes the request a CORS request, and the CDN answers with an Access-Control-Allow-Origin header.
Leave the attribute out and the check cannot be performed, so it fails and the resource is blocked. This is the single most common SRI mistake, and it is confusing because the hash itself may be perfectly correct. If a script with a verified hash refuses to load, check for crossorigin first, then check that the CDN sends the CORS header at all.
A mismatch is a hard failure
When the hashes differ, the resource is not executed and an error appears in the console. There is no fallback and no warning mode. That is intentional, because a check that lets the file run anyway protects nothing.
It does mean SRI only suits a URL whose content never changes. It is the wrong tool for:
- a
latesttag or any unversioned URL that follows new releases - an endpoint that minifies or bundles on request, where the output can shift between builds
- a font CSS file that the provider varies by user agent
In each case the file will legitimately change one day, the hash will stop matching, and part of your site will stop working with nothing but a console message to explain it. Pin a versioned, immutable URL, or do not use SRI on that resource.
Changing a file without an outage
You can list several hashes in one attribute, separated by spaces:
<script
src="https://static.example.com/app.js"
integrity="sha384-OLDdigest... sha384-NEWdigest..."
crossorigin="anonymous"></script>
The browser accepts the file if it matches any of them. This is how you replace a file at a fixed URL without a window where nothing loads: publish the page with both hashes, swap the file, then remove the old hash once caches have cleared.
When the list mixes algorithms, the browser prefers the strongest one present and checks against that. So use the same algorithm for the old and new hash. A sha256 value for one version next to a sha384 value for the other will not behave as a pair.
What SRI does not do
SRI verifies integrity, not freshness. It will happily keep running an old version with a known vulnerability, because the hash of that old file still matches. Pinning a version makes updates your job, so you need some routine for noticing when a dependency has a security release.
It also only covers the tags you remember to annotate. There is no browser enforced way to require integrity metadata on every script, so the check has to live in review or in your build. Content Security Policy is the useful companion here: it limits which origins may serve a script at all, which narrows the problem SRI then pins down. See content security policy explained for how the two fit together.
The remaining option is to self host the file. That removes the third party from the picture entirely, and with it most of the reason for SRI on that resource.
A short checklist
- Use a versioned, immutable URL.
- Generate the digest from the exact bytes served, in base64, with the algorithm prefix.
- Add
crossorigin="anonymous"for anything on another origin. - Load the page and read the console. A blocked resource says so there.
- Recompute the hash every time you change the version.