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

Abierto
#1,619 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
57/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
next.js, typescript

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
TypeScript
Estrellas
208
Forks
58
Merge medio
2 d 7 h
PR fusionados (30 d)
76

Preparar el entorno

  • Sin Dockerfile ni archivo de Docker Compose
  • Tiene una plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de PostHog/wizard

Todos los issues de PostHog/wizard

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.