:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(csp): allow blob workers, and pin the reason they are safe (#98051)
Tom Piccirello881a63f0ae2f
diff --git a/.github/workflows/ci-security.yaml b/.github/workflows/ci-security.yamlindex dc7e0f3935b8..fc490011e07c 100644--- a/.github/workflows/ci-security.yaml+++ b/.github/workflows/ci-security.yaml@@ -85,6 +85,7 @@ jobs:js:- 'frontend/**'- 'nodejs/**'+ - 'packages/**'- 'services/agent-proxy/**'- 'services/integration-service/**'- 'services/mcp/**'@@ -106,6 +107,7 @@ jobs:- '!frontend/**'- '!livestream/**'- '!nodejs/**'+ - '!packages/**'- '!posthog/**'- '!products/**'- '!rust/**'@@ -293,6 +295,7 @@ jobs:--config p/trailofbits--include /frontend--include /nodejs+ --include /packages--include /services/agent-proxy--include /services/integration-service--include /services/mcp@@ -378,6 +381,7 @@ jobs:--exclude /frontend--exclude /livestream--exclude /nodejs+ --exclude /packages--exclude /posthog--exclude /products--exclude /rustdiff --git a/.semgrep/rules/security/no-dynamic-worker-body.test.ts b/.semgrep/rules/security/no-dynamic-worker-body.test.tsnew file mode 100644index 000000000000..c1decf1a8caa--- /dev/null+++ b/.semgrep/rules/security/no-dynamic-worker-body.test.ts@@ -0,0 +1,78 @@+// @ts-nocheck+// Test fixture for the no-dynamic-worker-body rule.++// ruleid: no-dynamic-worker-body+const a = new Worker(URL.createObjectURL(new Blob([source], { type: 'application/javascript' })))++// ruleid: no-dynamic-worker-body+const b = new SharedWorker(URL.createObjectURL(new Blob([userSuppliedCode])))++// ruleid: no-dynamic-worker-body+const c = new Worker(window.URL.createObjectURL(new Blob([template, payload])), { name: 'thing' })++// ruleid: no-dynamic-worker-body+const d = new Worker('data:text/javascript;charset=utf-8,' + encodeURIComponent(source))++// ruleid: no-dynamic-worker-body+const e = new Worker(`data:text/javascript,${encodeURIComponent(body)}`)++// Revoking an object URL needs it held in a variable, so this is the shape a developer+// writes when the blob worker is written correctly.+const objectUrl = URL.createObjectURL(new Blob([source]))+// ruleid: no-dynamic-worker-body+const k = new Worker(objectUrl)++// ruleid: no-dynamic-worker-body+const l = new Worker(URL.createObjectURL(blob))++// ruleid: no-dynamic-worker-body+const m = new Worker(globalThis.URL.createObjectURL(new Blob([source])))++const dataUrl = 'data:text/javascript,' + encodeURIComponent(source)+// ruleid: no-dynamic-worker-body+const n = new Worker(dataUrl)++const templatedDataUrl = `data:text/javascript,${encodeURIComponent(body)}`+// ruleid: no-dynamic-worker-body+const o = new Worker(templatedDataUrl)++// A same-origin file. This is the shape every first-party worker in the app uses, and it+// needs no blob: allowance.+// ok: no-dynamic-worker-body+const f = new Worker(new URL('./hogqlParser.worker.ts', import.meta.url), { type: 'module' })++// ok: no-dynamic-worker-body+const g = new Worker('/static/decompressionWorker.js', { type: 'module' })++// ok: no-dynamic-worker-body+const h = new Worker(WORKER_URL, { type: 'module' })++// Nothing in scope puts a blob or a data URL in this variable, so there is no flow to+// report. A worker URL that arrives from outside the file is not something this rule can+// judge.+// ok: no-dynamic-worker-body+const i = new Worker(precomputedUrl)++// A same-origin path assembled from a value. The scheme decides, not the concatenation+// or the interpolation, so all three of these stay clean.+// ok: no-dynamic-worker-body+const p = new Worker('/static/workers/' + workerName, { type: 'module' })++// ok: no-dynamic-worker-body+const q = new Worker(`/static/workers/${workerName}.js`, { type: 'module' })++// ok: no-dynamic-worker-body+const r = new Worker(`${STATIC_BASE}/decompressionWorker.js`, { type: 'module' })++const interpolatedPath = `/static/workers/${workerName}.js`+// ok: no-dynamic-worker-body+const s = new Worker(interpolatedPath, { type: 'module' })++// Only the URL argument counts. A blob URL somewhere else in the call is not the worker+// body.+// ok: no-dynamic-worker-body+const t = new Worker('/static/decompressionWorker.js', { name: URL.createObjectURL(debugBlob) })++// Object URLs for downloads and previews are untouched: nothing runs them as code.+// ok: no-dynamic-worker-body+const j = URL.createObjectURL(new Blob([csv], { type: 'text/csv' }))diff --git a/.semgrep/rules/security/no-dynamic-worker-body.yaml b/.semgrep/rules/security/no-dynamic-worker-body.yamlnew file mode 100644index 000000000000..edc1351d278d--- /dev/null+++ b/.semgrep/rules/security/no-dynamic-worker-body.yaml@@ -0,0 +1,82 @@+rules:+ - id: no-dynamic-worker-body+ mode: taint+ message: |+ Worker body built from a value rather than a constant. The app policy allows+ `worker-src 'self' blob:`, and that allowance rests on every blob worker body+ being fixed at build time.++ A blob URL is safe to permit because only script can mint one, so an attacker+ needs script execution before they can use it. That argument fails if our own+ code turns attacker-influenced data into a worker body: a value the attacker+ controls then becomes code running with this origin's network credentials,+ reachable from a data-injection alone.++ Build the worker from a real file instead:++ new Worker(new URL('./thing.worker.ts', import.meta.url), { type: 'module' })++ `frontend/src/scenes/data-warehouse/editor/hogqlParserWorkerManager.ts` and+ `frontend/src/scenes/session-recordings/player/snapshot-processing/DecompressionWorkerManager.ts`+ both do this. A same-origin file needs no `blob:` allowance at all.++ A bundled library that ships its own blob worker is fine, because its body is a+ compile-time constant. This rule covers first-party code.++ The rule follows an object URL or a `data:` URL through the assignments in one+ function, so it catches the shapes we write. It does not follow a URL back out+ of a helper function that returns one. Treat it as a check on first-party code,+ not as a proof.++ If a dynamic body is genuinely unavoidable, put `// nosemgrep: no-dynamic-worker-body`+ on the line before it with a reason, and state where the body comes from and why+ no input can reach it.+ severity: ERROR+ languages: [typescript, javascript]+ paths:+ include:+ - frontend/src+ - products+ - common+ # The leading slash pins this to the repository root. The entries above+ # stay unanchored because anchoring them would drop directories this rule+ # scans today, such as `nodejs/src/common`.+ - /packages+ exclude:+ - '**/*.test.ts'+ - '**/*.test.tsx'+ - '**/*.spec.ts'+ pattern-sources:+ - pattern-either:+ # Every object URL is minted at run time, whatever the receiver is spelled+ # as: `URL`, `window.URL`, `globalThis.URL`, or a destructured alias.+ - pattern: createObjectURL(...)+ - pattern: $RECEIVER.createObjectURL(...)+ # A `data:` or `blob:` URL concatenated from a value. The scheme has to be+ # in the leading literal, so `'/static/' + name` stays clean.+ - patterns:+ - pattern: $SCHEME + $X+ - metavariable-regex:+ metavariable: $SCHEME+ regex: '^[''"`](data|blob):'+ # A `data:` or `blob:` URL interpolated from a value. A template-literal+ # pattern cannot reach the fixed part, so bind the whole literal and read+ # the scheme off its text. The `${` keeps a constant body out.+ - patterns:+ - pattern: $TEMPLATE+ - metavariable-regex:+ metavariable: $TEMPLATE+ regex: '^`(data|blob):[\s\S]*\$\{'+ # Only the URL argument is a sink. Without this, an interpolated worker name in the+ # options object matches too.+ pattern-sinks:+ - patterns:+ - pattern-either:+ - pattern: new Worker($URL, ...)+ - pattern: new SharedWorker($URL, ...)+ - focus-metavariable: $URL+ metadata:+ category: security+ confidence: MEDIUM+ references:+ - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/worker-srcdiff --git a/posthog/middleware.py b/posthog/middleware.pyindex 57cf2715a110..4be8ce5ceb77 100644--- a/posthog/middleware.py+++ b/posthog/middleware.py@@ -1261,7 +1261,21 @@ class CSPMiddleware:# parses with a WebAssembly build, so both break without it.f"script-src 'self' 'nonce-{nonce}' 'wasm-unsafe-eval' {resource_url} https://*.i.posthog.com",f"font-src 'self' {resource_url} https://app-static.eu.posthog.com https://app-static-prod.posthog.com https://fonts.gstatic.com https://cdn.jsdelivr.net",- "worker-src 'self'",+ # `blob:` grants nothing to an attacker who cannot already run script, because only+ # script can mint a blob URL, and a worker started from one inherits this policy+ # rather than escaping it. The ServiceWorker spec rejects `blob:` on its own, so+ # this cannot register a persistent worker either.+ #+ # The reasoning holds only while every blob worker body is a compile-time constant.+ # `no-dynamic-worker-body` in .semgrep/rules/security checks first-party code for+ # that. It follows an object URL or a `data:` URL into a worker constructor through+ # the assignments in one function, so it catches the shapes we write rather than+ # every possible one.+ #+ # posthog-js builds its rrweb recorder worker from a blob, and PixiJS builds two+ # ImageBitmap workers the same way. Do not add `data:`: the recorder falls back to a+ # data URL only when blob fails, so allowing blob stops those attempts.+ "worker-src 'self' blob:","child-src 'none'","object-src 'none'","media-src https://res.cloudinary.com",