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(logs): allow logs, traces and metrics under read-only impersonation (#86362)

Frank Hamandcda99f9d717d
diff --git a/posthog/middleware.py b/posthog/middleware.py
index 8cee3f993036..369a47ef0b72 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -1372,6 +1372,31 @@ READ_ONLY_IMPERSONATION_ALLOWLISTED_PATHS: list[tuple[str, str | re.Pattern]] =
),
# POST but read-only: kicks off insight/dashboard/session replay export renders (e.g. MP4)
("POST", re.compile(r"^/api/(environments|projects)/([0-9]+|@current)/exports/?$")),
+ # POST but read-only: the Logs product sends its queries as POST because the filter payload
+ # is too large for a query string. Action names are enumerated rather than allowing the whole
+ # `logs/` prefix, which also hosts writing CRUD viewsets (alerts, views, sampling_rules,
+ # retention_rules, metric_rules, anomalies).
+ (
+ "POST",
+ re.compile(
+ r"^/api/(environments|projects)/([0-9]+|@current)/logs/"
+ r"(query|sparkline|facet_values|count-ranges|count|services|patterns_diff|patterns|group-by)/?$"
+ ),
+ ),
+ # POST but read-only: same reasoning for the Traces (tracing spans) product
+ (
+ "POST",
+ re.compile(
+ r"^/api/(environments|projects)/([0-9]+|@current)/tracing/spans/"
+ r"(query|count|symbol-stats|sparkline|duration-histogram|latency-heatmap|aggregate|tree"
+ r"|attribute-breakdown|trace/[a-zA-Z0-9]+)/?$"
+ ),
+ ),
+ # POST but read-only: same reasoning for the Metrics product
+ (
+ "POST",
+ re.compile(r"^/api/(environments|projects)/([0-9]+|@current)/metrics/(query|samples|characterize|explain)/?$"),
+ ),
# Allow upgrading from read-only to read-write impersonation
("POST", "/admin/impersonation/upgrade/"),
# Logout is POST in Django 5; the frontend submits to `/logout` (no trailing slash),
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index 679150ff1e7e..2584f69c74c0 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -875,6 +875,13 @@ class TestImpersonationReadOnlyMiddleware(APIBaseTest):
"exports/",
{"export_format": "video/mp4", "export_context": {"session_recording_id": "test-session"}},
),
+ ("logs_query", "logs/query/", {}),
+ # hyphenated action that the shorter `count` alternative must not shadow
+ ("logs_count_ranges", "logs/count-ranges/", {"query": "not-a-dict"}),
+ ("tracing_attribute_breakdown", "tracing/spans/attribute-breakdown/", {}),
+ ("tracing_trace_by_id", "tracing/spans/trace/zzz/", {}),
+ ("metrics_query", "metrics/query/", {}),
+ ("metrics_explain", "metrics/explain/", {}),
]
)
def test_read_only_impersonation_allows_allowlisted_post(self, _name, path_suffix, body):
@@ -890,6 +897,27 @@ class TestImpersonationReadOnlyMiddleware(APIBaseTest):
assert response.status_code != 403 or response.json().get("code") != "impersonation_read_only"
+ @parameterized.expand(
+ [
+ ("logs_alerts", "logs/alerts/"),
+ ("logs_views", "logs/views/"),
+ ("logs_sampling_rules", "logs/sampling_rules/"),
+ ]
+ )
+ def test_read_only_impersonation_blocks_logs_crud_siblings(self, _name, path_suffix):
+ self.login_as_other_user_read_only()
+
+ # The logs query allowlist enumerates action names precisely because the same
+ # `logs/` prefix hosts these writing viewsets.
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/{path_suffix}",
+ data={},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 403
+ assert response.json()["code"] == "impersonation_read_only"
+
def test_read_only_impersonation_blocks_query_cancellation(self):
self.login_as_other_user_read_only()