Pinning third-party scripts and styles
Subresource Integrity (SRI) lets a page declare the cryptographic hash a
<script> or <link rel="stylesheet"> resource must match. If a CDN is
compromised or the file is altered in transit, the browser refuses to execute
or apply it. This reference covers the integrity attribute syntax, the
allowed hash algorithms and the crossorigin requirement, with a live parser.
How it works
An integrity value is one or more space-separated tokens. Each token is an
algorithm prefix and a base64-encoded digest of the resource’s raw bytes:
<script
src="https://cdn.example.com/lib.js"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
crossorigin="anonymous"></script>
The prefix is sha256-, sha384- or sha512-. The base64 payload decodes to
a digest of 32, 48 or 64 bytes respectively, so the encoded string has a fixed
length per algorithm (44, 64 and 88 base64 characters including = padding).
On download the browser hashes the bytes with the named algorithm and compares
to the pinned digest; a mismatch blocks the resource.
Tips and notes
- Generate a hash with
openssl dgst -sha384 -binary file.js | openssl base64 -A. - Always pair cross-origin SRI with
crossorigin="anonymous"or verification is skipped and the resource is blocked. - List several hashes (space-separated) to migrate algorithms without breaking older builds.
- Recompute the hash on every file change — even a whitespace edit changes the digest.
- SRI covers
scriptandlink; it does not protect images or other element types.