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(csp): keep embeddable documents report-only under enforcement (#98025)

Tom Piccirello7959856ef63c
diff --git a/posthog/middleware.py b/posthog/middleware.py
index 4be8ce5ceb77..6480f4026793 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -29,6 +29,7 @@ from django.utils.deprecation import MiddlewareMixin
from django.utils.http import http_date, url_has_allowed_host_and_scheme
import structlog
+import posthoganalytics
from django_prometheus.middleware import Metrics
from loginas.utils import is_impersonated_session, restore_original_login
from opentelemetry import trace
@@ -1161,6 +1162,66 @@ def csp_report_endpoint(**params: str) -> str:
# prefix match would also hand the app document this policy and stop it from starting.
REPLAY_PLAYER_FRAME_PATH = "/replay_player_frame/index.html"
+# The app policy names only PostHog origins in `frame-ancestors`. Enforcing it on these paths stops
+# every embedded dashboard, shared link and survey from rendering on a customer's site.
+#
+# The list follows `posthog/urls.py`. The Contour ingress keeps a similar list in
+# `charts/argocd/contour-ingress/values/values.{dev,prod-us,prod-eu}.yaml`, which omits
+# `/interview/` and the bare `/exporter`. Sync to the URL patterns, not to that list.
+EMBEDDABLE_PATH_PREFIXES = (
+ "/shared_dashboard/",
+ "/shared/",
+ "/embedded/",
+ "/interview/",
+ "/exporter/",
+ "/external_surveys/",
+)
+EMBEDDABLE_PATHS = frozenset({"/render_query", "/exporter"})
+
+
+def is_embeddable_document(path: str) -> bool:
+ return path in EMBEDDABLE_PATHS or path.startswith(EMBEDDABLE_PATH_PREFIXES)
+
+
+CSP_ENFORCE_APP_POLICY_FLAG = "csp-enforce-app-policy"
+
+
+def csp_enforcement_enabled(request: HttpRequest) -> bool:
+ user = getattr(request, "user", None)
+ distinct_id = getattr(user, "distinct_id", None) if user is not None and user.is_authenticated else None
+ if user is None or not distinct_id:
+ # An anonymous page has nobody to bucket, so login, signup and the OAuth pages keep the
+ # report-only header until enforcement covers everyone.
+ return False
+ try:
+ # Local evaluation only. A network call here would sit in the path of every HTML response,
+ # and an unevaluable flag returns None, which leaves the policy report-only.
+ #
+ # Local evaluation holds the flag's conditions but not the person's properties, so a
+ # condition on `email` cannot resolve unless the caller supplies it. Without this the
+ # staff-only rollout every other flag here uses would return None and enforce nothing.
+ return bool(
+ posthoganalytics.feature_enabled(
+ CSP_ENFORCE_APP_POLICY_FLAG,
+ distinct_id,
+ person_properties={"email": user.email} if user.email else {},
+ only_evaluate_locally=True,
+ )
+ )
+ except Exception:
+ # A failed lookup and a deliberate opt-out both leave the policy report-only. The rollout
+ # needs to tell them apart.
+ logger.warning("csp.enforcement_flag_check_failed_defaulting_off", exc_info=True)
+ return False
+
+
+def app_csp_header_name(request: HttpRequest) -> str:
+ if is_embeddable_document(request.path):
+ return "Content-Security-Policy-Report-Only"
+ if csp_enforcement_enabled(request):
+ return "Content-Security-Policy"
+ return "Content-Security-Policy-Report-Only"
+
class CSPMiddleware:
def __init__(self, get_response):
@@ -1240,7 +1301,12 @@ class CSPMiddleware:
f'posthog="{admin_report_endpoint}", default="{admin_report_endpoint}"'
)
response.headers["Content-Security-Policy"] = "; ".join(csp_parts)
- elif getattr(response, "_posthog_canvas_artifact", False) and "Content-Security-Policy" in response.headers:
+ elif "Content-Security-Policy" in response.headers:
+ # The view picked this policy for this document: a canvas artifact runs untrusted code,
+ # and the workflow asset endpoint sandboxes captured email HTML. The app policy would
+ # drop that sandbox and impose a frame-ancestors list the app's own origin does not
+ # match. Adding it report-only is no better, because these documents never aim to
+ # satisfy it, so each load would report a violation of a policy we chose not to apply.
return response
else:
resource_url = "https://*.posthog.com"
@@ -1316,7 +1382,7 @@ class CSPMiddleware:
# Browsers only deliver crash reports to the endpoint named `default`; the CSP
# `report-to posthog` directive keeps routing violations to `posthog`.
response.headers["Reporting-Endpoints"] = f'posthog="{report_endpoint}", default="{report_endpoint}"'
- response.headers["Content-Security-Policy-Report-Only"] = "; ".join(csp_parts)
+ response.headers[app_csp_header_name(request)] = "; ".join(csp_parts)
return response
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index d129be27d324..b5834de70890 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -13,6 +13,7 @@ from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.test import (
Client as DjangoClient,
RequestFactory,
+ SimpleTestCase,
)
from django.urls import reverse
@@ -25,14 +26,13 @@ from social_core.exceptions import AuthCanceled, AuthFailed, AuthMissingParamete
from posthog.api.test.test_organization import create_organization
from posthog.api.test.test_team import create_team
-from posthog.middleware import CSPMiddleware, per_request_logging_context_middleware
+from posthog.middleware import CSPMiddleware, app_csp_header_name, per_request_logging_context_middleware
from posthog.models.organization import Organization
from posthog.models.team import Team
from posthog.models.user import User
from posthog.settings import SITE_URL
from products.actions.backend.models.action import Action
-from products.canvas.backend.artifacts import CANVAS_ARTIFACT_RESPONSE_MARKER
from products.cohorts.backend.models.cohort import Cohort
from products.dashboards.backend.models.dashboard import Dashboard
from products.feature_flags.backend.models.feature_flag import FeatureFlag
@@ -1940,38 +1940,15 @@ class TestCSPMiddleware(APIBaseTest):
assert "Content-Security-Policy-Report-Only" in response
assert "Content-Security-Policy" not in response
- @parameterized.expand(
- [
- ("custom_policy", "/", False, "default-src 'self'", True),
- ("canvas", "/", True, "sandbox allow-scripts; default-src 'none'", False),
- ("custom_admin", "/admin/", False, "default-src *", True),
- ("marked_admin", "/admin/", True, "default-src *", True),
- ("marker_without_policy", "/", True, None, True),
- ]
- )
- @override_settings(CLOUD_DEPLOYMENT="US")
- def test_html_response_with_view_managed_csp(
- self, _name: str, path: str, canvas_artifact: bool, policy: str | None, expects_reporting: bool
- ) -> None:
- def view(_request: HttpRequest) -> HttpResponse:
- response = HttpResponse("<html><body>artifact</body></html>", content_type="text/html; charset=utf-8")
- if policy is not None:
- response["Content-Security-Policy"] = policy
- if canvas_artifact:
- setattr(response, CANVAS_ARTIFACT_RESPONSE_MARKER, True)
- return response
-
- response = CSPMiddleware(view)(RequestFactory().get(path))
+ @patch("posthog.middleware.posthoganalytics.feature_enabled", return_value=True)
+ def test_enforcement_reaches_an_app_page_but_not_an_embeddable_one(self, _mock_flag):
+ # The wiring guard for app_csp_header_name. The matrix of paths lives in
+ # TestAppCspHeaderName, which needs no database.
+ assert "Content-Security-Policy" in self.client.get("/")
- if path == "/admin/":
- assert "frame-ancestors 'none'" in response["Content-Security-Policy"]
- assert "default-src *" not in response["Content-Security-Policy"]
- elif policy is not None:
- assert response["Content-Security-Policy"] == policy
- else:
- assert "Content-Security-Policy" not in response
- assert ("Content-Security-Policy-Report-Only" in response) == (expects_reporting and path != "/admin/")
- assert ("Reporting-Endpoints" in response) == expects_reporting
+ embedded = self.client.get("/shared/notarealtoken")
+ assert "Content-Security-Policy" not in embedded
+ assert "frame-ancestors" in embedded["Content-Security-Policy-Report-Only"]
@override_settings(CLOUD_DEPLOYMENT="US") # As PostHog Cloud
def test_html_response_declares_default_reporting_endpoint_with_distinct_id(self):
@@ -2514,3 +2491,108 @@ class TestPerRequestLoggingContextMiddlewareMcpHeaders(APIBaseTest):
assert "mcp_session_id" not in ctx
assert "mcp_conversation_id" not in ctx
span.set_attribute.assert_not_called()
+
+
+class TestAppCspHeaderName(SimpleTestCase):
+ def _request(
+ self, path: str, *, distinct_id: str | None = "abc", email: str = "[email protected]"
+ ) -> HttpRequest:
+ request = RequestFactory().get(path)
+ request.user = MagicMock(is_authenticated=distinct_id is not None, distinct_id=distinct_id, email=email)
+ return request
+
+ @parameterized.expand(
+ [
+ ("shared_dashboard", "/shared_dashboard/abc123"),
+ ("shared", "/shared/abc123"),
+ ("embedded", "/embedded/abc123"),
+ ("interview", "/interview/abc123"),
+ ("exporter_with_token", "/exporter/abc123"),
+ ("exporter_render", "/exporter"),
+ ("render_query", "/render_query"),
+ ("external_survey", "/external_surveys/019efb7e-0672-0000-729b-e234586f6177"),
+ ]
+ )
+ @patch("posthog.middleware.posthoganalytics.feature_enabled", return_value=True)
+ def test_embeddable_document_stays_report_only_under_enforcement(self, _name, path, _mock_flag):
+ # A customer's site frames each of these. The app policy names only PostHog origins in
+ # frame-ancestors, so enforcing it here stops the document rendering on their page.
+ assert app_csp_header_name(self._request(path)) == "Content-Security-Policy-Report-Only"
+
+ @parameterized.expand(
+ [
+ ("app_root", "/"),
+ ("project_page", "/project/2/dashboard"),
+ # Neither prefix owns these. A shorter prefix match would hand the app catch-all the
+ # carve-out and quietly exempt an ordinary page from enforcement.
+ ("shared_prefix_without_separator", "/sharedthing"),
+ ("exporter_prefix_without_separator", "/exporterthing"),
+ ]
+ )
+ @patch("posthog.middleware.posthoganalytics.feature_enabled", return_value=True)
+ def test_ordinary_page_is_enforced_for_a_flagged_user(self, _name, path, _mock_flag):
+ assert app_csp_header_name(self._request(path)) == "Content-Security-Policy"
+
+ @patch("posthog.middleware.posthoganalytics.feature_enabled", return_value=False)
+ def test_ordinary_page_stays_report_only_without_the_flag(self, _mock_flag):
+ assert app_csp_header_name(self._request("/")) == "Content-Security-Policy-Report-Only"
+
+ @patch("posthog.middleware.posthoganalytics.feature_enabled", return_value=True)
+ def test_the_flag_lookup_carries_the_email_for_local_evaluation(self, mock_flag):
+ # Local evaluation cannot resolve a condition on email unless the caller supplies it, so a
+ # staff-only rollout would enforce nothing.
+ app_csp_header_name(self._request("/", email="[email protected]"))
+ assert mock_flag.call_args.kwargs["person_properties"] == {"email": "[email protected]"}
+ # Local evaluation keeps a flag network call out of every HTML response.
+ assert mock_flag.call_args.kwargs["only_evaluate_locally"] is True
+
+ @patch("posthog.middleware.posthoganalytics.feature_enabled", return_value=True)
+ def test_anonymous_request_stays_report_only(self, mock_flag):
+ # Nothing identifies an anonymous viewer, so the flag cannot bucket them. Login and signup
+ # keep the report-only header until enforcement covers everyone.
+ assert app_csp_header_name(self._request("/login", distinct_id=None)) == "Content-Security-Policy-Report-Only"
+ mock_flag.assert_not_called()
+
+ @patch("posthog.middleware.posthoganalytics.feature_enabled", side_effect=Exception("flags unavailable"))
+ def test_a_failing_flag_lookup_leaves_the_policy_report_only(self, _mock_flag):
+ # Fail safe: an enforced policy that nobody meant to turn on breaks the page.
+ assert app_csp_header_name(self._request("/")) == "Content-Security-Policy-Report-Only"
+
+
+class TestViewManagedCsp(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("custom_policy", "/", False, "default-src 'self'", False),
+ # The workflow asset endpoint sandboxes captured email HTML and leaves frame-ancestors
+ # open so the app can frame it. Enforcement must not replace that policy, because the
+ # app policy drops the sandbox and names a frame-ancestors list the app origin does not
+ # match, which blanks the viewer.
+ ("custom_policy_under_enforcement", "/", True, "sandbox; default-src 'none'", False),
+ ("custom_admin", "/admin/", False, "default-src *", True),
+ ("no_policy", "/", False, None, True),
+ ]
+ )
+ @override_settings(CLOUD_DEPLOYMENT="US")
+ def test_html_response_with_view_managed_csp(
+ self, _name: str, path: str, enforced: bool, policy: str | None, expects_reporting: bool
+ ) -> None:
+ def view(_request: HttpRequest) -> HttpResponse:
+ response = HttpResponse("<html><body>artifact</body></html>", content_type="text/html; charset=utf-8")
+ if policy is not None:
+ response["Content-Security-Policy"] = policy
+ return response
+
+ request = RequestFactory().get(path)
+ request.user = MagicMock(is_authenticated=True, distinct_id="abc", email="[email protected]")
+ with patch("posthog.middleware.posthoganalytics.feature_enabled", return_value=enforced):
+ response = CSPMiddleware(view)(request)
+
+ if path == "/admin/":
+ assert "frame-ancestors 'none'" in response["Content-Security-Policy"]
+ assert "default-src *" not in response["Content-Security-Policy"]
+ elif policy is not None:
+ assert response["Content-Security-Policy"] == policy
+ else:
+ assert "Content-Security-Policy" not in response
+ assert ("Content-Security-Policy-Report-Only" in response) == (expects_reporting and path != "/admin/")
+ assert ("Reporting-Endpoints" in response) == expects_reporting
diff --git a/products/canvas/backend/artifacts.py b/products/canvas/backend/artifacts.py
index bc893fab9cdb..2d8cf77a2dc1 100644
--- a/products/canvas/backend/artifacts.py
+++ b/products/canvas/backend/artifacts.py
@@ -27,7 +27,6 @@ from posthog.storage import object_storage
from products.canvas.backend.contract import artifact_csp
from products.canvas.backend.models import CanvasBuild
-CANVAS_ARTIFACT_RESPONSE_MARKER = "_posthog_canvas_artifact"
ARTIFACT_TOKEN_SALT = "posthog.canvas.artifact.v1"
# Tokens embed a coarse time bucket instead of a per-second timestamp, so the
# artifact URL for a build is stable within a bucket (the iframe src doesn't
@@ -173,6 +172,5 @@ def _with_artifact_headers(response: HttpResponse, etag: str, manifest: dict) ->
response["X-Content-Type-Options"] = "nosniff"
network_origins = ((manifest.get("capabilities") or {}).get("network") or {}).get("origins") or []
response["Content-Security-Policy"] = artifact_csp(network_origins)
- setattr(response, CANVAS_ARTIFACT_RESPONSE_MARKER, True)
response["Permissions-Policy"] = "camera=(), microphone=(), geolocation=(), payment=(), usb=()"
return response