: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): keep the admin policy off app pages when the admin portal is off (#104760)
Tom Piccirello28735bdc4687
diff --git a/posthog/middleware.py b/posthog/middleware.pyindex 1559a7e62f7d..53b0622766ad 100644--- a/posthog/middleware.py+++ b/posthog/middleware.py@@ -1438,7 +1438,10 @@ class CSPMiddleware:)return response- is_admin_view = request.path.startswith("/admin/")+ # With the admin portal off, Django admin is not mounted and `/admin/` reaches the app catch-all.+ # Choosing the admin policy by path alone would then enforce it on the app, and it has no+ # connect-src, so the app's own analytics and feature flag requests are refused.+ is_admin_view = getattr(settings, "ADMIN_PORTAL_ENABLED", False) and request.path.startswith("/admin/")if is_admin_view:django_loginas_inline_script_hash = "sha256-2bSkJXtgXFhxZUhgXzWsEsKImxJEQsqjns0vi3KiSrI="csp_parts = [diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.pyindex e326d020bc89..120e748b2864 100644--- a/posthog/test/test_middleware.py+++ b/posthog/test/test_middleware.py@@ -2347,8 +2347,8 @@ class TestCSPMiddleware(APIBaseTest):def test_admin_pages_enforce_the_policy_and_follow_the_reporting_default(self, _name, overrides, expects_reporting):# The admin policy is enforced, not report-only, and builds its own Reporting-Endpoints# header, so it can drift from the app policy unnoticed. A non-staff request redirects but- # still carries that policy, because the middleware picks its branch by path.- with override_settings(CSP_REPORT_ENDPOINT=None, **overrides):+ # still carries that policy, because the middleware picks its branch by path once the portal is on.+ with override_settings(CSP_REPORT_ENDPOINT=None, ADMIN_PORTAL_ENABLED=True, **overrides):response = self.client.get("/admin/")policy = response["Content-Security-Policy"]# Only the admin policy forbids framing outright; the non-HTML fallback is default-src alone.@@ -2368,6 +2368,15 @@ class TestCSPMiddleware(APIBaseTest):assert "report-to" not in policyassert "Reporting-Endpoints" not in response+ @override_settings(ADMIN_PORTAL_ENABLED=False)+ def test_admin_path_gets_the_app_policy_when_the_portal_is_off(self):+ # Without the portal, `/admin/` is an app page, and the app keeps running in that document+ # after it navigates away. The admin policy there would refuse every request the app makes+ # to another origin.+ response = self.client.get("/admin/")+ assert "frame-ancestors 'none'" not in response["Content-Security-Policy"]+ assert "connect-src 'self'" in response["Content-Security-Policy-Report-Only"]+@parameterized.expand([(