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.

fix(csp): narrow the app default-src to the bundle host (#104160)

Tom Piccirellob1452cad694f
diff --git a/posthog/middleware.py b/posthog/middleware.py
index 19da3b70b082..eb1fbef5f563 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -10,7 +10,7 @@ from datetime import UTC, datetime, timedelta
from enum import StrEnum
from ipaddress import ip_address, ip_network
from typing import Optional, cast
-from urllib.parse import urlencode
+from urllib.parse import urlencode, urlsplit
from django.conf import settings
from django.contrib.auth import BACKEND_SESSION_KEY, logout
@@ -1468,12 +1468,15 @@ class CSPMiddleware:
frame_ancestors += " http://localhost:8001"
connect_debug_url = "ws://localhost:8234" if settings.DEBUG or settings.TEST else ""
+ js_url = urlsplit(settings.JS_URL)
+ bundle_origin = f"{js_url.scheme}://{js_url.netloc}" if js_url.scheme and js_url.netloc else ""
csp_parts = [
# Firefox checks <link rel="modulepreload"> against default-src instead of script-src,
- # so without the static host it refuses the preloads index.html emits for the boot
- # chain. The fetch directives below each set their own sources, so only a load a
- # browser cannot map to one of them falls back to this list.
- f"default-src 'self' {resource_url}",
+ # so without the bundle host it refuses the preloads index.html emits for the boot
+ # chain. Every preload href starts with JS_URL, so its origin is the one host needed.
+ # The fetch directives below each set their own sources, so only a load a browser
+ # cannot map to one of them falls back to this list.
+ f"default-src 'self' {bundle_origin}".rstrip(),
f"style-src 'self' 'unsafe-inline' {resource_url} https://fonts.googleapis.com",
# 'wasm-unsafe-eval' permits WebAssembly compilation and nothing else. It is not
# 'unsafe-eval': it does not permit eval() or the Function constructor. Compiling a
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index ab3e82874a4e..0e463675c8e9 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -2182,11 +2182,30 @@ class TestCSPMiddleware(APIBaseTest):
response = self.client.get("/")
assert "frame-src 'self' https:" in response["Content-Security-Policy-Report-Only"]
- def test_app_policy_lets_firefox_preload_the_app_bundle(self):
+ @parameterized.expand(
+ [
+ ("local", {}, "default-src 'self' http://localhost:8234;"),
+ # The bundle host alone. The rest of the policy names every PostHog subdomain, and a
+ # fallback that broad admits whatever a new directive forgets to restrict.
+ (
+ "cloud",
+ {
+ "TEST": False,
+ "DEBUG": False,
+ "CLOUD_DEPLOYMENT": "US",
+ "SITE_URL": "https://us.posthog.com",
+ "JS_URL": "https://app-static-prod.posthog.com",
+ },
+ "default-src 'self' https://app-static-prod.posthog.com;",
+ ),
+ ]
+ )
+ def test_app_policy_lets_firefox_preload_the_app_bundle(self, _name, overrides, expected):
# Firefox judges <link rel="modulepreload"> by default-src, not script-src. A default-src of
# 'self' alone refuses every preload index.html emits for the boot chain.
- response = self.client.get("/")
- assert "default-src 'self' http://localhost:8234" in response["Content-Security-Policy-Report-Only"]
+ with override_settings(**overrides):
+ response = self.client.get("/")
+ assert expected in response["Content-Security-Policy-Report-Only"]
def test_replay_player_frame_serves_the_mount_node_without_a_session(self):
# Shared recordings render the player for logged-out viewers.