# Content-Security-Policy reference

> The policy consent-kit build generates: every directive, which artefact kind feeds it, how the registry's csp section changes it, and the shape of csp.json.

The policy `consent-kit build` generates: every directive, which artefact kind feeds it, how the registry's `csp` section changes it, and the shape of `csp.json`. The source is `packages/compiler/src/csp.ts`.

For why the policy is the boundary that counts, read [how consent flows at runtime](/docs/manual/concepts/runtime-consent-flow/). To change it, follow [loosen the CSP](/docs/manual/guides/loosen-the-csp/).

## The strict default

With no `csp` section, the policy is the strictest one the registry allows. For the fixture `vacation-planner.registry.yaml`:

```
default-src 'none';
script-src 'self' 'sha256-lpCxoItGn/AN3Db5+q6kQNmYs6OYxtO8HypGqcyTTME=' https://challenges.cloudflare.com https://plausible.io;
style-src 'self';
font-src 'self';
img-src 'self';
connect-src 'self' https://plausible.io;
frame-src 'self' https://challenges.cloudflare.com;
media-src 'self';
worker-src 'self';
manifest-src 'self';
object-src 'none';
base-uri 'self';
form-action 'self';
frame-ancestors 'none';
upgrade-insecure-requests
```

(The header is one line; it is wrapped here.)

| Directive | Value | Fed by |
| --- | --- | --- |
| `default-src` | `'none'` | Nothing. Whatever no directive below admits is refused. |
| `script-src` | `'self'`, the hash of the inline guard, then declared origins | `script` artefacts |
| `style-src` | `'self'`, then declared origins | `stylesheet` artefacts |
| `font-src` | `'self'`, then declared origins | `font` artefacts |
| `img-src` | `'self'`, then declared origins | `pixel` artefacts |
| `connect-src` | `'self'`, then declared origins | `endpoint` artefacts, and the origin of `receipts.endpoint` |
| `frame-src` | `'self'`, then declared origins | `iframe` artefacts |
| `media-src` | `'self'`, then declared origins | `media` artefacts |
| `worker-src` | `'self'` | No artefact kind |
| `manifest-src` | `'self'` | No artefact kind |
| `object-src` | `'none'` | |
| `base-uri` | `'self'` | |
| `form-action` | `'self'` | The registry does not record where a form posts to. A third-party form target is a `csp.allow` entry. |
| `frame-ancestors` | `'none'` | Only works as a response header. |
| `upgrade-insecure-requests` | present | Left out when the registry declares an `http:` URL, or when `csp.upgradeInsecureRequests` is `false`. |

Cookies and storage artefacts feed nothing: a Content-Security-Policy does not govern them.

What strict means:

- No `'unsafe-inline'`, no `'unsafe-eval'`, no wildcard, no `data:` and no `blob:`.
- Inline scripts run only by hash or nonce. `build` adds the hash of `guard.inline.js` for you. Inline styles run only by hash.
- `'self'` is the baseline of every fetch directive, including those with nothing declared. First-party files are never declared in the registry, so the compiler cannot know which kinds a site serves itself.
- Declared origins are sorted, so the header is stable.

## Origins, not paths

The policy lists the origin of each declared URL, not its path. `https://plausible.io/js/script.js` becomes `https://plausible.io` in `script-src`.

This is deliberate. Path matching in a Content-Security-Policy is unreliable as a boundary: it is ignored after a redirect, a path source without a trailing slash matches one exact file, and vendors move files within their origin. So a path that nothing declares, on an origin that is declared, passes the policy. The guard manifest and the audit keep the declared paths, and report such a request.

## The policy does not depend on consent

The header is the same before and after a visitor's decision. It separates declared from undeclared, not consented from unconsented. Holding a declared resource back until consent is the job of the gates: the blocked script tag, the embed placeholder, the storage facade and the guard.

This is why a `stylesheet`, `font` or `media` artefact cannot be bound to a consent purpose: no gate exists for those loads, and the policy admits them as soon as they are declared.

## Loosening

Every deviation is written in the registry's `csp` section. There is no switch that turns the policy off. The fields are in the [registry reference](/docs/manual/reference/registry/#csp).

| Registry | Effect on the policy | Deviation reported |
| --- | --- | --- |
| `csp.allow[]` | Each source is appended to the directive. A directive whose value is `'none'` loses the `'none'` at the first addition. | `sources-added`, origin `registry` |
| `csp.reportOnly: true` | The header name becomes `Content-Security-Policy-Report-Only`. Nothing is blocked. | `report-only`, origin `registry` |
| `csp.upgradeInsecureRequests: false` | `upgrade-insecure-requests` is left out. | `directive-omitted`, origin `registry` |
| An artefact with an `http:` URL | `upgrade-insecure-requests` is left out, because it would turn the declared load into a failing `https:` request. | `directive-omitted`, origin `automatic` |
| `csp.reportUri` | Adds `report-uri <value>`. | none |
| `csp.reportTo` | Adds `report-to <group>`. With `reportUri` too, `Reporting-Endpoints: <group>="<uri>"` is generated. | none |

The directives that `csp.allow` may name are `default-src`, `script-src`, `style-src`, `font-src`, `img-src`, `connect-src`, `frame-src`, `media-src`, `worker-src`, `manifest-src`, `object-src`, `base-uri`, `form-action` and `frame-ancestors`.

Deviations are listed by `consent-kit check` (and carried in `check --json` as `csp.deviations`) and by the audit report. Automatic ones are marked:

```
Content-Security-Policy: 1 deviation from strict
  upgrade-insecure-requests is left out (automatic): The registry declares http: URLs (http://127.0.0.1:4174); upgrading them to https would break those declared loads. Declare https: URLs to get the directive back.
```

In the diff, adding a source, switching to report-only or dropping `upgrade-insecure-requests` is `csp.loosened`, which needs a confirmation in `privacy.review.json` and never asks visitors again. Removing one is `csp.tightened`. A change to `reportUri` or `reportTo` is `csp.reporting_changed`; a reworded reason is `csp.reason_changed`. None of the last three needs a sign-off.

## csp.json

| Field | Type | Meaning |
| --- | --- | --- |
| `level` | `"strict"` or `"loosened"` | `strict` when there is no deviation. |
| `deviations` | array | `{ directive, kind, sources?, reason, origin }`. `directive` is `"policy"` for report-only. |
| `directives` | object | Directive to its values, in header order. A directive without values (`upgrade-insecure-requests`) has `[]`. |
| `headerName` | string | `Content-Security-Policy` or `Content-Security-Policy-Report-Only`. |
| `header` | string | The header value. |
| `meta` | string or `null` | The value for `<meta http-equiv="Content-Security-Policy">`, for hosts that cannot set headers: the same policy without `frame-ancestors`, `report-uri` and `report-to`, which browsers ignore in a meta element. `null` for a report-only policy, which cannot be delivered by meta. |
| `reportingEndpoints` | string, optional | The value of the `Reporting-Endpoints` header. |

Prefer the header. Only the header protects against framing.

## Delivering the policy

- **A host that reads `_headers`** (Netlify, Cloudflare Pages): copy the generated `_headers` next to the built pages. `examples/static-site/src/build.ts` does this.
- **Your own server or adapter**: read `headerName` and `header` from `csp.json` and set them on every HTML response.
- **A host that cannot set headers**: put `meta` from `csp.json` in a `<meta http-equiv="Content-Security-Policy">` element, before any script. You lose `frame-ancestors` and reporting.

## Using nonces instead of the guard hash

`consent-kit build` allows the inline guard by hash and offers no nonce option. A setup that sets a nonce per response calls `contentSecurityPolicy(registry, options)` from the `@consent-kit/cli` package itself:

| Option | Meaning |
| --- | --- |
| `noncePlaceholder` | Adds `'nonce-<placeholder>'` to `script-src`, for the server to replace per response, for example `"{{CSP_NONCE}}"`. Letters, digits and `{}_-.` only. |
| `inlineScriptHashes` | Base64 SHA-256 digests of inline scripts to allow in `script-src`. Each must be the digest of the script element's exact text. |
| `inlineStyleHashes` | Base64 SHA-256 digests of inline `<style>` elements to allow in `style-src`. Hashes do not cover `style="…"` attributes. |

## CookieConsent under this policy

CookieConsent works under the strict policy unchanged, provided its stylesheet is served by the site (`cookieconsent.css` from `'self'`, as in `examples/static-site`). It adds neither `<style>` elements nor `style` attributes.

## What the audit checks

The audit's setup check "no Content-Security-Policy violations in the page itself" fails when a page's own markup or code is refused by its policy: an inline `<style>`, a `style` attribute, an inline event handler, `eval`. A refused request to an undeclared third party is reported by "Undeclared drift" instead. See the [audit reference](/docs/manual/reference/audit/).
