Skip to content

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 -A

sha256, 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">, and link preloads/modulepreloads — each element carrying its own integrity. 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 sets integrity on the element), and anything your bundler emits without a plugin. Webpack's SubresourceIntegrityPlugin and 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 securitypolicyviolation report; 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.io served different bytes per browser fingerprint; an integrity attribute 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?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.