Lesson 11 of 14 · 11m
Subresource Integrity
The integrity attribute pins the bytes a fetched file must match — how to generate hashes, the CORS requirement, and why mutable endpoints can't be pinned.
Pin the bytes, not the URL
A <script src> trusts a host. Subresource Integrity trusts a hash. The integrity attribute on <script> or <link> carries the base64 digest of the exact file you intend to run:
<script src="https://cdn.example/lib-1.4.2.min.js"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
crossorigin="anonymous"></script>The browser fetches the resource, hashes the bytes it actually received, and compares against the attribute. Mismatch — one byte different, whether from compromise, corruption, or a CDN "improvement" — and the file is refused: scripts do not execute, stylesheets do not apply. The violation also reports through CSP if you collect securitypolicyviolation events. The payload never reaches the parser; SRI fails closed.
Generate the hash yourself:
openssl dgst -sha384 -binary lib-1.4.2.min.js | openssl base64 -Asha256, sha384, and sha512 are all legal; the browser uses the strongest it recognizes. Multiple space-separated hashes are allowed for graceful rotation, and metadata can be appended after a ? — though in practice a single sha384 is what ships. Two properties of the check are worth internalizing: it hashes the raw response bytes before they reach any parser, and it authenticates content, not transport — SRI says "these bytes are what I pinned," not "this host is who it claims to be."
The CORS requirement that surprises people
The integrity check happens on a fetched subresource, and for cross-origin loads SRI requires the fetch to run in CORS mode — that is what crossorigin="anonymous" sets. The CDN must answer with Access-Control-Allow-Origin, or the fetch itself fails and the resource never loads at all. jsdelivr, unpkg, and cdnjs send the header; a random CDN may not. This is a breakage mode, not a bypass — the failure looks like the script simply being absent, which is exactly the kind of silent failure that ships to production if nobody tests the tag.
What SRI covers — and what it cannot
- Covered:
<script>,<link rel="stylesheet">, andlinkpreloads/modulepreloads — each element carrying its ownintegrity.fetch(url, {integrity: "sha384-..."})exists in the Fetch spec and works in Chromium. - Not covered:
<img>, scripts injected dynamically by other scripts (unless the injecting code setsintegrityon the element), and anything your bundler emits without a plugin. Webpack'sSubresourceIntegrityPluginand the Vite equivalent emit hashes at build time — which is how you SRI your own first-party bundle, the BA-Modernizr case. - Mismatch is loud but not fatal-looking. A refused script logs a console error and fires a
securitypolicyviolationreport; the page keeps rendering without it. Features silently degrade — which is why SRI belongs in your CSP reporting pipeline, not just your markup. - Immutable bytes only. SRI pins content, so it pairs exclusively with versioned, immutable URLs:
lib@1.4.2/lib.min.js. A mutable endpoint —latest, or a service that varies output per user-agent — cannot be pinned by design.cdn.polyfill.ioserved different bytes per browser fingerprint; anintegrityattribute on it would have failed constantly, which is exactly why nobody could pin it and why mutable third-party endpoints have no place on pages that matter.
The abandoned enforcement layer
CSP Level 3 defined require-sri-for script style to mandate integrity on every script and stylesheet. Only Chromium ever implemented it, and it has been effectively abandoned — do not rely on it. SRI lives on the element.
What to actually do
The order of preference: self-host > CDN with immutable URL + SRI > CDN with immutable URL. Mutable third-party scripts are unacceptable on authenticated or payment surfaces regardless. For your own deploys, wire the bundler plugin so every emitted asset carries its hash — SRI is the difference between "someone changed a file on our host" and "browsers refused to run it."
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
They Got Root on a Fully Patched Ivanti VPN and the Integrity Checker Said Everything Was Fine
Volexity caught an attacker chaining zero-days into unauthenticated root on an Ivanti Connect Secure appliance running the latest build. The payload never touched disk, commands rode in over the same HTTPS requests as the exploit, and Ivanti's Integrity Checker reported no modifications.
Gitea Shipped 21 CVEs in One Release and Eleven of Them Are the Same Bug
Gitea 28.3.0 landed November 1 with twenty-one CVE fixes. Read the list and a pattern jumps out: more than half are the same failure repeated across different route handlers, where a token scoped to public data gets handed private data anyway. That's not twenty-one mistakes.
php2shell: The PHP Bug That Fires Before Your Code Runs
Two critical PHP flaws landed October 21 and were in CISA's exploited-vulnerabilities catalog five days later. The 9.2 score isn't the part that should bother you. It's where the bug lives — it goes off while PHP is still assembling $_POST, before a single line of your application has run.
A Certificate Authority's Private Keys Were the Numbers 4, 56 and 66
Eight eMudhra intermediate CA keys turned out to be recoverable from their own public certificates. Not stolen — computable, because the key generation produced almost no entropy. A researcher pulled all eight in under a second and signed a message with each one to prove it.