# Content Security Policy

> The exact CSP directives the Reveata script needs, with the extras script, the queue snippet, the managed proxy and your own proxy, plus examples for common servers.

If your site sends a `Content-Security-Policy` header (or a `<meta http-equiv="Content-Security-Policy">` tag), the browser only loads scripts and sends requests to the places it lists. When Reveata is missing from it, the script is blocked and no visits arrive. Your browser's console then shows an error such as *Refused to load the script* or *Refused to connect*.

The script needs two things:

- **`script-src`**: the address the script is loaded from.
- **`connect-src`**: the address events are sent to. The script sends them with `navigator.sendBeacon`, or `fetch` when beacons are not available, so they fall under `connect-src`.

It loads nothing else: no images, frames, fonts, styles or `eval`.

## The directives

| How you load Reveata | `script-src` | `connect-src` |
| --- | --- | --- |
| Standard snippet | `https://reveata.com` | `https://reveata.com` |
| [Managed proxy](https://reveata.com/docs/managed-proxy) on `stats.example.com` | `https://stats.example.com` | `https://stats.example.com` |
| [Your own proxy](https://reveata.com/docs/self-hosted-proxy) on your domain | `'self'` | `'self'` |
| Your own proxy for events only (`data-api="/dt/event"`, script from Reveata) | `https://reveata.com` | `'self'` |

The opt-in extras script (`/js/script.extras.js`, for broken links, downloads, forms and web vitals) comes from the same address as the main script, so it needs nothing more. The cookieless build (`/js/script.cookieless.js`) is the same.

A minimal policy for the standard snippet:

```
Content-Security-Policy: default-src 'self'; script-src 'self' https://reveata.com; connect-src 'self' https://reveata.com
```

Keep your other sources in those directives; add Reveata to them rather than replacing them.

## The queue snippet

The snippet itself is an external script and needs no `'unsafe-inline'`. Only the optional queue snippet, which lets you call `reveata()` before the script has loaded, is inline. Allow it in one of three ways:

1. **Move it to a file** you serve yourself, such as `/reveata-queue.js`, loaded before the snippet. `'self'` then covers it.
2. **Use a nonce**, the same one your server puts on its other inline scripts: `<script nonce="…">`.
3. **Use a hash.** For the queue snippet written exactly as this one line, with nothing before or after it inside the tag:

   ```html
   <script>window.reveata=window.reveata||function(){(window.reveata.q=window.reveata.q||[]).push(arguments)};</script>
   ```

   add `'sha256-wKW6agP5neyn7PyV6CfoGNfeBjGGnzy8ovggEAXlE+c='` to `script-src`. Any other spacing or line breaks change the hash.

## Next.js

Add Reveata to the policy you build in `next.config.ts` headers or in your middleware:

```ts
// next.config.ts
const csp = [
  "default-src 'self'",
  "script-src 'self' https://reveata.com",
  "connect-src 'self' https://reveata.com",
].join('; ');

export default {
  async headers() {
    return [{ source: '/:path*', headers: [{ key: 'Content-Security-Policy', value: csp }] }];
  },
};
```

The `<Reveata />` component from `@reveata/next` renders the queue snippet inline without a nonce. With a nonce-based policy, render the two scripts yourself with `next/script` and your nonce, using the helpers the package exports:

```tsx
import Script from 'next/script';
import { headers } from 'next/headers';
import { QUEUE_STUB, scriptAttributes, scriptSrc } from '@reveata/next';

const options = { websiteId: 'ws_your_tracking_id', host: 'https://reveata.com' };

export async function Analytics() {
  const nonce = (await headers()).get('x-nonce') ?? undefined;
  return (
    <>
      <Script id="reveata-queue" nonce={nonce} strategy="afterInteractive">
        {QUEUE_STUB}
      </Script>
      <Script
        id="reveata"
        nonce={nonce}
        src={scriptSrc(options)}
        strategy="afterInteractive"
        {...scriptAttributes(options)}
      />
    </>
  );
}
```

`x-nonce` is the request header your middleware sets when it generates the nonce, as in the Next.js CSP guide. With `'strict-dynamic'` in `script-src`, scripts added by a nonced script are allowed without listing their host, but keep Reveata in `connect-src`.

## Nginx

```nginx
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://reveata.com; connect-src 'self' https://reveata.com" always;
```

## Apache

```apache
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' https://reveata.com; connect-src 'self' https://reveata.com"
```

## Netlify

In `netlify.toml`:

```toml
[[headers]]
  for = "/*"
  [headers.values]
    Content-Security-Policy = "default-src 'self'; script-src 'self' https://reveata.com; connect-src 'self' https://reveata.com"
```

## Vercel

In `vercel.json`:

```json
{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        {
          "key": "Content-Security-Policy",
          "value": "default-src 'self'; script-src 'self' https://reveata.com; connect-src 'self' https://reveata.com"
        }
      ]
    }
  ]
}
```

## A meta tag

Static hosts without header settings can put the policy in the page `<head>`, before any script:

```html
<meta
  http-equiv="Content-Security-Policy"
  content="default-src 'self'; script-src 'self' https://reveata.com; connect-src 'self' https://reveata.com"
/>
```

## WordPress

WordPress sends no policy of its own. If a security plugin or your host adds one, add the Reveata address to its `script-src` and `connect-src` settings. With [your own proxy](https://reveata.com/docs/self-hosted-proxy#wordpress-without-a-plugin), `'self'` is enough.

## Check it works

1. Open your site with the browser's developer tools on the **Console** tab and reload. No *Refused to load* or *Refused to connect* message should mention Reveata or your proxy path.
2. On the **Network** tab, the script request answers `200` (or `304`) and each event request answers `202`.
3. Your visit appears in **Realtime**. If it still doesn't, see [Why events don't show up](https://reveata.com/docs/events-not-showing).

While testing a new policy, you can send it as `Content-Security-Policy-Report-Only` first: the browser reports what it would block without blocking it.
