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): report violations from about:blank and srcdoc frames (#104915)

Tom Piccirello6a5ff3774a13
diff --git a/posthog/middleware.py b/posthog/middleware.py
index b78126ee4ece..6cbdda0562f0 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -1500,20 +1500,17 @@ class CSPMiddleware:
admin_report_endpoint = csp_report_endpoint()
if admin_report_endpoint:
- csp_parts += [f"report-uri {admin_report_endpoint}", "report-to posthog"]
# Without a distinct_id the report endpoint mints a new one for every report, so a
- # single staff session reads as a crowd of users. Only this header carries it, as in
- # the app policy below.
+ # single staff session reads as a crowd of users.
user = getattr(request, "user", None)
distinct_id = getattr(user, "distinct_id", None) if user is not None and user.is_authenticated else None
reporting_endpoint = (
csp_report_endpoint(distinct_id=distinct_id) if distinct_id else admin_report_endpoint
)
- # 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="{reporting_endpoint}", default="{reporting_endpoint}"'
- )
+ # The policy has no `report-to` directive. The app policy below gives the reason.
+ csp_parts.append(f"report-uri {reporting_endpoint}")
+ # Browsers only deliver crash reports to the endpoint named `default`.
+ response.headers["Reporting-Endpoints"] = f'default="{reporting_endpoint}"'
response.headers["Content-Security-Policy"] = "; ".join(csp_parts)
elif "Content-Security-Policy" in response.headers:
# The view picked this policy for this document: a canvas artifact runs untrusted code,
@@ -1685,16 +1682,21 @@ class CSPMiddleware:
shadow_uri = csp_report_endpoint(sample_rate=sample_rate, v=NARROWED_APP_POLICY_REPORT_VERSION)
shadow_parts = [*narrowed_app_policy(csp_parts, replacements), f"report-uri {shadow_uri}"]
if report_uri:
- csp_parts += [f"report-uri {report_uri}", "report-to posthog"]
report_endpoint = report_uri
if distinct_id:
- # Crash reports arrive after the tab already died, so the report body is the
- # only chance to attribute them; carrying the distinct_id in the endpoint URL
- # ties the event to the person instead of a random per-report id.
+ # A report body never names the person, and a crash report arrives after the tab
+ # already died, so only the URL can carry the distinct_id. Without it, the report
+ # endpoint mints a random id for every report.
report_endpoint = csp_report_endpoint(sample_rate=sample_rate, distinct_id=distinct_id)
- # 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}"'
+ # The policy has no `report-to` directive, even though CSP3 marks `report-uri` as
+ # deprecated. While a policy names `report-to`, browsers ignore its `report-uri` and
+ # send reports only through the Reporting API. That API drops violations raised in
+ # about:blank and srcdoc frames, because those documents inherit this policy but not
+ # the Reporting-Endpoints header. Without `report-to`, browsers send those violations
+ # to `report-uri`.
+ csp_parts.append(f"report-uri {report_endpoint}")
+ # Browsers only deliver crash reports to the endpoint named `default`.
+ response.headers["Reporting-Endpoints"] = f'default="{report_endpoint}"'
header_name = app_csp_header_name(request)
response.headers[header_name] = "; ".join(csp_parts)
if shadow_parts:
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index eefe1cf65b35..24cdcbec0cc9 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -2267,18 +2267,25 @@ class TestCSPMiddleware(APIBaseTest):
@override_settings(CLOUD_DEPLOYMENT="US") # As PostHog Cloud
def test_html_response_declares_default_reporting_endpoint_with_distinct_id(self):
+ response = self.client.get("/")
+ policy = response["Content-Security-Policy-Report-Only"]
+ # A `report-to` directive makes browsers ignore `report-uri` and report through the
+ # Reporting API, which drops violations raised in about:blank and srcdoc frames.
+ assert "report-to" not in policy
+ _, report_endpoint = next(part for part in policy.split("; ") if part.startswith("report-uri ")).split()
+ assert report_endpoint.startswith("https://us.i.posthog.com/report/")
+ assert f"distinct_id={self.user.distinct_id}" in report_endpoint
# Browsers only deliver crash reports to the endpoint named `default`, so dropping or
# renaming it silently stops crash ingestion.
- response = self.client.get("/")
- header = response["Reporting-Endpoints"]
- assert 'posthog="https://us.i.posthog.com/report/' in header
- assert 'default="https://us.i.posthog.com/report/' in header
- assert f"distinct_id={self.user.distinct_id}" in header
+ assert response["Reporting-Endpoints"] == f'default="{report_endpoint}"'
@override_settings(CLOUD_DEPLOYMENT="US") # As PostHog Cloud
def test_reporting_endpoints_omit_distinct_id_when_logged_out(self):
self.client.logout()
response = self.client.get("/login")
+ policy = response["Content-Security-Policy-Report-Only"]
+ assert "report-uri https://us.i.posthog.com/report/" in policy
+ assert "distinct_id" not in policy
header = response["Reporting-Endpoints"]
assert 'default="https://us.i.posthog.com/report/' in header
assert "distinct_id" not in header
@@ -2334,12 +2341,8 @@ class TestCSPMiddleware(APIBaseTest):
policy = response["Content-Security-Policy-Report-Only"]
assert f"report-uri https://posthog.example.com/report/?sample_rate={expected_rate}" in policy
+ assert f"sample_rate={expected_rate}&distinct_id={self.user.distinct_id}" in policy
assert f"sample_rate={other_rate}" not in policy
- # The crash-reporting endpoint is built by a second call that takes the rate separately, so
- # it can drift from the directive above.
- header = response["Reporting-Endpoints"]
- assert f"sample_rate={expected_rate}&distinct_id={self.user.distinct_id}" in header
- assert f"sample_rate={other_rate}" not in header
@parameterized.expand(
[
@@ -2357,18 +2360,19 @@ class TestCSPMiddleware(APIBaseTest):
# Only the admin policy forbids framing outright; the non-HTML fallback is default-src alone.
assert "frame-ancestors 'none'" in policy
assert "Content-Security-Policy-Report-Only" not in response
+ assert "report-to" not in policy
if expects_reporting:
assert "report-uri https://us.i.posthog.com/report/" in policy
- assert "report-to posthog" in policy
# Sampling the admin policy too would silently drop violations, so the branches diverge.
assert "sample_rate" not in policy
# Without it every admin report arrives under a freshly minted id, so one staff session
# counts as many users.
- assert f"distinct_id={self.user.distinct_id}" in response["Reporting-Endpoints"]
+ assert f"distinct_id={self.user.distinct_id}" in policy
+ _, report_endpoint = next(part for part in policy.split("; ") if part.startswith("report-uri ")).split()
+ assert response["Reporting-Endpoints"] == f'default="{report_endpoint}"'
else:
assert "report-uri" not in policy
- assert "report-to" not in policy
assert "Reporting-Endpoints" not in response
@override_settings(ADMIN_PORTAL_ENABLED=False)