How to Add Subresource Integrity to CDN Scripts (2026)

In June 2024, Sansec reported that cdn.polyfill.io had started serving malware to visitors of more than 100,000 websites. A company had bought the domain and its GitHub account in February, and from then on every site with a <script src="https://cdn.polyfill.io/..."> tag was running whatever the new owner chose to send. Days later Sansec added more domains tied to the same actor. Two of them, bootcdn.net and staticfile.org, sit on the CDN list our scanner checks today.

That's the risk subresource integrity (SRI) covers. You put a hash of the exact file you approved in the tag, and the browser refuses to run anything else.

TL;DR

Pin every CDN URL to an exact version, generate a SHA-384 hash of that file, and add integrity="sha384-..." plus crossorigin="anonymous" to the tag. Then load the page and check the console, because a wrong hash, a missing crossorigin, and a CDN without CORS each block the file silently from the user's point of view. Google Fonts and Tailwind's Play CDN can't carry a stable hash, so self-host those.

What the finding means

CheckYourVibe reads your homepage HTML and looks at every <script src> and <link rel="stylesheet"> that points at another domain. If one of them has no integrity attribute, you get a finding:

  • CDN Resources Missing Subresource Integrity (medium) when the file comes from a public CDN on our list: jsDelivr, cdnjs, unpkg, Google's hosted libraries, code.jquery.com, the Bootstrap CDNs, Google Fonts, Font Awesome, Tailwind's CDN and a few others.
  • External Resources Missing SRI (low) for other third-party files, only when no CDN file was flagged.

One honest limit: the check looks for the attribute, not whether the hash inside it is right. A broken hash passes our scan and fails in the browser. That's why step 4 below matters.

CDN tags turn up most in single-file prototypes, because pasting one is the fastest way to get a library onto a page without a build step. If your app has a bundler, the cleaner fix is often to npm install the library and drop the tag entirely. Bundled code is served from your own domain and doesn't need a hash.

Add SRI in four steps

1

Pin the URL to an exact version

A hash describes one file. If the URL can resolve to a newer file, the hash breaks on the next release. So https://cdn.jsdelivr.net/npm/alpinejs/dist/cdn.min.js (latest) has to become https://cdn.jsdelivr.net/npm/alpinejs@3.14.1/dist/cdn.min.js.

You can see the difference in jsDelivr's own headers. The pinned URL comes back with cache-control: public, max-age=31536000, immutable. The unpinned one says max-age=604800, s-maxage=43200, which is jsDelivr telling you it may serve something different in 12 hours.

2

Generate the hash

Download the exact file and hash it. This runs anywhere with curl and openssl:

Generate a SHA-384 SRI hash
curl -s https://cdn.jsdelivr.net/npm/alpinejs@3.14.1/dist/cdn.min.js \
  | openssl dgst -sha384 -binary | openssl base64 -A
# l8f0VcPi/M1iHPv8egOnY/15TDwqgbOR1anMIJWvU6nLRgZVLTLSaNqi/TOoT5Fh

We ran it twice and got the same value both times, which is the point of pinning. If you'd rather not touch a terminal, srihash.org does the same thing in a browser, and jsDelivr and cdnjs both offer a copy button that includes the hash.

3

Add integrity and crossorigin to the tag

Before and after
<!-- Before: flagged -->
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs/dist/cdn.min.js"></script>

<!-- After -->
<script defer
  src="https://cdn.jsdelivr.net/npm/alpinejs@3.14.1/dist/cdn.min.js"
  integrity="sha384-l8f0VcPi/M1iHPv8egOnY/15TDwqgbOR1anMIJWvU6nLRgZVLTLSaNqi/TOoT5Fh"
  crossorigin="anonymous"></script>

Stylesheets work the same way: <link rel="stylesheet" href="..." integrity="sha384-..." crossorigin="anonymous">.

Don't skip crossorigin. It looks optional and it isn't, as the next step shows.

4

Load the page and read the console

Every SRI failure looks the same to a visitor: the library just doesn't load. The console is the only place that tells you which mistake you made.

What each mistake looks like in the browser

We set up a page on one local origin loading a script from a second origin, then broke it on purpose four ways in Chromium 153. Here's what ran and what the console said:

Tag setupScript ran?Console error
Correct hash + crossorigin="anonymous"YesNone
Wrong hashNoFailed to find a valid digest in the 'integrity' attribute for resource '...' with computed SHA-384 integrity '...'. The resource has been blocked.
Correct hash, no crossoriginNoSubresource 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.
Correct hash + crossorigin, server sends no CORS headerNoAccess to script at '...' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present

Two useful things in there. The wrong-hash error prints the hash the browser actually computed, so if you're certain the file is the one you meant, that's your corrected value. And the third row is the most common hand-edit mistake: someone pastes in integrity and leaves crossorigin behind.

The fourth row is why you can't add crossorigin to every third-party tag on autopilot. The big public CDNs send access-control-allow-origin: * (jsDelivr did on the file above), but a vendor's own script host might not. If it doesn't, adding crossorigin breaks the script whether or not you add a hash.

A hash only protects the version you approved

SRI stops a file from changing. It does nothing if the version you pinned was compromised before you hashed it. Hash a release you've actually used, not one published an hour ago.

Three CDNs a hash can't pin

Some of the domains on our list serve files that change by design. Adding a hash to these breaks your site for some or all visitors.

Google Fonts

The stylesheet at fonts.googleapis.com/css2?family=... is generated per browser. We requested the same Inter stylesheet three times with different user agents:

User agentSizeFont format
Chrome 130 on Windows4,942 byteswoff2
Safari on iOS 125,264 byteswoff2
curl458 bytestruetype

Three requests, three different SHA-384 hashes. Whichever one you pin, some visitors get a blocked stylesheet. The fix is to self-host: download the font files and serve them from your domain. In Next.js, next/font/google does this at build time. Elsewhere, the @fontsource npm packages give you the same fonts as installable files.

Tailwind's Play CDN

cdn.tailwindcss.com answers with a 302 redirect to the latest release (it pointed at /3.4.17 when we checked) and a four-hour cache. Any hash you take today breaks on the next release. Tailwind's docs are blunt about it anyway: "The Play CDN is designed for development purposes only, and is not intended for production." Install Tailwind through your build tool and the tag disappears.

polyfill.io-style dynamic services

Sansec noted that polyfill.io's code was "dynamically generated based on the HTTP headers" of each request. A service that builds a different response per browser can't be hashed at all. If a script's whole job is to tailor itself to the visitor, self-host a fixed version or drop it. Most polyfills haven't been needed by current browsers for years.

Font Awesome kits from kit.fontawesome.com fall in the same bucket for practical purposes. They're a loader tied to your account rather than a fixed file. The free icon set is also published as @fortawesome/fontawesome-free on npm, which you can bundle.

Have your AI editor do it

If your app has more than a couple of CDN tags, this prompt gets you most of the way. Check the result with step 4 regardless.

AI Prompt

Add SRI to CDN tags

Find every script and stylesheet tag in this project that loads from another domain. For each one:

  1. If the library is on npm and this project has a bundler, replace the tag with an npm install and an import instead.
  2. Otherwise pin the URL to the exact version currently in use, and add integrity="sha384-..." with the correct hash for that exact file, plus crossorigin="anonymous".
  3. Do not add an integrity hash to Google Fonts, cdn.tailwindcss.com, or kit.fontawesome.com. List those separately and suggest a self-hosted replacement. Show me the command you used to generate each hash.

The last line matters. AI editors will happily invent a plausible-looking sha384- string, and a made-up hash gives you the second row of the table above.

SRI pairs well with a Content Security Policy. SRI pins which file runs from a CDN; a CSP script-src pins which domains can serve scripts at all. Our CSP setup guide covers the second half.

What does subresource integrity actually protect against?

A file on someone else's server changing after you linked it. The integrity attribute holds a hash of the exact file you approved, and the browser refuses to run anything that doesn't match. It doesn't stop a CDN from going down, and it doesn't help if the version you pinned was already malicious.

Do I need crossorigin anonymous with the integrity attribute?

Yes, for any file on another domain. Without it Chromium blocks the file with the message that the resource requires the request to be CORS enabled to check the integrity. The CDN also has to send an Access-Control-Allow-Origin header, which the big public CDNs do.

Why did my script stop loading after I added an integrity hash?

Usually one of three things. The hash is wrong or for a different version, the URL isn't pinned so the file changed under you, or crossorigin="anonymous" is missing. Open the browser console: each case logs a different error that names the problem.

Can I add SRI to Google Fonts?

Not reliably. Google Fonts returns different CSS depending on the browser asking for it. We requested the same stylesheet with three user agents and got three different SHA-384 hashes, so any single hash will block the font for some visitors. Self-host the font files instead.

Do I need SRI on scripts served from my own domain?

No. SRI is for files you don't control. Scripts your own build produces and serves from your domain aren't flagged, and if someone can change those, they can change the HTML holding the hash too.

Find Every Unhashed CDN Tag

CheckYourVibe scans your live site and lists each third-party script and stylesheet loaded without an integrity hash.

How-To Guides

How to Add Subresource Integrity to CDN Scripts (2026)