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): check third-party access from the user's tokens (#90841)

José Sequeira7eb09d665020
diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.py
index 4a9e024260d1..8040421ad035 100644
--- a/posthog/api/test/test_user.py
+++ b/posthog/api/test/test_user.py
@@ -409,6 +409,7 @@ class TestUserAPI(APIBaseTest):
("live_refresh_token_only", -1, True, False, True),
("expired_access_token_only", -1, False, False, False),
("live_access_token_first_party", 1, False, True, False),
+ ("live_refresh_token_only_first_party", -1, True, True, False),
]
)
def test_requires_credential_review_for_oauth_access(
diff --git a/posthog/models/oauth.py b/posthog/models/oauth.py
index 0527923e4dbc..d6992e1316db 100644
--- a/posthog/models/oauth.py
+++ b/posthog/models/oauth.py
@@ -9,7 +9,7 @@ from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.indexes import GinIndex
from django.core.exceptions import ValidationError
from django.db import connection, models, transaction
-from django.db.models import Q
+from django.db.models import Exists, Q
from django.dispatch import receiver
from django.utils import timezone
@@ -26,6 +26,7 @@ from oauth2_provider.settings import oauth2_settings
from oauth2_provider.validators import AllowedURIValidator
from posthog.models.activity_logging.model_activity import ModelActivityMixin
+from posthog.models.user import User
from posthog.models.utils import UUIDT, generate_random_token, hash_key_value, mask_key_value
if TYPE_CHECKING:
@@ -705,12 +706,18 @@ def has_live_third_party_oauth_access(user: "User") -> bool:
First-party applications are excluded because they are PostHog's own surfaces, so a token from
one is not the third-party access this answers about.
+
+ The check starts from the user's tokens, not from the application table. An `id IN (...)`
+ filter on applications makes Postgres scan every application row even when the user has no
+ tokens at all, and this runs on every load of the current user.
+
+ Both lookups go in one query as `EXISTS(...) OR EXISTS(...)` on the user row. Postgres runs
+ the second subplan only when the first finds nothing, so refresh tokens go first: a partner
+ holds one for the life of the connection, while its access token is usually expired.
"""
- return OAuthApplication.objects.filter(
- Q(id__in=live_oauth_access_tokens(user).values("application_id"))
- | Q(id__in=live_oauth_refresh_tokens(user).values("application_id")),
- is_first_party=False,
- ).exists()
+ has_refresh_token = Exists(live_oauth_refresh_tokens(user).filter(application__is_first_party=False))
+ has_access_token = Exists(live_oauth_access_tokens(user).filter(application__is_first_party=False))
+ return User.objects.filter(pk=user.pk).filter(has_refresh_token | has_access_token).exists()
def lock_oauth_connection(*, user_id: int, application_id: uuid.UUID) -> None: