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(api): restore query performance UI access to slowest queries (#64890)

Juraj Majerik001a5583ef12
diff --git a/frontend/src/lib/scopes.tsx b/frontend/src/lib/scopes.tsx
index 5a9bd1a81d8e..0d2609aa6c22 100644
--- a/frontend/src/lib/scopes.tsx
+++ b/frontend/src/lib/scopes.tsx
@@ -121,7 +121,7 @@ export const API_SCOPES: APIScope[] = [
},
{ key: 'property_definition', objectName: 'Property definition', objectPlural: 'property definitions' },
{ key: 'query', objectName: 'Query', objectPlural: 'queries', disabledActions: ['write'] },
- // `query_performance` is omitted — see `INTERNAL_API_SCOPE_OBJECTS` in posthog/scopes.py.
+ // `query_performance` is omitted — OAuth-hidden, PAT-grantable only (see `OAUTH_HIDDEN_SCOPE_OBJECTS` in posthog/scopes.py).
{ key: 'replay_scanner', objectName: 'Replay scanner', objectPlural: 'replay scanners' },
{ key: 'revenue_analytics', objectName: 'Revenue analytics', objectPlural: 'revenue analytics' },
{ key: 'session_recording', objectName: 'Session recording', objectPlural: 'session recordings' },
diff --git a/posthog/api/debug_ch_queries.py b/posthog/api/debug_ch_queries.py
index 8d23917f0e2c..f2c2c15cabcc 100644
--- a/posthog/api/debug_ch_queries.py
+++ b/posthog/api/debug_ch_queries.py
@@ -38,10 +38,11 @@ class DebugCHQueries(viewsets.ViewSet):
List recent CH queries initiated by this user.
"""
- # `INTERNAL` (not `"query_performance"`) so a staff user's full-access (`*`) PAT
- # cannot satisfy the scope check via the wildcard short-circuit in
- # `APIScopePermission.has_permission`. The action below pins the explicit
- # `query_performance:read` requirement, which only programmatically-minted PATs hold.
+ # `scope_object = "INTERNAL"` blocks a staff user's full-access (`*`) PAT via the
+ # wildcard short-circuit in `APIScopePermission.has_permission`. The action below pins
+ # `query_performance:read` — an OAuth-hidden, PAT-grantable scope (see
+ # OAUTH_HIDDEN_SCOPE_OBJECTS) that automation carries; the browser uses session auth,
+ # which bypasses scope checks. `is_staff` gates the action itself in every case.
scope_object = "INTERNAL"
permission_classes = [IsAuthenticated, APIScopePermission]
authentication_classes = [SessionAuthentication, PersonalAPIKeyAuthentication]
diff --git a/posthog/api/test/test_debug_ch_queries.py b/posthog/api/test/test_debug_ch_queries.py
index 95f56f900b7b..69e0e8eaa07f 100644
--- a/posthog/api/test/test_debug_ch_queries.py
+++ b/posthog/api/test/test_debug_ch_queries.py
@@ -55,7 +55,8 @@ class TestDebugCHQuery(APIBaseTest):
def test_slowest_queries_wildcard_pat_rejected(self):
# A full-access (`*`) PAT must NOT satisfy the query_performance:read requirement —
- # this scope is INTERNAL and only programmatically-minted tokens carry it explicitly.
+ # the view's `scope_object = "INTERNAL"` blocks the wildcard short-circuit, so a PAT
+ # must carry `query_performance:read` explicitly.
self.user.is_staff = True
self.user.save()
token = self._create_pat(scopes=["*"])
diff --git a/posthog/scopes.py b/posthog/scopes.py
index 1d29ebf1870c..f42461d85488 100644
--- a/posthog/scopes.py
+++ b/posthog/scopes.py
@@ -133,7 +133,6 @@ API_SCOPE_ACTIONS: tuple[APIScopeActions, ...] = get_args(APIScopeActions)
INTERNAL_API_SCOPE_OBJECTS: frozenset[APIScopeObject] = frozenset(
{
"clickhouse_test_cluster_perf",
- "query_performance",
# Sandbox-only writes for the headless Signals agent (memory create/delete,
# finding emit). Read access for the same surface lives on the public
# `signal_scout` object so user-grantable PAKs can still inspect runs/memory.
@@ -142,10 +141,11 @@ INTERNAL_API_SCOPE_OBJECTS: frozenset[APIScopeObject] = frozenset(
)
# Scope objects available via personal API keys but never advertised through
-# OAuth metadata. Used for alpha / not-yet-public products where a user can
-# manually paste the scope into a PAT but where we don't want OAuth-based
-# clients (the consent screen, MCP, third-party apps) to discover it.
-OAUTH_HIDDEN_SCOPE_OBJECTS: frozenset[APIScopeObject] = frozenset({"wizard_session"})
+# OAuth metadata. Used where a user can manually paste the scope into a PAT but
+# we don't want OAuth-based clients (the consent screen, MCP, third-party apps)
+# to discover it — alpha / not-yet-public products, or staff-only debug endpoints
+# automation reaches with a PAT (e.g. `query_performance`, also gated by `is_staff`).
+OAUTH_HIDDEN_SCOPE_OBJECTS: frozenset[APIScopeObject] = frozenset({"wizard_session", "query_performance"})
# llm_gateway:read is omitted on purpose: it's alpha/privileged and granted only behind the
# ai-gateway flag in ProjectSecretAPIKeySerializer, not unconditionally like the entries here.