How to Add Subresource Integrity (SRI) to CDN Scripts

Your site loads JavaScript or CSS from a public CDN such as jsDelivr, cdnjs or unpkg without an `integrity` attribute, so the browser runs whatever the CDN returns. By the end of this guide, every flagged CDN file will be pinned to an exact version and carry an SRI hash plus `crossorigin="anonymous"`. Files that cannot be hashed will be self-hosted instead.

By Updated ⏱ 30–60 minutes🎯 Intermediate● Low impactSecurity
Illustration: Integrity checks on third-party scripts fix guide
SECURITY · FIX GUIDE Integrity checks on third-party s… IMPACTEFFORT TIME30–60 minutes LEVELIntermediate

Why it matters

If a public CDN or a package on it is compromised, any page that loads the file without SRI runs the attacker's code in your visitors' browsers. That code can skim form data, steal sessions or inject spam links. With an integrity hash, the browser compares the downloaded file to the hash you published and refuses to run it if they differ.

This is a low-impact check for rankings. It is a real supply-chain safeguard, and security scanners and procurement questionnaires often look for it.

✕Before<scriptsrc="https://cdn.jsdelivr.net/npm/swiper@latest/swiper-bundle.min.js"></script>✓After<scriptsrc="https://cdn.jsdelivr.net/npm/swiper@11.1.4/swiper-bundle.min.js"integrity="sha384-…"

What you need

Access to edit your site's HTML templates, theme files or custom code settings
The list of flagged CDN URLs from the audit report
A terminal with curl and openssl, or a browser to use srihash.org
Browser DevTools for testing

Step-by-step fix

1

List every CDN file missing an integrity attribute

Start with the URLs in the audit report. Each one is a script or stylesheet loaded from a public CDN without integrity. Then find where each URL is added: view the page source (Ctrl+U / Cmd+Option+U) and search for the CDN domain.

Also check the DevTools Network tab, filtered by domain, to catch files injected by plugins or tag managers. Note the file each tag lives in, such as a theme header, plugin setting or custom-code box.

Pro tip: Search your codebase for common CDN hosts: `grep -rnE "cdn.jsdelivr.net|cdnjs.cloudflare.com|unpkg.com|ajax.googleapis.com|code.jquery.com|stackpath.bootstrapcdn.com" ./`
2

Pin each URL to an exact version

SRI only works when the file never changes. A hash for a moving URL will break the moment the CDN serves a new version. Replace these with a full version number:

  • @latest
  • major-only tags like @5
  • unversioned paths

Then test the site with that exact version.

text
<!-- Breaks SRI: content changes over time -->
https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css

<!-- Safe to hash: immutable -->
https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css
Pro tip: jsDelivr and cdnjs list exact versions on each package page. unpkg redirects unversioned URLs to the latest release, so always copy the final versioned URL.
3

Generate a sha384 hash for each file

Download the exact file the browser will load and hash it with SHA-384, the algorithm most CDNs use. Run the command below once per URL and prefix the output with sha384-. Alternatively:

  • cdnjs.com has a Copy SRI option next to each file.
  • jsDelivr package pages show the SRI hash.
  • srihash.org generates the hash in the browser.
text
curl -sL "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" | openssl dgst -sha384 -binary | openssl base64 -A
Pro tip: Never hash a minified file you built yourself and then load a different copy from the CDN. Hash the exact URL in your tag.
4

Add integrity and crossorigin to each tag

Add the hash as integrity, plus crossorigin="anonymous", to every <script> and <link rel="stylesheet"> from a CDN. The crossorigin attribute makes the browser fetch the file in CORS mode, which SRI requires for cross-origin files. Without it, the browser blocks the file.

Major public CDNs already send Access-Control-Allow-Origin: *.

html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous" defer></script>
Pro tip: The hashes above are examples. Always generate your own from the exact URL you use.
5

Self-host files that change or cannot be hashed

Some CDN responses differ by visitor or change without a new URL, so a fixed hash will break them. For example, Google Fonts CSS varies by browser, and auto-updating loader scripts change in place. com/assets/`.

Files served from your own domain are not third-party CDN assets, so they no longer trigger the warning. Fonts can be self-hosted with tools like google-webfonts-helper or the @fontsource npm packages.

html
<!-- Instead of fonts.googleapis.com -->
<link rel="preload" href="/assets/fonts/inter-v13-latin-regular.woff2" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="/assets/css/fonts.css">
Pro tip: Self-hosting also removes a DNS lookup and connection to another host, which often helps load time.
6

Automate hashes in your build pipeline

If you bundle with webpack, Vite or a static site generator, generate SRI attributes at build time. Then hashes update automatically when you upgrade a library. Add a note to your update process: whenever a CDN version number changes, regenerate its hash in the same commit.

javascript
// webpack.config.js
const { SubresourceIntegrityPlugin } = require('webpack-subresource-integrity');
module.exports = {
  output: { crossOriginLoading: 'anonymous' },
  plugins: [new SubresourceIntegrityPlugin({ hashFuncNames: ['sha384'] })]
};
7

Test in the browser and re-run the audit

Load each affected page with DevTools open. Check that the Console shows no Failed to find a valid digest in the 'integrity' attribute errors. Confirm that sliders, menus and styles still work.

Then deliberately change one character in a hash on a staging copy and reload: the file should now be blocked, proving SRI is active. Finally, re-run the audit and confirm the CDN file count is zero.

Pro tip: Check pages that load plugin-specific assets, such as checkout, contact forms and blog posts, not just the homepage.

Platform-specific notes

WordPress

`wp_enqueue_script()` and `wp_enqueue_style()` have no integrity parameter. Add the attributes with the `script_loader_tag` and `style_loader_tag` filters in a child theme's functions.php or a code-snippets plugin, matching on the script handle. Plugins that load CDN files, such as page builders and slider plugins, often have a setting to load assets locally instead. Use it rather than editing plugin files, which are overwritten on update.

Shopify

Edit CDN tags in Online Store > Themes > … > Edit code, usually in `layout/theme.liquid` or a snippet. Files served from cdn.shopify.com are your store's own assets. Third-party app embeds inject their own scripts, which you cannot add SRI to. Ask the app developer, or consider whether you need the app.

Webflow

Add hashed tags in Site settings > Custom code (Head code / Footer code) or in Page settings > Custom code. Webflow's own hosted assets are outside your control. Focus on libraries you added yourself, like GSAP, Swiper or jQuery plugins.

Next.js

For third-party files, pass `integrity` and `crossOrigin="anonymous"` as props on the `<Script>` component from `next/script`, or on `<link>` tags. For your own bundles, `experimental.sri: { algorithm: 'sha384' }` in next.config.js adds hashes. It is webpack-only and works best with static pages, so test it before relying on it.

Cloudflare

cdnjs.cloudflare.com provides a Copy SRI option for every file. If you use Rocket Loader or other HTML-rewriting features, confirm they preserve the `integrity` and `crossorigin` attributes. If a feature rewrites or combines scripts, exclude those scripts or turn the feature off.

Check that it worked

✅ How to verify

  • View the page source and confirm every script or stylesheet from a public CDN has both `integrity="sha384-…"` and `crossorigin="anonymous"`.
  • Open DevTools > Console on each affected page and confirm there are no integrity or CORS errors and no blocked resources in the Network tab.
  • On staging, alter one character of a hash and confirm the browser blocks that file, then restore it.
  • Re-run the audit and confirm it reports that all CDN scripts and styles use integrity checks.

⚠️ Common mistakes

  • Hashing a URL that uses `@latest`, a major-only version or no version. The file changes, the hash stops matching and the library silently stops loading.
  • Adding `integrity` without `crossorigin="anonymous"`. The browser then blocks the cross-origin file.
  • Adding SRI to Google Fonts CSS or self-updating vendor loaders, whose responses vary or change. Self-host these instead.
  • Upgrading a library version in one place but forgetting to regenerate its hash, which breaks the feature after deployment.

Is this still an issue on your site? Run a free audit — ZORO checks this and 294 other SEO, AI-visibility, accessibility and security signals, with fixes for your platform.

Check my site free

FAQ

What does the integrity attribute do on a script tag?

It holds a cryptographic hash of the expected file, such as `sha384-…`. The browser hashes the downloaded file and only runs or applies it if the hashes match. If the CDN copy has been tampered with, the browser blocks it.

Do I need crossorigin="anonymous" with SRI?

Yes, for files on another domain. SRI checks on cross-origin resources require a CORS request. `crossorigin="anonymous"` makes that request without sending cookies, and public CDNs respond with the required `Access-Control-Allow-Origin` header.

Can I add SRI to Google Analytics or Google Tag Manager?

Not reliably. These scripts are updated in place at the same URL, so any hash would break when the vendor updates them. Load them as the vendor documents. For CDN-hosted libraries you control, pin a version and add SRI, or self-host.

Will adding SRI slow down my site?

No measurable amount. The browser hashes the file it has already downloaded, which takes only a moment. The main risk is a broken feature if the hash is wrong, so test after every change.