Subresource Integrity: give a script or stylesheet the hash of the exact file you expect, and the browser refuses to use anything else.
Overview
The integrity attribute carries a cryptographic hash of the file you expect. The browser hashes what it downloads, and if the two do not match it refuses to run the script or apply the stylesheet, and fires an error event. This is Subresource Integrity (SRI). It protects you when a CDN or other third-party host is compromised or changes a file under a URL you already use.
SRI fails closed. A wrong hash blocks the file. So does a cross-origin file loaded without crossorigin: the browser cannot check a no-cors response, so it refuses it rather than running it unchecked.
Applies to: <script src> (classic and module) and <link> with rel="stylesheet", rel="preload" or rel="modulepreload". Import maps also have an integrity section for modules (see below).
Both tags load the same file. The first has its current hash and runs. The second has the hash of an earlier version, so the browser blocks it and logs an integrity error.
Values
One or more space-separated hashes, each written algorithm-base64digest. An empty value means no check. A value with no valid hash in it also means no check, so a typo in the prefix silently turns SRI off.
| Prefix | Digest length (base64) | Notes |
sha256- | 44 characters | Minimum allowed. |
sha384- | 64 characters | The usual choice. |
sha512- | 88 characters | Strongest. |
Several hashes: the browser keeps only the hashes that use the strongest algorithm present, then accepts the file if any of those match. Mixing sha256 and sha384 adds no strength (the sha256 one is ignored). Several hashes of the same algorithm are useful when a URL may legitimately serve one of a few known files, for example during a rollout.
Examples
Generating a hash
Hash the exact bytes that will be served, from a copy you trust:
12345678
openssl dgst -sha384 -binary vanilla-breeze.js | openssl base64 -A curl -sL https://unpkg.com/[email protected]/dist/cdn/vanilla-breeze.js \ | openssl dgst -sha384 -binary | openssl base64 -A
Loading Vanilla Breeze from a CDN with SRI
The Quick Start uses a URL with no version (unpkg.com/vanilla-breeze/…), which the CDN redirects to the newest release. That is convenient, but it cannot be combined with SRI: the next release changes the bytes and the hash stops matching. Pin an exact version and add the hashes. These are the real hashes of the 0.5.0 entry files:
12345678
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/cdn/vanilla-breeze.css" integrity="sha384-gQwnTZMGbiu+eGWq8Z8Hwj4Ds6/b/baYpNiLzZupXkRzh7JZKSp8aqD2Ndc4qkgN" crossorigin="anonymous"><script type="module" src="https://unpkg.com/[email protected]/dist/cdn/vanilla-breeze.js" integrity="sha384-uZ2nKtKxACqlcJEEjavnf+D7QdWK23BYOIzSpbqD5Ej6Qh6ZmfNsuI/BV/fbDWRG" crossorigin="anonymous"></script>
Module scripts are always fetched with CORS, so crossorigin is technically optional on the script tag; keep it for clarity. Recompute the hashes whenever you change the version.
Preloads and modulepreload
A preload must carry the same integrity (and crossorigin) as the tag that uses the file, or the preloaded response cannot be reused.
1234
<link rel="modulepreload" href="https://unpkg.com/[email protected]/dist/cdn/vanilla-breeze.js" integrity="sha384-uZ2nKtKxACqlcJEEjavnf+D7QdWK23BYOIzSpbqD5Ej6Qh6ZmfNsuI/BV/fbDWRG" crossorigin="anonymous">
Import map integrity
The integrity attribute covers one file. Modules that file imports, statically or with import(), are separate requests with no hash. An import map's integrity section maps module URLs to hashes, and the browser checks them whenever those URLs load as modules.
12345678
<script type="importmap">{ "integrity": { "https://cdn.example.com/[email protected]/charts.js": "sha384-...", "https://cdn.example.com/[email protected]/axis.js": "sha384-..." }}</script>
In Vanilla Breeze
SRI on the VB entry tags covers those two files only. The full bundle loads more on demand, and none of it is covered:
- Packs (
dist/cdn/packs/<name>.full.js and .full.css), loaded with import() and a created <link> when a pack is activated (activateBundle() in src/lib/bundle-registry.js).
- Themes that are not built in, added as
<link> elements by src/lib/theme-loader.js.
- The autoload build (
vanilla-breeze-autoload.js), which fetches each component's chunk listed in components/manifest.json.
- Third-party modules:
<diagram-wc> imports Mermaid from cdn.jsdelivr.net at a pinned version (override with window.VB_MERMAID_URL).
- Fonts and other
url() requests made by the stylesheet.
For full coverage, either self-host dist/cdn/ (then you control every byte and the question goes away), or add import map integrity entries for the module chunks you use. Import maps do not cover stylesheets added with <link>.
Security
- SRI protects third-party hosting. For files on your own origin it only guards against tampering in caches or on the CDN in front of you.
- Generate hashes yourself from a reviewed copy, not from the host you are trying to protect against.
- Pair it with a CSP. SRI checks the files you list; a Content Security Policy (
nonce) stops injected tags from loading anything else.
Browser support
integrity on <script> and <link>: all browsers (Chrome 45, Firefox 43, Safari 11.1). Import map integrity: Chrome 127, Firefox 138, Safari 18 (MDN browser-compat-data, November 2026).
Gotchas
- Forgetting
crossorigin on a cross-origin classic script or stylesheet blocks it, even with the right hash. The server must also send Access-Control-Allow-Origin.
- Floating versions (
@latest, @0.6) break as soon as the file changes.
- Byte-exact. A CDN that re-minifies or re-compresses content differently changes the hash. (Transfer encoding such as gzip does not; the hash is of the decoded body.)
- Hash tools:
openssl ... | openssl base64 -A prints one line. Wrapped output or a hex digest is not valid in the attribute.
- Bundlers do not add SRI by default; Vite, webpack and others need a plugin.
Related
Which to choose
- A pinned file from a public CDN —
integrity plus crossorigin="anonymous".
- A module graph from a CDN —
integrity on the entry plus import map integrity for the rest, or self-host.
- Your own files — self-host with a CSP; SRI is optional.