Skip to content
StylingCSP

CSP for Nuxt

Markdown
Loading…

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, and theme colours are applied as CSS variables on the elements. The one inline block is critical.css, which the module 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.

DirectiveAddWhen
style-src'self'Always
script-src'self'Always
script-srcThe vendor's origin, such as https://www.googletagmanager.comFor every tool you load after consent, such as an integration
connect-srcYour apiUrl originmode: "self-hosted"
frame-srcThe embed's originEvery 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 module always inlines critical.css, so its hash belongs in style-src. It is exported ready to paste. The module adds no nonce to its tags, so use the hash even if you use nonces elsewhere.

nuxt.config.ts
import { CRITICAL_CSS_HASH } from "@cookieyes/nuxt";

export default defineNuxtConfig({
  modules: ["@cookieyes/nuxt"],
  routeRules: {
    "/**": {
      // Add the hash to the style-src of the policy you already send.
      headers: { "Content-Security-Policy": `style-src 'self' ${CRITICAL_CSS_HASH}` },
    },
  },
});

With googleConsentMode set, the module also writes Google's Consent Mode default as an inline <script> right after <meta charset>. No hash is exported for it, so script-src must allow that script some other way, for example with the hash of the snippet as your page serves it.

For a security review

The Nuxt module inlines one <style> block, the SDK's critical.css byte for byte, whose hash it 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 Nuxt bundle. With googleConsentMode set, the module adds one inline <script>: Google's Consent Mode default.

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

On this page