# Loosen the CSP correctly

> Add an exception to the generated Content-Security-Policy when the site needs one, in a way that is validated, visible in every report and signed off by a person.

Add an exception to the generated Content-Security-Policy when the site needs one, in a way that is validated, visible in every report and signed off by a person.

The generated policy is strict: `default-src 'none'`, no inline script or style, no wildcard, and only the origins the registry declares. There is no switch that turns it off. Every deviation is one entry in the registry's `csp` section, per directive, with a reason.

## First: do you need to loosen it at all?

Most refused loads are missing declarations, not reasons for an exception.

| The browser refuses | Do this instead of loosening |
| --- | --- |
| A script, iframe, pixel, stylesheet, font or media file from a third party | Declare it as an artefact. Its origin is then added to the right directive. See [write a registry entry](/docs/manual/guides/write-a-registry-entry/). |
| A `fetch` or beacon to a third party | Declare an `endpoint` artefact. |
| CookieConsent's stylesheet from a CDN | Serve `cookieconsent.css` from your own origin. |
| An inline `<script>` of your own | Move it into your bundle. |
| An inline `<style>` element of your own | Move it into a stylesheet, or allow it by hash with `inlineStyleHashes` if you generate the policy through the API. |

Loosen only what is left: typically a legacy widget that sets `style` attributes, images from `data:` URLs, or a form that posts to another origin.

## 1. Find the directive and the source

Open the browser console on the failing page. The violation message names the directive and what was blocked:

```
Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'".
```

Pick the narrowest source that fixes it. A specific host is better than a scheme, and a scheme is better than `'unsafe-inline'`.

## 2. Add the entry

```yaml
csp:
  allow:
    - directive: style-src
      sources: ["'unsafe-inline'"]
      reason: The legacy chat widget sets style attributes (WEB-123).
```

- `directive` is one of `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`, `frame-ancestors`.
- `sources` are written exactly as they appear in the header. A keyword keeps its single quotes, so in YAML it sits inside double quotes: `"'unsafe-inline'"`. A scheme ends in a colon: `"data:"`. A host is `"https://cdn.example"` or `"*.example.org"`.
- `reason` is required. Write why, and where the work to remove it is tracked. It is printed wherever the deviation is listed.

More examples:

```yaml
csp:
  allow:
    - directive: img-src
      sources: ["data:"]
      reason: The charting library renders legends as data URLs (WEB-207).
    - directive: form-action
      sources: ["https://payments.example"]
      reason: The checkout form posts to the payment provider.
```

`form-action` is the one directive where an exception is normal: the registry does not record where a form posts to, so a third-party form target can only be allowed here.

## 3. Check

```sh
npx consent-kit check
```

A malformed entry is a validation error:

| Rule | Cause |
| --- | --- |
| `csp/unknown-directive` | The directive is not one of the list above. |
| `csp/invalid-source` | Not a source expression. Usually a keyword without its quotes: `unsafe-inline` would be read by a browser as a host name. |
| `csp/redundant-source` | `'none'`, or a source another entry already allows for the same directive. |

A valid entry is listed as a deviation on every run from now on:

```
Content-Security-Policy: 1 deviation from strict
  style-src also allows 'unsafe-inline': The legacy chat widget sets style attributes (WEB-123).
```

## 4. Confirm it

If the registry has been released before, the loosening is a change that a person has to confirm:

```
Needs confirmation (presumed not material): 1
  csp.loosened@csp.allow.style-src.'unsafe-inline'  [not acknowledged]
      (none) -> "The legacy chat widget sets style attributes (WEB-123)."
      …

error  confirmation/unacknowledged  "csp.loosened@csp.allow.style-src.'unsafe-inline'" is presumed not material but nobody confirmed it. Add its key to "acknowledged" in privacy.review.json, or escalate it under "overrides".
```

Add the key to `privacy.review.json`, next to the registry:

```json
{
  "acknowledged": [
    "csp.loosened@csp.allow.style-src.'unsafe-inline'"
  ]
}
```

A loosening says nothing new to visitors, so it never asks them again and needs no purpose version bump. It needs a confirmation because someone should decide that a weaker policy is wanted.

## 5. Build and deploy

```sh
npx consent-kit build
```

`_headers` and `csp.json` now carry the looser policy, and `csp.json` has `"level": "loosened"` with the deviation and its reason. Deploy the header with the site.

## Rolling a policy out with report-only

On a site that never had a policy, start by observing:

```yaml
csp:
  reportOnly: true
  reportUri: /csp-reports
  reportTo: csp
```

- `reportOnly: true` emits `Content-Security-Policy-Report-Only`. Violations are reported; nothing is blocked.
- `reportUri` is where browsers POST violation reports. You provide that endpoint; no consent-kit package receives them. A third-party URL must be a declared `endpoint` artefact (`csp/undeclared-report-endpoint`), because every report carries the visitor's IP address and the page URL.
- `reportTo` names a reporting group. With both set, a `Reporting-Endpoints` header is generated too.

Report-only is itself a deviation, listed on every check as "the policy is report-only and blocks nothing", and switching to it is a `csp.loosened` change. It is for rolling a policy out, not for staying on. Remove `reportOnly` once the reports are quiet; that is `csp.tightened` and needs no sign-off.

## Serving over plain http

`upgrade-insecure-requests` tells browsers to fetch `http:` resources over `https:`. Two things leave it out:

- `csp.upgradeInsecureRequests: false`, for a site that is itself served over plain http.
- Any artefact declared with an `http:` URL. This is automatic, reported as such, and goes away when you declare `https:` URLs.

## Tightening again

Remove the entry. The change is `csp.tightened`, which is not material and needs no confirmation. Run `consent-kit build` and deploy.

## What not to do

- Do not add `'unsafe-inline'` or `'unsafe-eval'` to `script-src` to make a vendor's snippet work. It removes most of what the policy is for. Move the snippet into a file, or drop the vendor.
- Do not add a wildcard host for a vendor that you could declare as an artefact. A declared artefact is in the documents and the audit; an allowed source is only in the header.
- Do not edit `_headers` or `csp.json`. `build --check` fails on the next run, and the deviation would be invisible to everyone reading `check`.

## Related

- [Content-Security-Policy reference](/docs/manual/reference/content-security-policy/)
- [How consent flows at runtime](/docs/manual/concepts/runtime-consent-flow/)
