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

# Content Security Policy

> Configure Content Security Policy directives for the Foil browser SDK, including the script-src, connect-src, worker-src, and frame-src values you need.

If your site sets a `Content-Security-Policy` header, you need to allow the Foil SDK to load, execute WebAssembly, spawn inline workers, create probe iframes, and communicate with the Foil API.

## Required directives

### `script-src`

The SDK is loaded as an ESM module from the Foil CDN. It also contains an embedded WebAssembly module that requires `'wasm-unsafe-eval'` to instantiate.

```text theme={"dark"}
script-src 'self' https://cdn.usefoil.com 'wasm-unsafe-eval';
```

| Value | Why |
| - | - |
| `https://cdn.usefoil.com` | Loads the SDK bundle (`/t.js`) |
| `'wasm-unsafe-eval'` | Instantiates the embedded WebAssembly module used for device fingerprinting, behavioral analysis, and environment probing |

> [!NOTE]
>
> `'wasm-unsafe-eval'` is the standard keyword for allowing WebAssembly to compile without also allowing `eval()` and other string-to-code APIs. `'wasm-eval'` isn't a standard keyword, so use `'wasm-unsafe-eval'`.

### `connect-src`

The SDK sends encrypted observation data and receives scoring results from the Foil API, and runs the network edge probe against Foil's probe zone.

```text theme={"dark"}
connect-src 'self' https://api.usefoil.com https://*.e2.usefoil.com;
```

| Value | Why |
| - | - |
| `https://api.usefoil.com` | All collect endpoints: session creation, observation batches, fingerprint freezing, sealed token minting, and RTT pings |
| `https://*.e2.usefoil.com` | The network edge probe: a per-session subdomain of Foil's probe zone. If it is blocked, the session's network is recorded as unverified; nothing else is affected |

### `worker-src`

The SDK creates inline Web Workers from blob URLs for cross-thread environment validation and debugger detection.

```text theme={"dark"}
worker-src 'self' blob:;
```

| Value | Why |
| - | - |
| `blob:` | Inline workers are constructed via `new Blob()` + `URL.createObjectURL()` for runtime integrity checks that must execute in a separate thread |

### `frame-src`

The SDK creates temporary hidden same-origin iframes for realm isolation probes, CSP bypass detection, and font measurement.

```text theme={"dark"}
frame-src 'self';
```

| Value | Why |
| - | - |
| `'self'` | Hidden `<iframe>` elements are appended to the DOM to obtain a clean JavaScript realm for tamper-resistant API probing |
| `https://human.usefoil.com` | Only if you call `verifyHuman()`. It opens Foil's hosted verification page in an iframe |

### WebRTC traffic

Two probes use WebRTC, and their UDP traffic goes only to Foil's probe hosts:

| Host | Port | Why |
| - | - | - |
| `stun.a.e2.usefoil.com` | UDP 3479 (STUN) | The WebRTC fingerprint probe gathers ICE candidates against this STUN server |
| Per-session names under `*.e2.usefoil.com` | UDP 3478-3480 (ICE and STUN) and UDP 443 (WebTransport) | The network edge probe |

`connect-src` and `default-src` don't apply to WebRTC, so your policy needs no entry for these STUN and ICE connections. The network edge probe's HTTPS and WebTransport requests are covered by `https://*.e2.usefoil.com` in `connect-src`. If a firewall blocks outbound UDP to these ports, the fingerprint probe reports only the candidates the browser gathers locally, and the network edge probe still completes over HTTPS but without its WebRTC and WebTransport measurements.

## Full example

A minimal CSP that supports Foil alongside your own assets:

```text theme={"dark"}
default-src 'self';
script-src 'self' https://cdn.usefoil.com 'wasm-unsafe-eval';
connect-src 'self' https://api.usefoil.com https://*.e2.usefoil.com;
worker-src 'self' blob:;
frame-src 'self';
style-src 'self' 'unsafe-inline';
img-src 'self' data: https:;
object-src 'none';
base-uri 'self';
```

> [!NOTE]
>
> Foil itself needs no `font-src` - it measures font metrics in place and fetches no external fonts. Add `font-src` only for your own web fonts.

If you use a meta tag instead of a response header:

```html theme={"dark"}
<meta
  http-equiv="Content-Security-Policy"
  content="default-src 'self'; script-src 'self' https://cdn.usefoil.com 'wasm-unsafe-eval'; connect-src 'self' https://api.usefoil.com https://*.e2.usefoil.com; worker-src 'self' blob:; frame-src 'self';"
/>
```

## First-party integrations

If you serve Foil from your own domain through a [first-party integration](https://usefoil.com/docs/first-party/overview.md), the SDK loads `t.js` and sends collection requests to the integration URL instead of `cdn.usefoil.com` and `api.usefoil.com`. Allow the integration's origin in `script-src` and `connect-src`. The network edge probe still connects to Foil's probe zone directly, so keep it in `connect-src`. Step 4 of the integration page shows the exact `script-src` and `connect-src` lines for your integration next to the install snippet.

For an integration at `https://www.example.com/foil` on a page served from `https://www.example.com`, `'self'` already covers the integration origin:

```text theme={"dark"}
script-src 'self' 'wasm-unsafe-eval';
connect-src 'self' https://*.e2.usefoil.com;
```

When the integration URL is on a different origin from the page, such as `https://app.example.com` loading from `https://www.example.com/foil`, list it explicitly:

```text theme={"dark"}
script-src 'self' https://www.example.com 'wasm-unsafe-eval';
connect-src 'self' https://www.example.com https://*.e2.usefoil.com;
```

| Value | Why |
| - | - |
| The integration origin, or `'self'` | Loads `t.js` and sends every collect request through your CDN |
| `https://*.e2.usefoil.com` | The network edge probe, which connects from the browser directly to Foil and never passes through your CDN |

Remove `https://cdn.usefoil.com` and `https://api.usefoil.com` only when no page on your site loads the SDK directly. If you use `verifyHuman()`, keep `https://cdn.usefoil.com` in `script-src`, `https://api.usefoil.com` in `connect-src`, and `https://human.usefoil.com` in `frame-src`: human verification always loads from and talks to Foil directly. The `worker-src` directive doesn't change.

### Allow the install snippet

The install snippet's module script is inline. A policy whose `script-src` doesn't include `'unsafe-inline'` blocks it, even when the policy allows the integration origin. Allow it in one of three ways.

The snippet's `modulepreload` link needs no nonce or hash, because `script-src` already allows the integration origin. If your policy uses `'strict-dynamic'`, which ignores origins, give the link the same nonce as the script.

#### Use a nonce

Generate a new random nonce for every response, add it to `script-src`, and set the same value on the `<script>` element:

```text theme={"dark"}
script-src 'self' 'nonce-rAnd0mV4lue' 'wasm-unsafe-eval';
```

```html theme={"dark"}
<script type="module" nonce="rAnd0mV4lue">
  const foil = await import("https://www.example.com/foil/t.js").then((Foil) =>
    Foil.start({
      publishableKey: "pk_live_your_publishable_key",
      endpoint: "https://www.example.com/foil",
    }),
  );
</script>
```

Never reuse a nonce across responses, and don't cache a page whose HTML contains one.

#### Use a hash

Add the SHA-256 hash of the script's exact contents, everything between `<script type="module">` and `</script>`, to `script-src`:

```text theme={"dark"}
script-src 'self' 'sha256-<base64 hash>' 'wasm-unsafe-eval';
```

Compute the hash with `openssl`, where `snippet.js` holds the script's contents byte for byte, including whitespace:

```bash theme={"dark"}
openssl dgst -sha256 -binary snippet.js | openssl base64
```

The hash changes whenever the snippet's contents change, including its whitespace and the publishable key, so update the policy whenever you change the snippet.

#### Load it from a file

Save the module script's contents as a module file on your site, such as `/foil.js`, and load it with a `src` attribute. `'self'` in `script-src` then covers it, and the policy needs no nonce or hash. Keep the `modulepreload` link for `t.js` in the page:

```html theme={"dark"}
<link rel="modulepreload" href="https://www.example.com/foil/t.js" crossorigin>
<script type="module" src="/foil.js"></script>
```

```javascript theme={"dark"}
// /foil.js
const foil = await import("https://www.example.com/foil/t.js").then((Foil) =>
  Foil.start({
    publishableKey: "pk_live_your_publishable_key",
    endpoint: "https://www.example.com/foil",
  }),
);
```

### Roll out the policy

Deploy a new or changed policy with the `Content-Security-Policy-Report-Only` header first, with a `report-to` or `report-uri` endpoint. The browser then reports violations without blocking anything. Load pages that run the install snippet, confirm that no reports name the snippet, the integration URL, or `*.e2.usefoil.com`, then send the same policy as `Content-Security-Policy`.

## Graceful degradation

The SDK is designed to degrade gracefully. If a CSP directive blocks a specific probe (e.g., workers or iframes), the SDK will skip that signal and continue with the remaining detection surface. However, missing signals reduce coverage and may affect verdict confidence. For best results, allow all directives listed above.

## What Foil does NOT need

| Directive | Why not needed |
| - | - |
| `media-src` | No audio or video resources are loaded |
| `child-src` | Covered by `worker-src` and `frame-src` above |

## Troubleshooting

Check the browser DevTools console for CSP violation errors. Common failures:

| Error message | Fix |
| - | - |
| Refused to load the script `https://cdn.usefoil.com/t.js` | Add `https://cdn.usefoil.com` to `script-src` |
| Refused to compile or instantiate WebAssembly module | Add `'wasm-unsafe-eval'` to `script-src` |
| Refused to connect to `https://api.usefoil.com` | Add `https://api.usefoil.com` to `connect-src` |
| Refused to load or connect to your own integration URL | Add the integration origin to `script-src` and `connect-src`. See [First-party integrations](#first-party-integrations). |
| Refused to execute an inline script, on the install snippet | Allow the snippet with a nonce or a hash, or load it from a file. See [Allow the install snippet](#allow-the-install-snippet). |
| Refused to create a worker from `blob:` | Add `blob:` to `worker-src` |
| Refused to frame (same-origin iframe blocked) | Add `'self'` to `frame-src` |
| Refused to frame `https://human.usefoil.com` | Add `https://human.usefoil.com` to `frame-src` |

> [!TIP]
>
> If you use a CSP reporting endpoint (`report-uri` or `report-to`), deploy in report-only mode first with `Content-Security-Policy-Report-Only` to catch violations without breaking the page.


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