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): name the region when /authorize rejects a client_id (#96870)

posthog[bot]4cacf55c249e
diff --git a/posthog/api/oauth/test_cimd.py b/posthog/api/oauth/test_cimd.py
index f7081ccbcb3e..700d4fd76b7e 100644
--- a/posthog/api/oauth/test_cimd.py
+++ b/posthog/api/oauth/test_cimd.py
@@ -1118,6 +1118,9 @@ class TestCIMDAuthorizeIntegration(APIBaseTest):
self.assertEqual(response.status_code, 400)
self.assertIn("invalid", response.json().get("error", "").lower())
+ # A CIMD client_id is a URL we resolve, not a registration we hold, so the description
+ # must point at the metadata document instead of suggesting the other cloud region.
+ self.assertIn("client metadata document", response.json()["error_description"])
@patch("posthog.api.oauth.cimd.requests.Session.get")
def test_cimd_mismatched_redirect_uri_rejected(self, mock_get, _url_mock):
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.py
index eca82e7d61dd..df5f57061c8e 100644
--- a/posthog/api/oauth/test_views.py
+++ b/posthog/api/oauth/test_views.py
@@ -272,15 +272,31 @@ class TestOAuthAPI(APIBaseTest):
self.assertEqual(response.json()["error"], "invalid_request")
self.assertEqual(response.json()["error_description"], f"Duplicate {param} parameter.")
- def test_authorize_invalid_client_id(self):
+ @parameterized.expand(
+ [
+ ("self_hosted", None, None),
+ ("us_cloud", "US", "https://eu.posthog.com"),
+ ("eu_cloud", "EU", "https://us.posthog.com"),
+ ]
+ )
+ def test_authorize_invalid_client_id(self, _name, cloud_deployment, other_region_host):
url = self.base_authorization_url
- url_without_client_id = self.replace_param_in_url(url, "client_id", "invalid_id")
+ url_with_unknown_client_id = self.replace_param_in_url(url, "client_id", "invalid_id")
+
+ with override_settings(CLOUD_DEPLOYMENT=cloud_deployment):
+ response = self.client.get(url_with_unknown_client_id)
- response = self.client.get(url_without_client_id)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.json()["error"], "invalid_request")
- self.assertEqual(response.json()["error_description"], "Invalid client_id parameter value.")
+ description = response.json()["error_description"]
+ if other_region_host is None:
+ self.assertNotIn("region", description)
+ else:
+ # An app created in the other region is the case oauthlib's flat "Invalid client_id
+ # parameter value." hides, so the description has to name both regions.
+ self.assertIn(f"in the {cloud_deployment} region", description)
+ self.assertIn(other_region_host, description)
def test_authorize_missing_redirect_uri(self):
# According to the spec, if the client has a single redirect URI, the authorization server does not require an
diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.py
index 5ff36539deb4..f20c964d737d 100644
--- a/posthog/api/oauth/views.py
+++ b/posthog/api/oauth/views.py
@@ -34,7 +34,7 @@ from oauth2_provider.views import (
)
from oauth2_provider.views.mixins import OAuthLibMixin
from oauthlib.common import Request as OauthlibRequest
-from oauthlib.oauth2 import InvalidGrantError
+from oauthlib.oauth2 import InvalidClientIdError, InvalidGrantError
from redis.exceptions import RedisError
from rest_framework import serializers, status
from rest_framework.authentication import SessionAuthentication
@@ -131,6 +131,38 @@ def get_region_info() -> dict | None:
return None
+# The host the other PostHog Cloud region answers on, so an unknown client_id can name it.
+_OTHER_CLOUD_REGION_HOST = {"US": "https://eu.posthog.com", "EU": "https://us.posthog.com"}
+
+
+def unknown_client_id_description(client_id: str | None) -> str:
+ """Describe an unknown `client_id` so the client can act on the 400.
+
+ An OAuth application exists in one region only, and a request sent to the other
+ region fails the client lookup with nothing to separate it from a typo. Naming the
+ region is what makes that case recognizable.
+
+ A CIMD client_id is a metadata URL resolved on demand rather than a registration
+ we hold, so it fails for a different reason and gets a different description.
+ """
+ if is_cimd_client_id(client_id):
+ return (
+ "PostHog could not read the client metadata document at this client_id. "
+ "Check that the URL is reachable over HTTPS and returns valid client metadata."
+ )
+
+ cloud = getattr(settings, "CLOUD_DEPLOYMENT", None)
+ other_host = _OTHER_CLOUD_REGION_HOST.get(cloud or "")
+ if other_host is None:
+ return "No OAuth application is registered with this client_id. Check the client_id, or register the application again."
+
+ return (
+ f"No OAuth application is registered with this client_id in the {cloud} region. "
+ f"An application belongs to the region it was created in. If you created it on {other_host}, "
+ f"send the authorization request to {other_host} instead. Otherwise check the client_id."
+ )
+
+
# Substrings identifying transient database failures that OAuth clients should retry.
# PgBouncer (port 6543) kills queries waiting too long for a backend connection with
# `query_wait_timeout`, and surfaces dropped/reset backend connections as connection
@@ -1366,7 +1398,13 @@ class OAuthAuthorizationView(OAuthLibMixin, APIView):
try:
application = OAuthApplication.objects.get(client_id=credentials["client_id"])
except OAuthApplication.DoesNotExist:
- return Response({"error": "Invalid client_id"}, status=status.HTTP_400_BAD_REQUEST)
+ return Response(
+ {
+ "error": "invalid_request",
+ "error_description": unknown_client_id_description(credentials["client_id"]),
+ },
+ status=status.HTTP_400_BAD_REQUEST,
+ )
# Track OAuth authorization attempts with the authenticated user
registration_type = self._registration_type(application)
@@ -1496,7 +1534,13 @@ class OAuthAuthorizationView(OAuthLibMixin, APIView):
application = OAuthApplication.objects.get(client_id=serializer.validated_data["client_id"])
except OAuthApplication.DoesNotExist:
logger.warning("oauth_authorize_invalid_client", client_id=serializer.validated_data["client_id"])
- return Response({"error": "Invalid client_id"}, status=status.HTTP_400_BAD_REQUEST)
+ return Response(
+ {
+ "error": "invalid_request",
+ "error_description": unknown_client_id_description(serializer.validated_data["client_id"]),
+ },
+ status=status.HTTP_400_BAD_REQUEST,
+ )
credentials = {
"client_id": serializer.validated_data["client_id"],
@@ -1643,6 +1687,15 @@ class OAuthAuthorizationView(OAuthLibMixin, APIView):
Handle errors either by redirecting to redirect_uri with a json in the body containing
error details or providing an error response
"""
+ # oauthlib reports every failed client lookup as "Invalid client_id parameter value.",
+ # which reads the same whether the client_id is wrong or the app lives in the other
+ # region. Replace it before the response is built so the region is named.
+ oauthlib_error = getattr(error, "oauthlib_error", None)
+ if isinstance(oauthlib_error, InvalidClientIdError):
+ # An unknown client_id is fatal, so DOT never redirects it and this description
+ # only ever reaches the JSON body.
+ oauthlib_error.description = unknown_client_id_description(self.request.query_params.get("client_id"))
+
redirect, error_response = super().error_response(error, **kwargs)
# Surface scope-ceiling rejections so on-call can alert on /authorize failing with invalid_scope.