Subresource Integrity lets you pin a script or stylesheet to a cryptographic hash: <script src="…" integrity="sha384-…" crossorigin="anonymous">. If the CDN serves different bytes, whether compromised or just updated, the browser refuses to run it. SRI is ideal for versioned, immutable files on a public CDN. It doesn't work for "evergreen" vendor tags (analytics, chat, A/B testing) whose content changes without notice. Those need self-hosting, sandboxing, CSP and vendor governance instead.
<script
src="https://cdn.example.net/lib@4.2.1/lib.min.js"
integrity="sha384-BASE64_DIGEST_OF_THE_EXACT_FILE_BYTES"
crossorigin="anonymous"></script>
integrity value (SHA-256, -384 or -512). You can list several hashes to allow a transition between versions.crossorigin is required for cross-origin resources. The response must be CORS-enabled (Access-Control-Allow-Origin), otherwise the check fails and the script is blocked.openssl dgst -sha384 -binary lib.min.js | openssl base64 -A), or let your bundler's SRI plugin add it to emitted tags.<script> and <link rel="stylesheet"> and to preloads. For module graphs, an import map integrity field is supported in recent Chromium and Safari versions, so check support.require-sri-for CSP directive was never shipped widely. Don't rely on it.| Strategy | Effect |
|---|---|
| Self-host pinned versions | You control the bytes, and SRI becomes unnecessary for same-origin files |
| Inventory and ownership | Every tag has an owner, purpose, data access review and removal date |
CSP with strict-dynamic and tight connect-src | Limits where injected code can send data |
| Isolate in a sandboxed iframe on another origin | The vendor can't read your DOM, cookies or form fields |
| Web Worker sandboxes (e.g. Partytown-style approaches) | Moves tag execution off the main thread; isolation is limited since the DOM is proxied |
| Keep sensitive pages clean | No marketing tags on checkout, login or account settings pages |
| Tag manager governance | Restrict who can publish custom HTML tags, with review and audit logs |
| Runtime monitoring | CSP violation reports, client-side script-change detection |
PCI DSS v4 now explicitly requires an inventory and integrity verification of scripts on payment pages, which pushes many teams to formalise this.
/latest/), so the site breaks on the vendor's next deploy.crossorigin, or a CDN without CORS headers, which silently blocks the script.crossorigin requirement and where it fits (immutable, versioned assets).Spotted a mistake or a rights concern? Report an issue