Overview
Subresource Integrity (SRI) lets the browser check that a script it loads from a CDN has not changed. You add anintegrity attribute with a hash of the file. If the file on the CDN is different, the browser does not run it.
This protects your site if the CDN or the path to it is compromised: a changed SDK file cannot run on your pages.
The pinned snippet
The setup page gives a script tag for one exact SDK version on jsDelivr, with its SRI hash:srcnames an exact version, never@latest.integrityis the hash of that exact file.crossorigin="anonymous"is required for the browser to check the hash of a cross-origin script.
How the browser checks the hash
- The browser downloads the file in
src. - It computes the SHA-384 hash of the file and compares it with
integrity. - If the hashes are different, the browser does not run the script and reports an integrity error in the console.
Domains
Allow these exact hosts, not a wildcard such as
https://*.jsdelivr.net. With a reverse proxy on your own domain, 'self' covers both. See CSP for a full policy.
Verify the hash
You can compute the hash of the pinned file yourself and compare it with theintegrity value. Replace VERSION with the version in your src.
With OpenSSL (macOS and Linux):
sha384- before you compare it. The result must match the integrity value on the setup page exactly.
Update the SDK
- Copy the new script tag from the setup page or the SDK release notes.
- Change the version and the hash together.
- Deploy, and check that page views still arrive in Activity.
npm installs
When you install@untitled-analytics/web from npm, your bundler includes the SDK in your own scripts. SRI on the CDN tag does not apply. Pin the version in your lockfile and review updates as you do for other dependencies.