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: mcp impersonation skip cred review (#85258)

Christian Rafferty051ffcf2b77e
diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.py
index af5d2cf963ff..61684b3f000e 100644
--- a/posthog/api/test/test_user.py
+++ b/posthog/api/test/test_user.py
@@ -344,6 +344,51 @@ class TestUserAPI(APIBaseTest):
assert response.status_code == 200
assert response.json()["requires_credential_review"] is expected
+ def test_requires_credential_review_skipped_when_impersonating_via_session(self):
+ User.objects.filter(pk=self.user.pk).update(credentials_reviewed_at=None)
+ PersonalAPIKey.objects.create(
+ user=self.user,
+ label="Test key",
+ secure_value=hash_key_value("phx_test_value_impersonated"),
+ scopes=["*"],
+ )
+ with patch("posthog.helpers.impersonation.is_impersonated_session", return_value=True):
+ response = self.client.get("/api/users/@me/")
+ assert response.status_code == 200
+ assert response.json()["requires_credential_review"] is False
+
+ def test_requires_credential_review_skipped_when_impersonating_via_oauth_token(self):
+ User.objects.filter(pk=self.user.pk).update(credentials_reviewed_at=None)
+ PersonalAPIKey.objects.create(
+ user=self.user,
+ label="Test key",
+ secure_value=hash_key_value("phx_test_value_impersonated_oauth"),
+ scopes=["*"],
+ )
+ staff = User.objects.create_user(email="[email protected]", password="x", first_name="Staff", is_staff=True)
+ app = OAuthApplication.objects.create(
+ name="MCP client",
+ client_id="test_impersonation_client_id",
+ client_type=OAuthApplication.CLIENT_CONFIDENTIAL,
+ authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
+ redirect_uris="https://example.com/callback",
+ algorithm="RS256",
+ user=self.user,
+ )
+ token = OAuthAccessToken.objects.create(
+ user=self.user,
+ application=app,
+ token="pha_test_impersonated_access_token",
+ scope="user:read",
+ expires=timezone.now() + timedelta(hours=1),
+ scoped_teams=[self.team.id],
+ impersonated_by=staff,
+ )
+ self.client.logout()
+ response = self.client.get("/api/users/@me/", headers={"authorization": f"Bearer {token.token}"})
+ assert response.status_code == 200
+ assert response.json()["requires_credential_review"] is False
+
def test_requires_credential_review_unverified_passkey(self):
# Unverified passkeys are the realistic pre-claim attack artifact - a partner
# session can register a credential without ever completing verification.
diff --git a/posthog/api/user.py b/posthog/api/user.py
index 631a35302468..633bc4da3f23 100644
--- a/posthog/api/user.py
+++ b/posthog/api/user.py
@@ -90,6 +90,7 @@ from posthog.helpers.email_utils import (
reject_plus_addressed_email,
validate_display_name,
)
+from posthog.helpers.impersonation import is_impersonated
from posthog.helpers.session_cache import SessionCache
from posthog.helpers.two_factor_session import has_passkeys, set_two_factor_verified_in_session
from posthog.helpers.verified_domain_enforcement import VERIFIED_DOMAIN_REQUIRED_ERROR, resolve_login_organization
@@ -457,6 +458,9 @@ class UserSerializer(serializers.ModelSerializer):
def get_requires_credential_review(self, instance: User) -> bool:
if instance.credentials_reviewed_at is not None:
return False
+ # impersonating users shouldn't bounced into the credential review screen
+ if is_impersonated(self.context.get("request")):
+ return False
if PersonalAPIKey.objects.filter(user=instance).exists():
return True
if WebauthnCredential.objects.filter(user=instance).exists():