> ## Documentation Index
> Fetch the complete documentation index at: https://docs.manalens.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Subresource Integrity (SRI)

> Make sure the Web SDK script on your site has not changed, with a pinned version and an integrity hash.

## Overview

Subresource Integrity (SRI) lets the browser check that a script it loads from a CDN has not changed. You add an `integrity` 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:

```html theme={null}
<script
  src="https://cdn.jsdelivr.net/npm/@untitled-analytics/web@VERSION/dist/snippet.global.js"
  integrity="sha384-..."
  crossorigin="anonymous"
  data-publishable-key="YOUR_PUBLISHABLE_KEY"
  data-endpoint="https://YOUR_COLLECTOR_ENDPOINT"
  defer
></script>
```

* `src` names an exact version, never `@latest`.
* `integrity` is the hash of that exact file.
* `crossorigin="anonymous"` is required for the browser to check the hash of a cross-origin script.

The hosted dashboard offers only a pinned snippet with a hash. If no pinned release is available, the setup page offers no script tag: use the npm install for Next.js or React instead. It never offers an unpinned script.

## How the browser checks the hash

1. The browser downloads the file in `src`.
2. It computes the SHA-384 hash of the file and compares it with `integrity`.
3. If the hashes are different, the browser does not run the script and reports an integrity error in the console.

A file that is changed in transit or on the CDN fails the check. A published version on jsDelivr does not change, so a correct hash keeps working until you update the version.

## Domains

| Domain | CSP directive | Used for |
| - | - | - |
| `https://cdn.jsdelivr.net` | `script-src` | The pinned SDK script (script tag installs only) |
| Your collector endpoint (the `data-endpoint` value) | `connect-src` | Event ingestion (`POST /v0/events`) and the other requests the SDK makes |

Allow these exact hosts, not a wildcard such as `https://*.jsdelivr.net`. With a [reverse proxy](https://docs.manalens.dev/guides/reverse-proxy.md) on your own domain, `'self'` covers both. See [CSP](https://docs.manalens.dev/security/csp.md) for a full policy.

## Verify the hash

You can compute the hash of the pinned file yourself and compare it with the `integrity` value. Replace `VERSION` with the version in your `src`.

With OpenSSL (macOS and Linux):

```bash theme={null}
curl -fsSL https://cdn.jsdelivr.net/npm/@untitled-analytics/web@VERSION/dist/snippet.global.js \
  | openssl dgst -sha384 -binary | openssl base64 -A
```

With Node.js:

```javascript theme={null}
const crypto = require('crypto');
const https = require('https');

const url = 'https://cdn.jsdelivr.net/npm/@untitled-analytics/web@VERSION/dist/snippet.global.js';

https.get(url, (res) => {
  if (res.statusCode !== 200) throw new Error(`HTTP ${res.statusCode}`);
  const hash = crypto.createHash('sha384');
  res.on('data', (chunk) => hash.update(chunk));
  res.on('end', () => console.log('sha384-' + hash.digest('base64')));
});
```

The OpenSSL output has no prefix: add `sha384-` before you compare it. The result must match the `integrity` value on the setup page exactly.

## Update the SDK

1. Copy the new script tag from the setup page or the SDK release notes.
2. Change the version and the hash together.
3. Deploy, and check that page views still arrive in [Activity](https://docs.manalens.dev/features/activity.md).

> [!WARNING]
>
> Do not change the version without the hash, and do not point a pinned snippet at an unpinned build. The hash then fails and the script does not run, so no events are sent.

## 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.

## With a reverse proxy

A [reverse proxy](https://docs.manalens.dev/guides/reverse-proxy.md) that forwards the same file unchanged keeps the same hash. If your proxy changes the file (for example, it minifies or rewrites it), the hash fails.

## Combine with CSP

For the strongest protection, use SRI with a [Content Security Policy](https://docs.manalens.dev/security/csp.md) that allows only the SDK's script host and the collector endpoint.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.