Article
Subresource Integrity (SRI): what one hash attribute actually protects
Published 2026-11-27
Subresource Integrity (SRI): what one hash attribute actually protects
Every third-party script tag on your page is someone else's code, loaded from a server you do not control, and changeable without telling you. If your page loads jQuery from a CDN, analytics from a vendor, or a chat widget from a supplier, the bytes your users execute are not the bytes you reviewed when you picked the library. They are whatever is on that URL today.
Most days that is fine. The interesting day is the one the UK Information Commissioner's Office wrote about in its 2020 penalty notice against Ticketmaster. Attackers got into Inbenta, a company whose JavaScript chatbot Ticketmaster embedded on its payment page, and modified the hosted script so it scraped the payment form: names, card numbers, expiry dates, CVVs. Banks started reporting fraudulent cards in April 2018, and per the ICO it took Ticketmaster around nine weeks from Monzo's first notification to actually run a test payment and watch the network traffic. The chatbot script itself was never in Ticketmaster's control, and nobody on Ticketmaster's side had pinned what its contents should be. The fine was £1.25 million.
The notice is also explicit that technical measures in line with the state of the art, "such as SRI", could have significantly reduced the likelihood of a successful compromise. When a data protection regulator names a one-line HTML attribute in a legal document, it is worth knowing what that attribute is. This article covers what it does, how to generate the values it needs, and where it cannot help you.
What the integrity attribute actually does
Subresource Integrity is a browser feature. You put a cryptographic hash of the expected file contents next to the URL:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"
integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="
crossorigin="anonymous"></script>
Before the browser executes that script, it hashes the downloaded file and compares the result against the value in the attribute. If they match, execution continues. If they do not match, the browser refuses the resource and reports a network error, the same error it reports when the CDN is down. The script simply does not run. No prompt, no fallback load, no second try. That all-or-nothing behavior is the design: a modified file is treated as a broken file, because from your page's perspective it is worse than broken.
The attribute accepts sha256, sha384, and sha512 values, and you can list several. One practical note from the MDN documentation: if you provide hashes with different algorithms, the browser only uses the strongest one present, so mixing sha256 and sha384 means the sha256 entry is decoration. One hash from one algorithm is enough.
There is a second attribute in that snippet and it is not optional. The crossorigin attribute forces the request into CORS mode, and per the spec a request with an integrity attribute in no-cors mode always fails. The reason is subtle: in no-cors mode a page cannot read the response, but an attacker could still measure whether a resource matching a given hash loaded or errored, which leaks information. Browsers closed that hole by requiring CORS for SRI, which means the CDN must send Access-Control-Allow-Origin. The big public CDNs do; I checked both cdnjs and jsDelivr while writing this and both returned Access-Control-Allow-Origin: *. A vendor server that does not send CORS headers cannot be protected with SRI at all, which is worth knowing before you promise anyone coverage.
SRI applies to more than scripts: the MDN reference lists <script> plus <link> elements with rel of stylesheet, preload, and modulepreload. Anything else, images and fonts included, has no integrity mechanism today. Third-party iframes have their own sandbox problem and SRI does nothing there either.
Generating a hash you can trust
The hash must be of the exact file at the exact URL you are pinning. Generate it from the file itself, not from a copy-pasted snippet on a blog, including this one. The OpenSSL pipeline on any Linux box or Git Bash on Windows:
curl -sO https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js
openssl dgst -sha512 -binary jquery.min.js | openssl base64 -A
Output for jQuery 3.7.1 is the value in the snippet above, and it matches the SRI value the cdnjs API publishes for that file, which is the cross-check you want: your pipeline and the library's own published hash agree. On macOS, shasum works too, and MDN documents the awk-and-xxd incantation needed because shasum echoes the filename into the pipe. If you do not want a terminal at all, the srihash.org generator run by the SRI spec authors does the same job in a browser.
Two ways this goes wrong in real repos:
Hash the wrong URL. If you pin the minified file but load the unminified one, or hash 3.7.0 while the src says 3.7.1, every page load fails and the error looks like CDN downtime. It is not downtime; the console will say the integrity check failed, and the fix is one diff away.
Hash an unpinned URL. A hash next to /jquery/latest.js breaks the moment the CDN rotates the file, which is the correct behavior but a nasty surprise on a Friday. The rule is that SRI and floating versions are incompatible by design. If you want automatic updates from a CDN, you have chosen the thing SRI exists to prevent, and you should pick one or the other honestly. In practice this is where self-hosting wins an argument people usually frame as performance versus control: when the file lives in your own repository, its integrity is your build system's problem, and a hash in your HTML is redundant. I self-host when I can and pin hard when I cannot.
The day-to-day failure mode
Because a failed integrity check kills the resource, a stale hash does not degrade gracefully. It is a hard break: your styles disappear or your JS is dead, on every page, for every user, until someone updates the attribute. This is the cost of the guarantee. Any release process that bumps third-party library versions needs to regenerate integrity values in the same commit, and code review needs to treat a version bump without a matching hash change as a bug. If your CI can render the page, a smoke check that each script with an integrity attribute actually loads is cheap insurance against the quiet combination: someone upgrades the library, forgets the hash, and staging is fine because the file got pinned there correctly and production is not.
The other quiet failure is no failure at all. SRI only compares against the URL you named. If an attacker can modify your HTML, they can remove the integrity attribute or point the src somewhere else, which is why the Mozilla team that shipped SRI noted it protects against a compromised third party, not against a compromised origin. If your own server or the page-rendering layer is attacker-controlled, the hash protects nothing. SRI sits on top of your own security; it does not replace it.
On plain HTTP the protection is thinner still: anyone in the network path can rewrite the HTML in transit and strip the attribute before the browser ever sees it. That is not a reason to skip SRI, but it is a reason to close the mixed-content hole first, otherwise you are bolting a lock onto a door standing in an open doorway.
What about requiring SRI everywhere?
You may run into the require-sri-for Content Security Policy directive in older guides. Do not build on it. It never shipped in a stable browser, Chrome only ever ran it behind an experimental flag, and it was removed from the CSP specification in September 2022. The direction the platform has taken instead is the Integrity-Policy header, which lets a document refuse to load scripts and styles that lack integrity metadata, with a report-only mode for measuring violations before enforcing. It is specified on MDN with a structured-field syntax for blocked destinations and reporting endpoints. Support in shipping browsers is thin, so treat it as a forward-looking header to watch rather than something that covers your users today. What you can already enforce, and what I would set first if your scripts are self-hosted, is a CSP that whitelists where script can come from; the article on this site about Content Security Policy covers that without the folklore.
Questions people ask
What is Subresource Integrity in simple terms?
It is an HTML attribute holding a cryptographic hash of a script or stylesheet file, so the browser only runs the file if its contents match that hash, refusing it with a network error if anything changed.
Does SRI slow down my site?
No measurable performance cost, since the browser hashes a file it already downloaded before executing. The hash computation on a few hundred kilobytes of JavaScript is negligible on any device that can run the JavaScript itself.
Can I use SRI with a third-party script that updates itself?
No, and that combination is a hard outage waiting to happen. SRI pins exact bytes, so the first time the vendor legitimately changes the file, every visitor's browser will refuse to run it.
Does SRI protect against an attacker modifying my own HTML?
No. If someone can change your page, they can delete the integrity attribute or change the src, so SRI protects against a compromised CDN or vendor, not a compromised origin.
SRI is markup-level protection, so the scan on this site does not grade it; what the scan does see is the layer underneath it, where these problems usually travel together. If you are pinning third-party scripts because a supplier got burned once, the free CheckWebSecurity scan will show you the headers and TLS posture of that same perimeter, including whether Content-Security-Policy is even set. Run a scan and see the parts of the page that load before any script does.
Related: Content Security Policy explained in plain terms · Mixed content: what it is, why it breaks, and how to find every instance on your site · How to read a security headers report