: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(oauth): only let a client revoke tokens issued to it (#105396)
Rafael Audibertb5464ab5f337
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.pyindex ec279a32e06d..5811de891708 100644--- a/posthog/api/oauth/test_views.py+++ b/posthog/api/oauth/test_views.py@@ -4673,11 +4673,9 @@ class TestOAuthAPI(APIBaseTest):"swept when their refresh token is revoked",)+ @parameterized.expand([("refresh_token",), ("access_token",)])@time_machine.travel("2025-01-01 00:00:00", tick=False)- def test_dcr_refresh_token_revoke_from_other_client_does_not_sweep_session(self):- # RFC 7009 §2.1: the server verifies the token was issued to the requesting- # client. A different dynamic client presenting app A's refresh token must not- # trigger the (user, application) session sweep for app A.+ def test_dcr_token_revoke_from_other_client_leaves_session_untouched(self, presented_token_type):self.public_application.is_dcr_client = Trueself.public_application.save()@@ -4732,20 +4730,22 @@ class TestOAuthAPI(APIBaseTest):},)self.assertEqual(refresh_response.status_code, status.HTTP_200_OK)+ linked_access_token = refresh_response.json()["access_token"]+ presented_token = refresh_token if presented_token_type == "refresh_token" else linked_access_tokenrevoke_response = self.post("/oauth/revoke/",{- "token": refresh_token,+ "token": presented_token,"client_id": other_dynamic_application.client_id,},)self.assertEqual(revoke_response.status_code, status.HTTP_200_OK)- self.assertTrue(- OAuthAccessToken.objects.filter(token=unlinked_access_token).exists(),- "a dynamic client presenting another app's refresh token must not sweep "- "that app's (user, application) access-token family",+ self.assertIsNone(OAuthRefreshToken.objects.get(token=refresh_token).revoked)+ self.assertEqual(+ set(OAuthAccessToken.objects.filter(application=self.public_application).values_list("token", flat=True)),+ {unlinked_access_token, linked_access_token},)@time_machine.travel("2026-01-01 00:00:00", tick=False)diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.pyindex 65d38bcfc28f..cecc1ee1f1fe 100644--- a/posthog/api/oauth/views.py+++ b/posthog/api/oauth/views.py@@ -1049,15 +1049,25 @@ class OAuthValidator(OAuth2Validator):alike; a single indexed lookup is cheap and the cost of getting thiswrong is leaving compromised tokens valid.- The sweep only fires when the presented token belongs to the- authenticated client (RFC 7009 §2.1: the server verifies the token was- issued to the requesting client). Without that binding, any dynamic- client that learned another app's refresh token could revoke that- app's entire ``(user, application)`` session instead of just the one- token upstream would revoke.+ A token issued to a different application is left untouched, for every+ client and both token types (RFC 7009 §2.1: the server verifies the+ token was issued to the requesting client). Upstream revokes any token+ it finds by value, so without this check a client that learned another+ app's token could end that app's session. The request still gets a 200,+ the same response as an unknown token, so the endpoint does not tell a+ caller whether a token value is live for some other client."""- rt = OAuthRefreshToken.objects.filter(token=token, revoked__isnull=True).first()- if rt and self._is_dynamic_client(request) and rt.application_id == getattr(request.client, "pk", None):+ rt = OAuthRefreshToken.objects.filter(token=token).first()+ presented = rt or OAuthAccessToken.objects.filter(token=token).first()+ requesting_application_id = getattr(request.client, "pk", None)+ if presented is not None and presented.application_id != requesting_application_id:+ logger.warning(+ "oauth_revoke_token_client_mismatch",+ client_id_prefix=str(getattr(request.client, "client_id", ""))[:8],+ token_type="refresh_token" if rt else "access_token",+ )+ return+ if rt and rt.revoked is None and self._is_dynamic_client(request):revoke_oauth_session(refresh_token=rt)returnreturn super().revoke_token(token, token_type_hint, request, *args, **kwargs)