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.

feat(csp): report every violation staff hit, not one in ten (#102020)

Tom Piccirellod17348aa105c
diff --git a/posthog/middleware.py b/posthog/middleware.py
index 0a3d6501c37c..efe38ba4ae98 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -1414,16 +1414,35 @@ class CSPMiddleware:
"form-action 'self' https://accounts.google.com",
]
- report_uri = csp_report_endpoint(sample_rate="0.1")
+ # Both values are read inside one narrowed block, so nothing below re-checks `user`.
+ user = getattr(request, "user", None)
+ if user is not None and user.is_authenticated:
+ is_staff = bool(getattr(user, "is_staff", False))
+ distinct_id = getattr(user, "distinct_id", None)
+ else:
+ is_staff = False
+ distinct_id = None
+
+ # Staff get the policy enforced ahead of everyone else, so each violation they report is
+ # something already broken for a colleague rather than one sample of a trend. At 0.1 we
+ # would see one breakage in ten, which is the opposite of what the staff rollout is for.
+ # The endpoint does the sampling, so browsers already send every report and taking staff
+ # to 1 costs ingestion rather than client traffic.
+ #
+ # This keys on is_staff rather than on the enforcement flag, which would otherwise track
+ # the enforced population exactly. The flag widens until it covers everyone, and would
+ # silently take the whole fleet to unsampled reporting; staff stays bounded.
+ sample_rate = "1" if is_staff else "0.1"
+
+ report_uri = csp_report_endpoint(sample_rate=sample_rate)
if report_uri:
csp_parts += [f"report-uri {report_uri}", "report-to posthog"]
report_endpoint = report_uri
- user = getattr(request, "user", None)
- if user is not None and user.is_authenticated and getattr(user, "distinct_id", None):
+ 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.
- report_endpoint = csp_report_endpoint(sample_rate="0.1", distinct_id=user.distinct_id)
+ 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}"'
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index 7b1653f9ceff..e991dab023eb 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -2062,6 +2062,33 @@ class TestCSPMiddleware(APIBaseTest):
assert "us.i.posthog.com" not in header
assert f"distinct_id={self.user.distinct_id}" in header
+ @parameterized.expand(
+ [
+ ("staff", True, "1", "0.1"),
+ ("not_staff", False, "0.1", "1"),
+ ]
+ )
+ @override_settings(CSP_REPORT_ENDPOINT="https://posthog.example.com/report/")
+ def test_staff_report_every_violation_while_everyone_else_is_sampled(
+ self, _name, is_staff, expected_rate, other_rate
+ ):
+ # Staff get the policy enforced ahead of everyone else, so a violation of theirs is
+ # something already broken for a colleague rather than one sample of a trend. At 0.1 nine
+ # in ten of those never arrive, which defeats the point of rolling out to staff first.
+ self.user.is_staff = is_staff
+ self.user.save()
+
+ response = self.client.get("/")
+
+ 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={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(
[
("cloud", {"CLOUD_DEPLOYMENT": "US"}, True),