A CDN parcel on the post’s table: a sha384 from crypto.subtle as integrity, a stylesheet loaded for real as a blob:. If one byte changes, what does your…
Why it matters
One breached CDN can inject code into thousands of sites at once. SRI makes the browser refuse a file whose bytes differ from what you checked, so a CDN cannot quietly swap its contents.
The key idea
integrity pins a file to its exact bytes
Common mistakes
Forgetting crossorigin="anonymous" Without CORS, a response from another origin is opaque: the browser cannot read its bytes to hash them, so the file with integrity is refused (the console says so). Set crossorigin="anonymous", and the CDN must send Access-Control-Allow-Origin.
Pinning a file whose version is "latest" or auto-updating If the URL points at a version that can change (/lib@3, @latest), every new release stops matching and is refused. Pin an EXACT version (/[email protected]) whose contents never change.
Using SHA-1, or copying a hash from an untrusted source SRI only accepts sha256, sha384, sha512. A sha1-… token is ignored, and if it is the only one, the file loads WITHOUT being checked. Compute the hash from a file you checked yourself, not from the same CDN.
Try it in this playground
Load a matching parcel Your browser hashes it and loads it.
Change one byte, see the browser refuse A wholly different hash: an error event, not used.
Pin an exact version Back to 1.2.0, or recompute the hash from a checked 1.3.0.
See sha1 ignored The changed file still loads: nothing is checked.