The problem in one line
By loading a library from someone else's domain you grant that domain the right to run any code on your page — with access to forms, cookies and content. While the file stays unchanged, all is well. But you control neither that server, nor its administrator, nor what happens after it is compromised. Your page will keep loading the same address while the code inside is different.
Subresource Integrity, or SRI, closes exactly this gap: the browser compares the file it received against the hash you declared and refuses to execute the file if they differ.
What it looks like
The canonical form from the specification is a hash in the integrity attribute with a crossorigin attribute alongside it:
<script src="https://cdn.example.com/framework.js"
integrity="sha384-Li9vy3DqF8tnTXuiaAJuML3ky+er10rcgNR/VqsVpcw+ThHmYcwiB1pbOxEbzJr7"
crossorigin="anonymous"></script>The value is the hash name, a hyphen, and the hash itself in base64. The specification allows three families: sha256, sha384 and sha512. The practical choice is sha384: shorter than sha512 and comfortably stronger than sha256.
How to compute the hash
The hash is taken over the very file that will be served. The simplest way is to take it straight from the address you load:
curl -sS https://cdn.example.com/framework.js \
| openssl dgst -sha384 -binary \
| openssl base64 -AThe resulting string goes after sha384-. For a local file the command is the same, with reading the file in place of curl. If the file is built by you, computing the hash at build time and inserting it into the markup automatically is convenient: by hand it is forgotten the first time the library is updated.
Why crossorigin is needed
This is the part where SRI most often "does not work". The browser will not verify the hash of a file whose contents it is not entitled to know: for a foreign domain the response is opaque by default. The crossorigin="anonymous" attribute switches the request into permission-checked mode, and then the third-party server must send an Access-Control-Allow-Origin header.
Two consequences follow. First: if that server does not send the header, loading its file with an integrity check is impossible — the limitation is not yours and cannot be worked around. Second: omit crossorigin and you get not an error but a file that silently does not execute, and the breakage will look like "the script stopped loading".
What happens on a mismatch
The browser does not execute the file at all — it makes no attempt to "execute partially" and does not ask the visitor. A mismatch message appears in the console, and the feature the script provided disappears from the page.
| Case | What the visitor sees |
|---|---|
| The file was substituted | The script does not run — the protection worked |
| The library was updated at the same address | The same: the hash is stale, the feature is gone |
No crossorigin | The script does not run, for no obvious reason |
| The server compresses the file differently | The hash covers the final bytes, so there should be no mismatch |
The second row is the main reason SRI is sometimes called inconvenient. It also explains the rule: SRI applies only to addresses pinned to a version. A reference like /latest/lib.js and SRI are incompatible by meaning: you are asking for "always the newest" and "exactly this one" at the same time.
Where SRI will not help
- If your own site is compromised. Whoever edits your markup will simply change the hash along with the link.
- For references created by code. A script that adds another script bypasses the attribute — the loader library itself is what needs checking.
- For dynamic files. Counters and widgets that differ on every request do not fit a hash check.
Hence the practical conclusion: SRI is a shield against someone else's server being compromised, not a replacement for other measures. It works together with a CSP policy, which limits which domains may serve code at all.
Where to start on your own site
- Write down every external reference: look for
srcandhrefpointing at other domains. - For each one decide whether it is needed at all — a superfluous reference is easier to remove than to protect.
- Pin the rest to a version and add the hash with
crossorigin. - Recheck the page in a browser with the console open: SRI breaks silently.
What sits next to this
Which domains may serve code to your page at all is a job for CSP: Content Security Policy. The full set of protective headers is security headers. Checking your own dependencies for known flaws is npm audit and composer audit.