Skip to content

: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.

chore: tell agents the CSP is enforced (#101380)

Tom Piccirellof53f4a54a07c
diff --git a/.agents/security.md b/.agents/security.md
index 8d9bac305950..692e80cb45a9 100644
--- a/.agents/security.md
+++ b/.agents/security.md
@@ -157,3 +157,43 @@ semgrep --test .semgrep/rules/security/
# Or via Docker
docker run --rm -v "${PWD}:/src" semgrep/semgrep semgrep --test /src/.semgrep/rules/security/
```
+
+## Content Security Policy
+
+`CSPMiddleware` in `posthog/middleware.py` attaches a policy to every HTML response.
+Treat it as enforced.
+A refused resource produces no user-visible error, so the feature simply does not work, and the only signal is a `$csp_violation` event in project 2.
+
+Three policies exist, and a change lands in whichever one covers the page:
+
+- **The app policy** governs every SPA page. It is enforced per user behind the `csp-enforce-app-policy` flag, and report-only otherwise.
+- **The admin policy** governs `/admin/`. It is enforced for every staff member, with no flag, so a mistake here breaks admin immediately.
+- **A view may set its own policy.** The canvas artifact and the workflow asset endpoint do this to sandbox untrusted HTML. `CSPMiddleware` returns a response that already carries the header unchanged, so do not expect the app policy on those documents.
+
+### What the app policy forbids
+
+| You want to | The policy says |
+| ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| Load a script from a new origin | Only `'self'`, our own CDNs, and a named list. Stripe, Turnstile and Unlayer are there because each vendor requires its own origin. Prefer serving the file yourself. |
+| Call `eval` or `new Function` | Refused. `wasm-unsafe-eval` permits WebAssembly compilation only. |
+| Start a worker | `'self'` and `blob:` only. Never `data:`: a `data:` worker body is code the policy cannot inspect. |
+| Load a font from a CDN | Self-host it instead. A CDN font already caused a regression when the origin was removed. |
+| Submit a form | `'self'` plus the admin OAuth origin. The directive is checked on every hop of a redirect chain, so a same-origin action that redirects off-origin is refused. |
+| Set `<base href>` | Refused. Chromium judges the assignment even on a detached document. |
+
+### Traps that have already cost us
+
+- **A dependency can carry the origin.** Grep `node_modules` for the literal host before you call an origin unused. A font CDN was removed on the belief nothing used it, and a transitive dependency defaulted to it.
+- **posthog-js wraps `fetch`, so an extension's request is reported with our bundle as the source file.** Never conclude a violation is ours from the source file alone. Corroborate with the blocked URL and the document.
+- **A CSP report names the original URL, not the hop that failed.** For `form-action` and any redirected navigation, the blocked URL can be same-origin while the refusal happened later in the chain.
+- **The Vite dev server cannot reproduce any of this.** It injects CSS as `<style>` blocks and emits absolute asset URLs, so paths that depend on a stylesheet's own href never run. Only the esbuild build matches production.
+
+### Checking a change
+
+Add the source to the policy that covers the document, which is not always `CSPMiddleware`.
+An app or admin page takes the matching list in `CSPMiddleware`.
+A canvas artifact takes `artifact_csp()` in `products/canvas/backend/contract.py`, and a workflow message asset takes the header its endpoint sets in `products/workflows/backend/api/hog_flow.py`.
+`CSPMiddleware` returns a view-set header untouched, so widening the app policy does nothing for those two.
+Say why the source is needed in a comment either way, then run `posthog/test/test_middleware.py::TestCSPMiddleware`.
+To see what the policy currently blocks, query `$csp_violation` events in project 2.
+Filter to the current policy text and exclude browser extensions on both the source file and the blocked URL, or the result is mostly noise.
diff --git a/AGENTS.md b/AGENTS.md
index 8168ee5ab6d5..168f271b981d 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -144,6 +144,13 @@ Do not add new `INTERNAL_API_SECRET` callers.
Read [.agents/security.md](.agents/security.md) before touching auth, secrets, service-to-service calls, raw SQL, or HogQL string building — it covers least privilege, the injection rules, and how to respond when semgrep flags your code.
`.semgrep/rules/security/` is the enforced set; run `semgrep --config .semgrep/rules/security/ .` to check a change locally.
+**Treat the Content Security Policy in `CSPMiddleware` as enforced.**
+A resource from an origin the policy does not name is refused, and the page shows the user no error, so the feature fails silently.
+Loading a script, font, stylesheet, image, frame, or `fetch` target from a new external origin needs that directive widened in the same PR.
+`eval` and `new Function` never run, because the policy grants `wasm-unsafe-eval` and nothing more.
+In a Django template an inline `<script>` needs `nonce="{{ request.csp_nonce }}"`, and an inline `onclick` or `onsubmit` attribute cannot be made to work at all — put the handler in a nonce'd block instead.
+Read [.agents/security.md](.agents/security.md#content-security-policy) for the directive list and the traps before you add any of these.
+
## Architecture guidelines
Each rule is tagged with what catches a violation.