← Back to Frontend, page 2

frontend-0036 · Medium mid

What is Subresource Integrity, and how do you protect a page from a tampered third-party script?

Subresource Integrity (SRI) adds a hash of the expected file to a script or link tag. The browser downloads the file, hashes it and refuses to run it if the hash does not match, so a compromised CDN cannot silently change the code. It fits files that never change at their URL.

Full answer

A page that loads a library from a CDN trusts that CDN completely. If the CDN or the package account is compromised, the attacker's code runs in your origin, with the same power as your own code.

SRI limits this. You add an integrity attribute with the algorithm and the base64 digest of the exact file:

<script src="https://cdn.example.com/lib.js" integrity="sha384-..." crossorigin="anonymous"></script>

The browser fetches the file, computes the hash and runs it only if it matches. A mismatch blocks the script and writes an error to the console. For files on another origin, the server must allow CORS, and the tag needs crossorigin="anonymous", otherwise the browser cannot check the bytes.

How to create the value: hash the file with SHA-256, SHA-384 or SHA-512 and encode the digest with base64. Tools such as openssl dgst -sha384 -binary lib.js | openssl base64 -A or a few lines of Node do it. Build tools can add the attribute to the output HTML automatically.

Limits:

  • The URL must serve identical bytes. Any URL that changes without notice, such as latest or a tag manager snippet, will break. Pin a version.
  • It checks only the file, not what the file loads next.
  • It does not protect against code you already trust that turns hostile in a new release. Review updates and pin versions.
  • A page with a strict CSP can also require integrity metadata for scripts.

Other supply chain steps: lock files and reviewed dependency updates, npm audit in CI, the fewest third-party scripts that the business allows, and loading the rest in a sandboxed iframe or a worker.

The Node script computes the attribute for a small file. The test serves the file and runs the tag in Chrome. The script runs when the hash matches. When the served file is changed by one character, Chrome blocks it.

javascript
import { createHash } from 'node:crypto';

const file = 'window.libLoaded = true;\n';
const digest = createHash('sha384').update(file).digest('base64');
console.log(`<script src="/lib.js" integrity="sha384-${digest}" crossorigin="anonymous"></script>`);

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The Node script printed the tag, and the test used it in Chrome. A file with the matching hash ran, and the same file with one extra comment was blocked with an integrity error.

Key points

  • SRI pins a script or style to a hash, and the browser refuses mismatches.
  • Cross-origin files need crossorigin anonymous and CORS headers.
  • Pin an exact version, because SRI breaks on files that change.
  • It protects the file, not what the file loads.
  • Combine with lock files, review and fewer third-party scripts.

Common mistakes

  • Adding integrity to a URL that serves different bytes over time.
  • Forgetting crossorigin for a cross-origin file.
  • Thinking SRI protects against a malicious new package version that you chose to install.
  • Computing the hash from a different copy of the file than the one served.

Follow-ups

  • Which hash algorithms can integrity use?
  • What happens to the page when the hash fails?
  • How would you handle a tag manager that you cannot pin?

Tips

  • Show the attribute and say what the browser does with it.
  • Mention the CORS requirement, since it trips people.