Your checkout page loads a small price-formatting library from a CDN (a network of servers that delivers files, such as JavaScript libraries, to many sites). One day someone else replaces that file on the CDN, and your users' form data starts going to a stranger's server. Your own code never changed a line.
SRI (Subresource Integrity) stops a replaced CDN file from running on your page. You write the fingerprint (hash) of the original file in an integrity attribute, on the <script> or <link> tag. The browser computes the hash of the file that actually arrives from the CDN. When the result differs from integrity, the browser doesn't use the file. Picture ordering medicine, and the pharmacy gives you a code computed from what's in it. When the parcel arrives, you compute the code again from the contents. If a single pill was swapped, the code changes completely, and you don't take that medicine. The parcel of medicine is the JavaScript file from the CDN, the pharmacy's code is the hash in integrity, and the one who computes it again is the browser.
Why files from a CDN need checking
Say you write <script src="https://cdn.example.com/lib.js"></script>. That file runs on your page with the same rights as your own code: it can read form contents, change the page, and send data anywhere. You're trusting the CDN never to change it. If the CDN is broken into, or its domain is sold to someone else, the attacker's code runs on every site that loads the file.
This has happened. In June 2024 it was found that the domain polyfill.io was adding malicious code to the files it served. More than 100,000 sites loaded the script from that domain. The domain had been sold to a new owner a few months before.
A hash: a file's fingerprint
A hash (you met it in the Encryption vs Hashing vs Encoding lesson) turns any content into a fingerprint of fixed length. The hash algorithm SHA-384 produces 48 bytes, written in base64 as 64 characters. The same content always gives the same hash. Content that differs by a single byte gives a completely different hash.
This is a small lib.js file (80 bytes):
function formatPrice(amount) {
return "$" + amount.toLocaleString("en-US");
}
This table shows the SHA-384 of that file, before and after one character changes:
| File content | SHA-384 (base64) |
|---|---|
original, "$" |
OTgONS824A9wS7ZtLsdDNR9w0EVcvNbKlga0369Z8GZIFGVxnF5IVbz6+ixAt9zn |
changed, "S" |
0G1vaUuh2+KB6YNRN5H/QvTa6+pVLtYkuRP7fpabrhxCAyCuTlxIHqNt9Fk1XDDQ |
Only one character changed, $ to S, yet the hash changed completely. No part of it is alike. So an attacker can't change the file and keep its hash.
Adding integrity and crossorigin
This is the <script> tag for the original lib.js, with two extra attributes:
<script
src="https://cdn.example.com/lib.js"
integrity="sha384-OTgONS824A9wS7ZtLsdDNR9w0EVcvNbKlga0369Z8GZIFGVxnF5IVbz6+ixAt9zn"
crossorigin="anonymous"
></script>
integrityholds the algorithm name (sha256,sha384, orsha512), a minus sign, then the hash in base64.sha384is the most common choice.- The browser downloads the file, computes the SHA-384 of its contents, then compares the result with
integrity. If they're equal, the script runs. If they differ, the script is blocked. For the file changed to"S", Chrome writes this message in the console:
Failed to find a valid digest in the 'integrity' attribute for resource 'https://cdn.example.com/lib.js' with computed SHA-384 integrity '0G1vaUuh2+KB6YNRN5H/QvTa6+pVLtYkuRP7fpabrhxCAyCuTlxIHqNt9Fk1XDDQ'. The resource has been blocked.
crossorigin="anonymous"makes the browser request the file through CORS, without cookies. The browser may only read a file's contents from another origin when CORS allows it (remember the CORS Deep Dive lesson). Without reading the contents, the browser can't compute the hash. Public CDNs such as cdnjs and jsDelivr answer with the headerAccess-Control-Allow-Origin: *, so adding this attribute is enough. Withoutcrossorigin, the file is blocked even when it's the original:
Subresource Integrity: The resource 'https://cdn.example.com/lib.js' has an integrity attribute, but the resource requires the request to be CORS enabled to check the integrity, and it is not. The resource has been blocked because the integrity cannot be enforced.
CSS works the same way: <link rel="stylesheet" href="..." integrity="sha384-..." crossorigin="anonymous">.
Making the hash
In a terminal, compute the hash of the original file. The line that starts with # is the result:
openssl dgst -sha384 -binary lib.js | openssl base64 -A
# OTgONS824A9wS7ZtLsdDNR9w0EVcvNbKlga0369Z8GZIFGVxnF5IVbz6+ixAt9zn
openssl dgst -sha384 -binaryasks openssl (a cryptography program for the terminal) to compute the SHA-384 and output it as raw bytes.openssl base64 -Aturns those bytes into base64 on one line. Putsha384-in front.
Or in the browser, use Web Crypto (the browser's built-in feature for hashing and encryption). The Lab uses this way too:
async function sriHash(text) {
const bytes = new TextEncoder().encode(text);
const digest = await crypto.subtle.digest("SHA-384", bytes);
const base64 = btoa(String.fromCharCode(...new Uint8Array(digest)));
return "sha384-" + base64;
}
TextEncoderturns the text into UTF-8 bytes, because a hash is computed from bytes.crypto.subtle.digestreturns a Promise holding the hash, so useawait.btoa(...)turns the bytes into base64.
The cdnjs and jsDelivr pages also show the full tag with its integrity, ready to copy.
💡 Several hashes, and SRI's limits
The integrity attribute may hold several hashes, separated by spaces. The algorithms in order of strength, from the weakest: sha256, sha384, sha512. The browser only uses the strongest algorithm among the hashes you wrote. Then it accepts the file when its contents match any hash of that algorithm. So integrity="sha384-AAA sha384-BBB" accepts two versions of the file.
Limits you should know:
- SRI applies to
<script>and some kinds of<link>, such as<link rel="stylesheet">, not to images or fonts. - A file whose content keeps changing can't use SRI, because it has no single fixed hash. polyfill.io itself is an example: its content was different for each browser. The fix is to keep a copy of the file on your own server.
- Every time a library gets a new version, the hash changes too. Use a URL with a fixed version, such as
[email protected], notlatest(the newest version, which changes by itself).
⚠️ Hashes that don't match, and what SRI doesn't guard
- Forgetting
crossorigin="anonymous". A file from another origin is blocked even when its hash is right. The reason: without CORS, the browser may not read its contents to compute the hash. - A URL without a fixed version.
.../latest/lib.jschanges when the library releases a new version. The hash no longer matches, and features on your page suddenly stop working. - A hash computed from a different file. A file minified again (made smaller again), or one that differs by one newline (line break) at the end, already gives another hash. Compute the hash from the exact file the CDN serves.
- Thinking SRI guards every resource. Images, fonts, and scripts with dynamic content (content that keeps changing) aren't covered. For third-party scripts you can't lock with a hash, read the Third-Party Script Risks lesson.
TL;DR: SRI (Subresource Integrity) puts a file's hash (its fingerprint) in the integrity attribute of a <script> or <link> tag. The browser computes the hash of the file that arrives from the CDN, then refuses the file when the hash doesn't match. The format is sha384- followed by the hash in base64, such as sha384-OTgONS824A9wS7ZtLsdDNR9w0EVcvNbKlga0369Z8GZIFGVxnF5IVbz6+ixAt9zn for a small lib.js. Change a single character and the hash changes completely. A file from another origin must have crossorigin="anonymous", so that the browser requests it through CORS. Without it, the file is blocked because its hash can't be checked. Make the hash with openssl dgst -sha384 -binary lib.js | openssl base64 -A or crypto.subtle.digest("SHA-384", ...). SRI only covers <script> and stylesheets whose content is fixed, so use URLs with a fixed version.
⚡ A file's fingerprint
Meaning: A hash turns any content into a fingerprint of fixed length. Content that differs by a single byte gives a completely different hash: in the file lib.js, changing "$" to "S" changes the whole hash. So an attacker can't change the file and keep its hash. SRI (Subresource Integrity) uses hashes to check files from a CDN.
⚡ integrity and crossorigin
Write:
<script
src="https://cdn.example.com/lib.js"
integrity="sha384-OTgONS824A9wS7ZtLsdDNR9w0EVcvNbKlga0369Z8GZIFGVxnF5IVbz6+ixAt9zn"
crossorigin="anonymous"
></script>
Guess first: crossorigin="anonymous" makes the browser request the file through CORS. The hash is right, but crossorigin was forgotten. Does the script run?
No, it's blocked. Without CORS, the browser may not read a file's contents from another origin, so it can't compute the hash.
Meaning: The integrity attribute holds the hash of the original file: the name of the hash algorithm (here sha384), a minus sign, then the hash in base64. The browser downloads the file, computes its SHA-384, then compares the result with integrity. Equal: the script runs. Different: the script is blocked.
⚡ Making the hash
Write:
openssl dgst -sha384 -binary lib.js | openssl base64 -A
Meaning: This command computes the SHA-384 of lib.js and writes it in base64. Put sha384- in front of the result, then write it in integrity. The hash must come from exactly the file the CDN serves: a file minified again (made smaller again) already gives another hash.
⚡ The version must be fixed
Meaning: A URL like .../latest/lib.js always points to the newest version, so its content changes each time the library releases a new version. The hash no longer matches integrity, and the browser blocks your script. Use a URL with a fixed version, such as [email protected].