Modern web applications rely heavily on external Content Delivery Networks (CDNs) to serve popular open-source libraries like jQuery, React, Bootstrap, fonts, and polyfills. While CDNs improve download speeds and caching, they introduce a serious supply-chain risk: if the CDN server or domain is compromised, an attacker can modify the script to steal session tokens, capture credit cards, or inject malware into every visitor's browser.
The 2024 polyfill.io domain supply-chain attack demonstrated exactly this risk: hundreds of thousands of websites suddenly served malicious redirects after an attacker acquired the CDN domain.
Subresource Integrity (SRI) is a browser security mechanism that eliminates this vulnerability. It ensures that external scripts and stylesheets are executed only if their exact cryptographic content matches what you intended.
How Subresource Integrity works
When you include a script or stylesheet with an SRI attribute, you provide a cryptographic hash of the expected file content directly in the HTML tag:
<script
src="https://cdn.example.com/npm/framework@1.2.3/bundle.min.js"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
crossorigin="anonymous">
</script>When the browser downloads the file from cdn.example.com, it performs the following steps before executing the code:
- Computes the cryptographic hash of the received response body (using the algorithm specified in the prefix, like
sha384-). - Compares the computed hash against the value in your
integrityattribute. - If the hashes match, the script executes normally.
- If the hashes do not match (meaning the file was altered, truncated, or tampered with), the browser immediately discards the file, refuses execution, and outputs a security error in the console:
Failed to find a valid digest in the 'integrity' attribute for resource 'https://cdn.example.com/...' with computed SHA-384 integrity '...'. The resource has been blocked.Why crossorigin="anonymous" is required
Whenever you use integrity on a resource hosted on a different origin (domain), you must also include the crossorigin="anonymous" attribute:
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">By default, cross-origin resource requests are subject to the browser's Same-Origin Policy and CORS rules. Without crossorigin="anonymous":
- The browser sends a basic request without CORS headers.
- To prevent sensitive data leaks across origins, the browser hides the byte-level response body from the calling document.
- Because the browser cannot inspect the raw bytes without CORS, it cannot compute the integrity digest and will block the resource outright.
Adding crossorigin="anonymous" requests the file using CORS mode without sending user cookies or authentication credentials. The CDN must respond with an Access-Control-Allow-Origin: * header for the browser to verify the hash.
Choosing a hash algorithm: SHA-256 vs SHA-384 vs SHA-512
SRI supports three cryptographic hash algorithms:
| Prefix | Algorithm | Output Hash Length | Recommendation |
|---|---|---|---|
sha256- | SHA-256 | 32 bytes (44 Base64 chars) | Acceptable, fast on 32-bit hardware |
sha384- | SHA-384 | 48 bytes (64 Base64 chars) | W3C Recommended standard for SRI |
sha512- | SHA-512 | 64 bytes (88 Base64 chars) | High collision resistance, longer string |
The W3C specification recommends SHA-384 as the sweet spot between digest length, performance on 64-bit architectures, and long-term cryptographic collision resistance.
You can also specify multiple hashes separated by a space (for example, if providing backward-compatible hashes). The browser will always select and enforce the strongest algorithm specified.
How to generate an SRI hash
In the terminal with OpenSSL and cURL
You can calculate an SRI hash for any remote URL directly from your command line:
curl -s https://cdn.example.com/library.js | openssl dgst -sha384 -binary | openssl base64 -APrefix the output with sha384- and attach it to your <script> or <link> tag.
To hash a local file before deployment:
cat dist/bundle.js | openssl dgst -sha384 -binary | openssl base64 -AIn your browser
Instead of remembering OpenSSL pipe arguments, you can paste the file or URL into ToolDock's free SRI Hash Generator. It automatically fetches, validates CORS support, computes SHA-256, SHA-384, and SHA-512 digests, and formats ready-to-paste <script> and <link> HTML tags.
SRI with Content Security Policy (CSP)
SRI and Content Security Policy (CSP) complement each other:
- CSP restricts where scripts can be loaded from (e.g.
script-src https://cdn.example.com). - SRI ensures what code is inside those scripts once downloaded.
Furthermore, you can enforce that all external scripts on your website must have SRI attributes by using the CSP require-sri-for directive:
Content-Security-Policy: require-sri-for script style;When enabled, the browser will refuse to execute any <script> or <link> that omits the integrity attribute.
You can craft and test your full CSP header using the Content Security Policy Generator.