NetSecTools

Home / Guides / Subresource Integrity (SRI): How to Protect CDN Scripts and Stylesheets

Subresource Integrity (SRI): How to Protect CDN Scripts and Stylesheets

· 7 min read

Loading JavaScript libraries or stylesheets from a public CDN saves bandwidth and build steps, but it introduces a major supply-chain risk: if an attacker compromises the CDN, hijacks its DNS or tampers with a versioned file on the origin, every visitor to your site executes the attacker’s code with full access to your DOM, cookies and session tokens.

Subresource Integrity (SRI) eliminates that blind trust. By placing a cryptographic hash of the expected file inside the HTML <script> or <link> tag, you instruct the browser to fetch the asset, hash its bytes locally and refuse to execute or apply it if even a single byte changed.

You can generate and verify SRI digests locally without network requests using our SRI Hash Generator & Verifier.

How SRI stops CDN tampering

When a browser parses an element with an integrity attribute, it performs three steps before running the code:

  1. Downloads the resource according to its CORS policy.
  2. Computes the digest using the strongest supported SHA-2 algorithm listed in integrity.
  3. Compares the Base64-encoded hash against the expected value. If it does not match, the browser discards the file and fires a network error instead of running tampered JavaScript or injecting rogue CSS.

Choosing SHA-384 vs. SHA-256 and SHA-512

The W3C Subresource Integrity specification supports three hash prefixes: sha256-, sha384- and sha512-.

  • sha384 (recommended default): Offers a wide security margin (192-bit collision resistance), fits comfortably in HTML attributes and is the standard used by most CDN libraries.
  • sha256: Valid and slightly shorter, commonly used when tooling standardizes on SHA-256 everywhere.
  • sha512: Longest string, useful when policy mandates maximum digest length.

How browsers evaluate multiple hashes

You can provide multiple space-separated hashes in one integrity attribute (for example, integrity="sha256-... sha384-..."). Under the W3C algorithm selection rule, the browser identifies the strongest hash function it recognizes (sha512 > sha384 > sha256) and validates only the hashes matching that strongest algorithm. Listing a weaker sha256 hash alongside sha384 does not weaken verification in modern browsers—they will still enforce sha384 and fall back to sha256 only on runtimes that do not support sha384.

Why crossorigin=“anonymous” and CORS are mandatory

For any resource loaded from a different origin (such as cdn.example.com while your site runs on app.example.com), SRI requires crossorigin="anonymous":

<script
  src="https://cdn.example.com/lib/v1.4.2/app.min.js"
  integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
  crossorigin="anonymous"
></script>

Without crossorigin="anonymous", cross-origin fetches return an opaque response whose bytes the browser is not allowed to expose to integrity checks, causing SRI validation to fail and blocking the script. On the CDN side, the server must respond with Access-Control-Allow-Origin: * (or your exact origin). If the CDN omits CORS headers, either self-host the file or choose a CDN that supports CORS cleanly.

Ready-to-copy script and stylesheet snippets

Use the same integrity and crossorigin="anonymous" pattern for both scripts and external stylesheets:

<!-- External JavaScript with SRI -->
<script
  src="https://cdn.example.com/app.min.js"
  integrity="sha384-REPLACE_WITH_BASE64_SHA384_DIGEST"
  crossorigin="anonymous"
></script>

<!-- External CSS with SRI -->
<link
  rel="stylesheet"
  href="https://cdn.example.com/styles.min.css"
  integrity="sha384-REPLACE_WITH_BASE64_SHA384_DIGEST"
  crossorigin="anonymous"
/>

Important deployment caveats:

  • Pin immutable versioned URLs: Never attach SRI to a mutable “latest” URL such as /v1/lib.min.js that receives silent patch updates, or your site will break the moment the upstream file changes.
  • Avoid on-the-fly CDN minifiers or UA-varying builds: If a CDN serves different bytes based on User-Agent or compression differences, the digest will fail for some visitors.

If you prefer generating the hash on the command line:

openssl dgst -sha384 -binary app.min.js | openssl base64 -A

Pairing SRI with a strict Content Security Policy

SRI and Content Security Policy (CSP) reinforce each other:

  • CSP restricts where assets may load from: Pin exact CDN origins in script-src and style-src rather than wildcards (* or https:), and avoid 'unsafe-inline'.
  • SRI guarantees what those origins actually serve: Even if an allowlisted third-party CDN origin is breached, the browser rejects any modified file whose hash does not match your HTML.

Audit your CSP header with our CSP Analyzer & Builder, verify all HTTP response headers with the HTTP Security Headers Checker, and compute or verify exact sha256/sha384/sha512 tags offline with our SRI Hash Generator & Verifier.