Subresource Integrity (SRI)

Difficulty: Intermediate

Question

What is Subresource Integrity (SRI), how does the browser enforce it, and when should you use it for scripts loaded from a CDN?

Answer

Subresource Integrity is a browser feature that lets you pin the exact contents of a script or stylesheet you load from somewhere else, usually a CDN. You add an integrity attribute containing a cryptographic hash of the file; the browser hashes what it downloads and refuses to execute or apply it if the hashes do not match. This protects you if the CDN, or the domain serving the file, is compromised and starts serving modified code.

How it works

  1. At build time you compute a hash of the exact file, typically SHA-384, and base64-encode it.
  2. You add it to the tag: integrity="sha384-<base64 hash>" along with crossorigin="anonymous".
  3. The browser downloads the file, computes the same hash and compares.
  4. On a match the script runs. On a mismatch the browser treats it as a network error, does not execute it, and logs a console error.

Supported algorithms are sha256, sha384 and sha512. You can list several hashes separated by spaces, and the browser uses the strongest algorithm it supports.

Why crossorigin is required

For cross-origin resources the browser can only check integrity if the response is CORS-enabled. Without CORS the response would be opaque and its bytes not inspectable by the page, so the browser blocks it instead. The CDN must send Access-Control-Allow-Origin, which all major public CDNs (jsDelivr, cdnjs, unpkg) do.

The threat it addresses

Loading third-party JavaScript means trusting whoever controls that server forever. A real example: in 2024 the polyfill.io domain changed hands and began serving malicious code to sites that embedded it, affecting a very large number of websites. Any site that had pinned a specific file with SRI would have had the modified response blocked rather than executed. More generally SRI protects against:

  • A compromised CDN account or origin server.
  • A DNS or domain takeover of the script host.
  • A man-in-the-middle on a misconfigured connection (although HTTPS already covers most of this).

Limitations and trade-offs

  • Only works for static, versioned files. If the URL is /latest/lib.js or the server varies content by user agent, the hash will break on the next change. Always pin an exact version.
  • No automatic updates. Upgrading a library means updating the hash. This is a feature: you choose when code changes.
  • Covers <script> and <link rel="stylesheet"> (plus <link rel="preload"/"modulepreload">). It does not apply to images, iframes or code that a script loads later itself.
  • Not a substitute for vetting the library. If the pinned version is itself malicious, SRI happily allows it.
  • Self-hosting third-party code from your own origin removes the CDN dependency entirely, which many teams now prefer.

Using it in practice

  • Bundlers can generate hashes automatically (for example webpack-subresource-integrity or Vite plugins).
  • Pair SRI with a strict CSP; CSP limits where scripts can come from, SRI pins what they contain.
  • For payment pages, PCI DSS v4.0 requirement 6.4.3 asks you to inventory and authorize every script and ensure its integrity, and SRI is one common way to meet it.
How the browser enforces SRI. The script only runs if its hash matches the one in the HTML.
  1. Build: hash the exact file: Compute sha384 and put it in the integrity attribute.
  2. Browser fetches with CORS: crossorigin=anonymous makes the bytes inspectable.
  3. Browser hashes the response: Uses the strongest listed algorithm it supports.
  4. Compare hashes: Match means run; mismatch means blocked as a network error.

Code examples

Generate an SRI hash and use it

# Download the exact pinned version
curl -sL https://cdn.jsdelivr.net/npm/dayjs@1.11.13/dayjs.min.js -o dayjs.min.js

# Compute base64-encoded SHA-384 digest
echo "sha384-$(openssl dgst -sha384 -binary dayjs.min.js | openssl base64 -A)"

Always hash the exact versioned file you will reference. The output goes straight into the integrity attribute.

Script tag with integrity and crossorigin

<!-- Pinned version, hash, and CORS mode -->
<script
  src="https://cdn.jsdelivr.net/npm/dayjs@1.11.13/dayjs.min.js"
  integrity="sha384-REPLACE_WITH_GENERATED_HASH"
  crossorigin="anonymous"
  defer></script>

If the CDN ever serves different bytes for this URL, the browser refuses to run it and logs an integrity error.

Key points

  • SRI pins the content of a third-party script or stylesheet with a sha256/384/512 hash.
  • Cross-origin SRI requires crossorigin=anonymous and a CORS-enabled response.
  • Only use it with exact, versioned URLs; never with 'latest' or user-agent dependent content.
  • A mismatch blocks the resource entirely, so the page must cope with it failing.
  • Combine with a strict CSP; SRI checks what a file contains, CSP checks where it comes from.

Common follow-up questions

  • What happens to the page if an SRI check fails, and how would you handle that gracefully?
  • Why can't you use SRI with Google Fonts CSS?
  • How does SRI complement Content Security Policy?
  • Would you rather self-host third-party scripts or use SRI with a CDN? Why?

Interview tips

  • Explain the threat model: a compromised CDN or domain, i.e. a supply chain attack.
  • Mention the crossorigin requirement; many candidates forget it.
  • Call out the limitation with dynamic or unversioned resources to show practical experience.

Concepts covered

Subresource Integrity, integrity attribute, SHA-384, CDN Compromise, Supply Chain Security, CORS

More Web Security (OWASP, JWT, XSS, CSRF) questions