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(oauth): reject CIMD scope sets that strip to nothing (#62478)

Matt Brooker4e0dc88430de
diff --git a/posthog/api/oauth/cimd.py b/posthog/api/oauth/cimd.py
index a6c6112e2d0a..420fbba52e6f 100644
--- a/posthog/api/oauth/cimd.py
+++ b/posthog/api/oauth/cimd.py
@@ -346,6 +346,13 @@ def _resolve_scopes(metadata: CIMDMetadataDocument) -> list[str] | None:
"""Resolve the allow-listed `com.posthog.scopes` for an app, or None when the field
is absent so callers leave existing scopes untouched. A present field returns a
(possibly empty) list, capped to grantable scopes by `filter_to_unprivileged_scopes`.
+
+ Raises CIMDValidationError when the field is present and non-empty but every entry is
+ non-grantable. An empty ceiling falls back to the broad UNPRIVILEGED_SCOPES default
+ (`effective_ceiling`), so silently storing `[]` here would widen a misconfigured app
+ to the full default surface — broader than it asked for. Reject it the way DCR rejects
+ an all-stripped `scope` string, so the partner fixes their metadata. An explicitly empty
+ list is left as the legitimate "use default" signal, same as an absent field.
"""
com_posthog = metadata.get("com.posthog")
if not isinstance(com_posthog, dict):
@@ -354,7 +361,13 @@ def _resolve_scopes(metadata: CIMDMetadataDocument) -> list[str] | None:
raw_scopes: object = com_posthog.get("scopes")
if not isinstance(raw_scopes, list):
return None
- return filter_to_unprivileged_scopes(raw_scopes)
+ filtered = filter_to_unprivileged_scopes(raw_scopes)
+ if raw_scopes and not filtered:
+ raise CIMDValidationError(
+ "None of the declared com.posthog.scopes are available to self-registered clients. "
+ "Remove the field to register with the default scope set."
+ )
+ return filtered
def _create_cimd_application(url: str, metadata: CIMDMetadataDocument) -> OAuthApplication:
diff --git a/posthog/api/oauth/test_cimd.py b/posthog/api/oauth/test_cimd.py
index 07b36542c171..34f233adc309 100644
--- a/posthog/api/oauth/test_cimd.py
+++ b/posthog/api/oauth/test_cimd.py
@@ -1,6 +1,7 @@
import json
import base64
import hashlib
+from typing import cast
from urllib.parse import urlencode
import pytest
@@ -9,7 +10,7 @@ from unittest.mock import MagicMock, patch
from django.conf import settings
from django.core.cache import cache as real_cache
-from django.test import override_settings
+from django.test import SimpleTestCase, override_settings
import requests
from cryptography.hazmat.primitives import serialization
@@ -21,8 +22,10 @@ from posthog.api.oauth.cimd import (
CIMD_PROVISIONING_ACCOUNT_REQUESTS_DEFAULT_RATE_LIMIT,
CIMD_PROVISIONING_ACCOUNT_REQUESTS_VERIFIED_RATE_LIMIT,
CIMDFetchError,
+ CIMDMetadataDocument,
CIMDValidationError,
_fetch_lock_key,
+ _resolve_scopes,
fetch_and_upsert_cimd_application,
fetch_cimd_metadata,
get_application_by_client_id,
@@ -1056,3 +1059,56 @@ class TestCIMDComPostHogNamespace(APIBaseTest):
assert app is not None
self.assertEqual(app.scopes, [])
+
+ # A present, non-empty com.posthog.scopes that strips to nothing is rejected, not
+ # stored as [] (which would widen the app to the broad UNPRIVILEGED default via the
+ # empty-ceiling fallback). Mirrors DCR's all-stripped rejection; no app is created.
+ @patch("posthog.api.oauth.cimd.requests.get")
+ def test_all_non_grantable_scopes_on_creation_rejected(self, mock_get, _url_mock):
+ only_non_grantable = [*sorted(PRIVILEGED_SCOPES), "not_a_real_scope:write"]
+ mock_get.return_value = _mock_response(_make_metadata(com_posthog={"scopes": only_non_grantable}), headers={})
+
+ with self.assertRaises(CIMDValidationError):
+ fetch_and_upsert_cimd_application(VALID_CIMD_URL)
+
+ self.assertFalse(OAuthApplication.objects.filter(cimd_metadata_url=VALID_CIMD_URL).exists())
+
+ # On refresh, a doc whose scopes all strip out is rejected and the existing ceiling is
+ # left untouched (fail-closed) rather than widened to the default.
+ @patch("posthog.api.oauth.cimd.requests.get")
+ def test_all_non_grantable_scopes_on_refresh_leaves_existing_untouched(self, mock_get, _url_mock):
+ mock_get.return_value = _mock_response(_make_metadata(com_posthog={"scopes": ["insight:read"]}), headers={})
+ created = fetch_and_upsert_cimd_application(VALID_CIMD_URL)
+ assert created is not None
+
+ real_cache.delete(_fetch_lock_key(VALID_CIMD_URL))
+ only_non_grantable = [*sorted(PRIVILEGED_SCOPES), "not_a_real_scope:write"]
+ mock_get.return_value = _mock_response(_make_metadata(com_posthog={"scopes": only_non_grantable}), headers={})
+
+ with self.assertRaises(CIMDValidationError):
+ fetch_and_upsert_cimd_application(VALID_CIMD_URL)
+
+ created.refresh_from_db()
+ self.assertEqual(created.scopes, ["insight:read"])
+
+
+class TestResolveScopes(SimpleTestCase):
+ """`_resolve_scopes` parsing in isolation — no DB, so it runs without local services."""
+
+ def test_absent_or_malformed_field_returns_none(self) -> None:
+ self.assertIsNone(_resolve_scopes({}))
+ self.assertIsNone(_resolve_scopes({"com.posthog": {}}))
+ # Malformed partner JSON: a non-list scopes value hits the runtime guard and returns None.
+ self.assertIsNone(_resolve_scopes(cast(CIMDMetadataDocument, {"com.posthog": {"scopes": "not-a-list"}})))
+
+ def test_explicit_empty_list_is_use_default(self) -> None:
+ # Distinct from all-stripped: an explicitly empty array is the legitimate "use default" signal.
+ self.assertEqual(_resolve_scopes({"com.posthog": {"scopes": []}}), [])
+
+ def test_partial_strip_keeps_grantable(self) -> None:
+ resolved = _resolve_scopes({"com.posthog": {"scopes": ["insight:read", "llm_gateway:read"]}})
+ self.assertEqual(resolved, ["insight:read"])
+
+ def test_all_non_grantable_raises(self) -> None:
+ with self.assertRaises(CIMDValidationError):
+ _resolve_scopes({"com.posthog": {"scopes": ["llm_gateway:read", "not_a_real_scope:write"]}})