The wizard's CSP change leaves the assets host out of connect-src, so session replay and surveys never start

Aperta
#1,619 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
next.js, typescript

Direzione di ricerca

The issue points to the CSP guidance and review check associated with #1439 (cm#245, cm#278), but does not name their files. Start by locating those guidance and review-check entries, then trace how they build or validate connect-src. Done means the assets host is included there and the review check accepts it; verify against the reported /array/<token>/config request.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I ran the setup wizard (3.0.0) on my Next.js 16 app, which builds a nonce-based Content-Security-Policy in src/server/csp.ts. The policy already allowed the API host in connect-src, and the wizard added the assets host to script-src and a worker-src, like this.

- script-src 'self' 'nonce-…' 'strict-dynamic'
+ script-src 'self' 'nonce-…' 'strict-dynamic' https://eu-assets.i.posthog.com
  connect-src 'self' https://eu.i.posthog.com
+ worker-src 'self' blob:

Events were coming in, so everything looked fine, but session replay, surveys, web vitals and dead clicks never started. posthog-js 1.454.2 loads its remote config with fetch() from the assets host (https://eu-assets.i.posthog.com/array/<token>/config), so it falls under connect-src, and the browser blocks it without anything showing up in the console. The run summary didn't mention the CSP either.

I think this comes from the CSP guidance in #1439 (cm#245, cm#278), which puts the API host in connect-src and the assets host only in script-src.

To reproduce it I made one page without any framework, served with the same directives.

// `npm i posthog-js && node repro.mjs`, then open http://localhost:4777 (wizard's policy) and http://localhost:4777/nocsp
import { readFileSync } from "node:fs";
import { createServer } from "node:http";

const TOKEN = process.env.POSTHOG_TOKEN ?? "<project token>";
const nonce = "dGVzdG5vbmNl";
const csp = [
  "default-src 'self'",
  `script-src 'self' 'nonce-${nonce}' 'strict-dynamic' https://eu-assets.i.posthog.com`,
  "connect-src 'self' https://eu.i.posthog.com",
  "worker-src 'self' blob:",
].join("; ");
const page = `<!doctype html>
<script nonce="${nonce}">
  document.addEventListener("securitypolicyviolation", (e) => console.log("blocked", e.violatedDirective, e.blockedURI));
</script>
<script type="module" nonce="${nonce}">
  import posthog from "/module.js";
  posthog.init("${TOKEN}", { api_host: "https://eu.i.posthog.com", defaults: "2026-08-30" });
  setTimeout(() => console.log("recording:", posthog.sessionRecordingStarted()), 8000);
</script>`;
const sdk = readFileSync("node_modules/posthog-js/dist/module.js");

createServer((req, res) => {
  if (req.url === "/module.js") {
    res.writeHead(200, { "content-type": "text/javascript" });
    return res.end(sdk);
  }
  const headers = { "content-type": "text/html" };
  if (req.url !== "/nocsp") headers["content-security-policy"] = csp;
  res.writeHead(200, headers);
  res.end(page);
}).listen(4777);

With a project that has session replay on, the console shows this.

/        blocked connect-src https://eu-assets.i.posthog.com/array/phc_…/config
         recording: false
/nocsp   recording: true

With the wizard's policy surveys.js, posthog-recorder.js, dead-clicks-autocapture.js and web-vitals-with-attribution.js are never requested. Without the policy all of them load. Events and /flags/ go through in both cases.

The fix I would suggest is to add the assets host to connect-src as well, both in the guidance and in the review check. Also, with 'strict-dynamic' modern browsers ignore hosts in script-src, so adding the assets host there doesn't really change anything. The lazy-loaded scripts still load because a nonced script inserts them (that's why exception-autocapture.js loaded with the wizard's policy).

For now I worked around it by sending everything through a same-origin rewrite (/relay/* to eu.i.posthog.com and eu-assets.i.posthog.com) and keeping connect-src 'self'.

I ran it with wizard 3.0.0, Next.js 16.4.6 (Turbopack), posthog-js 1.454.2, EU Cloud, Chromium, macOS arm64 and Node 25.2.1.

Lingua principale
TypeScript
Stelle
208
Fork
58
Merge medio
2g 7h
PR unite (30g)
76

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PostHog/wizard

Tutte le issue di PostHog/wizard

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.