Subresource Integrity
| Type | Browser security feature |
|---|---|
| Also known as | SRI |
| Specification | W3C Recommendation, 23 June 2016; level 2 in draft |
| HTML attribute | integrity, with crossorigin |
| Hash algorithms | SHA-256, SHA-384, SHA-512 |
| Weakness addressed | CWE-829: Inclusion of Functionality from Untrusted Control Sphere |
| OWASP Top 10 | A08:2025 Software or Data Integrity Failures |
A page that includes https://cdn.example.com/lib.js runs whatever that server returns. Whoever gains control of the server can edit the file or swap it for another, and every page that includes it then runs the new code.1 Subresource Integrity (SRI) lets the page fix in advance which file it will accept. The integrity attribute on the tag carries a cryptographic hash of the expected file; the browser hashes what it downloads and discards a file whose hash differs.1 The W3C published the mechanism as a Recommendation on 23 June 2016.2 Its stated goal is that a breach at a third-party host should not turn, by itself, into a breach of every site that loads scripts from it, the pattern described in Software supply chain compromise.3
<script src="https://cdn.example.com/lib@1.2.3/lib.min.js"
integrity="sha384-(base64 hash of the file)"
crossorigin="anonymous"></script>How the browser checks the hash
The attribute holds one or more values separated by spaces. Each value is an algorithm name, a dash and the hash written in base64, a way of spelling binary data with letters and digits. Browsers accept SHA-256, SHA-384 and SHA-512. It applies to <script> elements and to <link> elements whose rel is stylesheet, preload or modulepreload.1
Before running a script or applying a stylesheet, the browser picks the strongest algorithm that appears in the attribute and drops the values made with weaker ones. It hashes the downloaded bytes with that algorithm and compares the result with each remaining value. One match lets the file load. With no match the download ends as a network error, and the script never runs.1 Two values with the same algorithm therefore allow two versions of one file.
A value that names an algorithm the browser does not know counts as if the attribute were absent, so a site can add a newer algorithm without breaking older browsers. MD5 and SHA-1 are on the draft's list of hashes browsers should refuse, and SHA-384 is its suggested baseline.3 A hash fits one exact sequence of bytes, so the URL has to name a fixed version of the file: a file that the host replaces in place stops loading after each change.
The crossorigin requirement
By default a tag fetches a file from another site in no-cors mode, in which the browser may use the response while the page may not read it. If integrity checks worked on such requests, a hostile page could load a private file from another site with a guessed hash and learn from the load or error event whether the guess was right. Browsers close that leak by failing every no-cors request that carries an integrity value.1
A tag that loads from another site with integrity also needs crossorigin="anonymous", and the host has to answer with an Access-Control-Allow-Origin header, which makes the request a CORS request. Public CDNs commonly send the value *.1
Limits
The check compares bytes. A file that was already harmful when the developer computed its hash passes it. On a page served over plain HTTP, the hash protects only against a breach of the host: an attacker on the network path can rewrite or delete the attribute in the HTML itself, which is why integrity values are meant to be sent only from HTTPS pages.3
Polyfill.io
Polyfill.io served code that adds missing features to older browsers, and built that code from the HTTP headers of each request.4 Two browsers could therefore get different files from the same URL, and a hash taken from one copy would fail on the other.1
In February 2024 the domain and its GitHub account were sold to a company that Cloudflare identified as Funnull.45 On 25 June 2024 Sansec reported that cdn.polyfill.io was injecting malware into more than 100,000 sites that embedded it, JSTOR, Intuit and the World Economic Forum among them. The sample it decoded sent some mobile visitors, at set hours, to a sports betting site by way of a domain spelled to resemble Google Analytics, and stayed quiet when it detected an administrator.4 Cloudflare rewrote references in its customers' pages to a copy it had hosted on cdnjs since February, and the registrar Namecheap put the domain on hold.54
The library's original author had said that current browsers no longer need it, and Sansec advised removing every reference to polyfill.io.4 For sites that still depend on it, cdnjs.cloudflare.com/polyfill/ serves the same content.5
Adding SRI to a site
OpenSSL prints the value for a file:1
cat lib.min.js | openssl dgst -sha384 -binary | openssl base64 -ANext.js 14 and later has an experimental option that hashes the JavaScript files at build time and writes the values into the script tags, in the App Router only. The framework offers it as a way to keep pages statically generated under a strict Content Security Policy, where nonces would force each page to be rendered again for every request.6
// next.config.js
module.exports = {
experimental: { sri: { algorithm: 'sha384' } },
};A module pulled in with import has no tag of its own. An import map can carry an integrity object that maps each module URL to its hash.7
The Integrity-Policy response header, set alongside the other security headers, makes the attribute compulsory. With blocked-destinations=(script), the browser blocks every script request that lacks an integrity value or uses no-cors mode, and posts an integrity-violation report to the endpoints listed in a Reporting-Endpoints header; style is the one other destination allowed. Sites usually start with Integrity-Policy-Report-Only, which reports without blocking, and switch once the reports stop.1
Reporting-Endpoints: integrity-endpoint="https://example.com/reports"
Integrity-Policy-Report-Only: blocked-destinations=(script), endpoints=(integrity-endpoint)List every script and stylesheet tag in this project that loads a file from another domain. For each one, pin the URL to an exact version, compute a sha384 hash of that exact file, and add integrity="sha384-..." and crossorigin="anonymous" to the tag. If the URL returns different content per browser or per request, do not hash it: tell me, and suggest serving a copy from this project instead. Do not change what the page loads.
In SecFlash
SecFlash reports a script from another site that newly appears on a page as SF-SEC-401, and a script from another company on a login or payment page as SF-SEC-402, both on the Security page.
See also
- Content Security Policy
- HTTP security headers
- Software supply chain compromise
- Responding to unauthorised changes to a website
- Cross-origin resource sharing
- Cross-site scripting
- Category: Software supply chain
References
- a b c d e f g h i "Subresource Integrity". MDN Web Docs. Mozilla.
- back Akhawe, D.; Braun, F.; Marier, F.; Weinberger, J., eds. (23 June 2016). "Subresource Integrity". W3C Recommendation. W3C.
- a b c Braun, Frederik, ed. (20 March 2026). "Subresource Integrity". W3C Working Draft. W3C Web Application Security Working Group.
- a b c d e Sansec Forensics Team (25 June 2024, updated 28 June 2024). "Polyfill supply chain attack hits 100K+ sites". Sansec.
- a b c Cloudflare (June 2024). "Automatically replacing polyfill.io links with Cloudflare's mirror for a safer Internet". The Cloudflare Blog.
- back "How to configure Content Security Policy for your Next.js application", section Subresource Integrity (Experimental). Next.js documentation. Vercel.
- back "<script type="importmap">". MDN Web Docs. Mozilla.
External links
- A08:2025 Software or Data Integrity Failures, OWASP Top 10:2025
- CWE-829: Inclusion of Functionality from Untrusted Control Sphere, MITRE CWE