CSP for SvelteKit
The SDK works under a strict Content-Security-Policy without unsafe-inline or a nonce.
What you need
The full stylesheet is a normal file served from your own origin by the stylesheet route, and theme colours are applied as CSS variables on the elements. The one inline block is critical.css, which cookieyesHandle writes into every page's <head>, so style-src needs 'self' plus that block's hash. See Critical CSS below. No 'unsafe-inline' and no nonce for styles.
| Directive | Add | When |
|---|---|---|
style-src | 'self' | Always |
script-src | 'self' | Always |
script-src | The vendor's origin, such as https://www.googletagmanager.com | For every tool you load after consent, such as an integration |
connect-src | Your apiUrl origin | mode: "self-hosted" |
frame-src | The embed's origin | Every iframe blocked until consent |
Roll a new policy out with Content-Security-Policy-Report-Only first, and click through the whole flow, including opening the preferences dialog: it exists in the page only while open, so a policy that passes on load can still block it.
Gated scripts
A tool loaded after consent is a real <script src> and needs its origin in script-src, even though nothing loads until the visitor agrees. If the origin is missing, the tag is blocked silently: the console shows the violation, the SDK does not.
Critical CSS
The handle always inlines critical.css, so its hash belongs in style-src. The package exports it as CRITICAL_CSS_HASH, the SHA-256 of @cookieyes/sveltekit/critical.css. svelte.config.js runs in Node, which cannot load the package's entry (it holds .svelte files), so compute the hash from the file there. In SvelteKit 2 it goes in kit.csp:
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
const critical = readFileSync(
createRequire(import.meta.url).resolve("@cookieyes/sveltekit/critical.css"),
"utf8",
);
const criticalHash = `'sha256-${createHash("sha256").update(critical, "utf8").digest("base64")}'`;
export default {
kit: {
csp: { directives: { "style-src": ["self", criticalHash] } },
},
};SvelteKit 3 takes the same csp object as a top-level key of the sveltekit({ ... }) call in vite.config.ts, with the same criticalHash computed there:
export default defineConfig({
plugins: [sveltekit({ csp: { directives: { "style-src": ["self", criticalHash] } } })],
});The handle never rewrites a CSP header and puts no nonce on its <style>: SvelteKit admits styles by hash, not by nonce. With googleConsentMode set, the handle also writes Google's Consent Mode default as an inline <script>, and that script takes the nonce SvelteKit put on its own scripts, so a script-src with a nonce admits it on a page rendered per request. A prerendered page carries its policy in a <meta> tag that comes after these tags, so there the policy that matters is the one your host sends as a header.
For a security review
cookieyesHandle inlines one <style> block, the SDK's critical.css byte for byte, whose hash @cookieyes/sveltekit exports as CRITICAL_CSS_HASH. The full stylesheet loads as a normal file from your own origin, and theme colours are applied as CSS custom properties directly on the elements. style-src therefore needs 'self' and that hash, never 'unsafe-inline'. The SDK's code is part of your SvelteKit bundle. With googleConsentMode set, the handle adds one inline <script>: Google's Consent Mode default, carrying SvelteKit's nonce when the page has one.
Common mistakes
A tool never loads after the visitor accepts.
Its origin is missing from script-src. The console shows the CSP violation.
Consent never reaches your server.
connect-src does not include your apiUrl. The request fails without affecting the banner, and a development build logs a console warning; check the Network tab. The record is kept in the browser and sent again once connect-src allows it.
An embed stays on its placeholder after consent.
Add the embed's origin to frame-src.
A [cookieyes] A style was blocked by your Content-Security-Policy warning appears.
The SDK reports style-src violations it notices, but it cannot cause them itself. Something else on the page uses inline styles.
Next steps
- Critical CSS: the inline block this policy has to allow
- Integrations: the scripts that need their origin allowed