Content Security Policy for the Lucida browser SDK

A site with a strict Content-Security-Policy must allow the hosts the SDK talks to. Below, https://lc.example.com stands for your apiHost (the gateway, or...

Embed
Preview of the card
<a href="https://lucidace.com/help/csp"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/08294bcb-a0a2-45f7-a3ca-a88afbda44f4/social-card.svg?v=3e7ae33eec8b" alt="Content Security Policy for the Lucida browser SDK" width="600" height="315"></a>

A site with a strict Content-Security-Policy must allow the hosts the SDK talks to. Below, https://lc.example.com stands for your apiHost (the gateway, or your reverse proxy, see the self-hosting reverse-proxy guide). https://app.example.com stands for uiHost, the Lucida app/API host the toolbar talks to. If you leave uiHost unset, the toolbar uses apiHost instead.

The short version for each framework (install, init, CSP, proxy, consent) is on Install by framework. This page is the full list of directives.

Lucida does not collect CSP violation reports. Report ingestion belongs to Watchtower.

Minimal policy (capture, flags, replay, heatmaps)

script-src  'self';
connect-src 'self' https://lc.example.com;
  • script-src: serve the SDK bundle yourself (npm build or dist/lucida.js). Replay, the toolbar, the experiment editor and resource-center modules are lazy chunks. They load from the same origin as the bundle, so 'self' (or your CDN host) covers them. The SDK never uses eval, so it never needs 'unsafe-eval'.

  • connect-src: covers /v1/capture, /v1/batch, /v1/replay (fetch, plus sendBeacon on page hide), /v1/flags, /v1/flags/stream, /v1/config and /v1/guides. All of these go to apiHost.

  • Hosted Lucida, npm package: every call above goes to apiHost (https://capture.lucidace.com, or your proxy). The npm package never calls https://lucidace.com on its own. That host is needed only (1) in script-src and connect-src for the classic <script src="https://lucidace.com/lucida.js"> loader and the modules and manifest.json it fetches from there, and (2) in connect-src on pages where your team opens the toolbar (uiHost). The embed manifest (/.well-known/lucida-embed.json) has one surface per install: npm (only connect-src https://capture.lucidace.com), browser (the classic snippet: both hosts) and toolbar (https://lucidace.com). Its storage list has one row per name and store, so it is longer than the list of names in storage.json; both come from the same file, and storage.json is the source.

  • worker-src: not needed for DOM replay, which compresses on the main thread. Only canvas recording (replay with canvas on and no canvas masking) starts one blob: Web Worker, from the recorder's bundled rrweb code. If you record canvas under a strict CSP, add worker-src 'self' blob:. lucida doctor --site <url> warns when replay is on and the CSP gives blob: workers no source. It also fails when the site's origin is not in capture_allowed_origins.

Guides, web experiments and the toolbar

style-src   'self' 'nonce-{RANDOM}';
img-src     'self' https:;
media-src   https:;
frame-src   https://www.youtube-nocookie.com https://www.youtube.com https://player.vimeo.com https://www.loom.com https://fast.wistia.net;
connect-src 'self' https://lc.example.com https://app.example.com;
  • style-src: guides (inside a shadow root) and the anti-flicker veil of web experiments each inject a <style> element. Pass your per-response nonce to the SDK and every one of them carries it:

    init("lc_pk_...", { nonce: document.querySelector("meta[name=csp-nonce]")?.content });

    Without nonce these elements need 'unsafe-inline'. Note that browsers ignore 'unsafe-inline' once a nonce or hash is listed, so a policy with a nonce always needs the nonce option. The toolbar, the consent banner and appearance tokens style through CSSOM (element.style), which CSP does not restrict.

  • Web-experiment custom code (per-variant page CSS and JS, P-H5): page CSS is a <style> and page JS a <script>, both with the configured nonce, so a nonce-based script-src / style-src admits them without 'unsafe-inline'. Page JS runs only when the project approved it (experiment.custom_code approval policy key: the API stamps approved on the change at start, and any value sent by a client is overwritten) and the site opts in with visualChanges: { allowCustomJs: true } (default false). Inserted HTML is sanitized in the browser (no scripts, styles, frames, forms, SVG or on* handlers; links http(s), mailto, tel or relative; images http(s), relative or raster data:) and checked again by the API on save. Swapped images uploaded from the visual editor are served from {LC_PUBLIC_URL}/v1/experiment-assets/...; add that host to img-src.

  • img-src / media-src: guide media blocks load from the https URLs authored in the guide. The SDK drops non-https media. To be stricter, list your asset hosts instead of https:.

  • frame-src: guide embed blocks frame only the five hosts above, and the iframes are sandboxed. Omit any you do not use.

  • connect-src: the toolbar calls uiHost (/v1/toolbar/exchange and the project API). Add uiHost only for pages where your team opens the toolbar.

  • script-src with modules: the classic script's modules option loads ESM files from assetHost (see Pinning the SDK version). Add that host to script-src. A strict nonce-based script-src with 'strict-dynamic' allows them, because the loader that imports them is trusted.

Checking

Open the page with the toolbar or ?__lc_debug=1, then look in the browser console for Refused to connect or Refused to load messages. Each message names the directive that blocked the request.

In debug mode the SDK adds its own hints (console only, nothing is sent):

  • CSP hint: this page's Content-Security-Policy blocks inline <style>: a <meta http-equiv="Content-Security-Policy"> policy restricts inline styles and no nonce is set. A policy sent as an HTTP header is invisible to scripts, so this check only sees meta policies.

  • CSP hint: an inline style was blocked by style-src...: the browser refused a style. Set nonce, or check that it matches the nonce of the current response.

lucida doctor --site <url> checks the live page's policy against the loader script and the API host.

Capturing violations as events

Set captureCspViolations: true (or { maxPerPage: 50 }) to send a $csp_violation event for each violation the browser reports on the page: $csp_directive, $csp_blocked_uri, $csp_source_file (origin and path only, never a query string), $csp_line, $csp_column and $csp_disposition. The code sample and the referrer are never sent. Identical violations count once and a page sends at most 20 by default. It is off unless you set it, and respects consent like every other event. Blocked-resource reports from report-to endpoints and the full report pipeline stay in Watchtower.

Updated