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(security): only fire credential review for partner-provisioned PATs (#59751)

Matt Brookeraee7dfaf1e34
diff --git a/posthog/api/cli_auth.py b/posthog/api/cli_auth.py
index d1a5f8ee9871..af9558f60228 100644
--- a/posthog/api/cli_auth.py
+++ b/posthog/api/cli_auth.py
@@ -242,6 +242,8 @@ class CLIAuthViewSet(viewsets.ViewSet):
team_name_truncated = team.name[:max_team_name_len] if len(team.name) > max_team_name_len else team.name
label = f"CLI - {team_name_truncated} - {timestamp}"
+ had_prior_pat = PersonalAPIKey.objects.filter(user=user).exists()
+
PersonalAPIKey.objects.create(
user=user,
label=label,
@@ -250,6 +252,16 @@ class CLIAuthViewSet(viewsets.ViewSet):
scopes=scopes,
)
+ # User explicitly authorized this CLI via SessionAuthentication (see
+ # CLIAuthViewSet.get_authenticators - authorize is session-only). If they
+ # had no prior PATs, treat the CLI key as already-acknowledged so the
+ # review interstitial doesn't fire for the key they just minted. Skip
+ # when prior PATs exist - those may be partner-issued and still warrant
+ # the review.
+ if not had_prior_pat and user.credentials_reviewed_at is None:
+ user.credentials_reviewed_at = timezone.now()
+ user.save(update_fields=["credentials_reviewed_at"])
+
# Mark device as authorized and store the API key
device_data["status"] = "authorized"
device_data["personal_api_key"] = api_key_value
diff --git a/posthog/api/personal_api_key.py b/posthog/api/personal_api_key.py
index 4c193a2edf69..eebe80c22c14 100644
--- a/posthog/api/personal_api_key.py
+++ b/posthog/api/personal_api_key.py
@@ -159,6 +159,24 @@ class PersonalAPIKeySerializer(serializers.ModelSerializer):
user=user, secure_value=secure_value, mask_value=mask_value, **validated_data
)
personal_api_key._value = value # type: ignore
+ # User created their FIRST PAT themselves through a session, so the credential
+ # review interstitial has nothing partner-issued to surface for them - mark it
+ # acknowledged. Three gates, all load-bearing:
+ # - count == 0: no pre-existing PATs, so this is the user's first. If they
+ # already had keys, those might be partner-issued and still awaiting review,
+ # so don't stamp.
+ # - SessionAuthentication: PAT-bearer auth would let an attacker holding a
+ # partner-issued PAT mint another PAT to silently dismiss the victim's
+ # review screen. Same constraint as credentials_review_complete.
+ # - credentials_reviewed_at IS NULL: don't clobber a real review timestamp.
+ request = self.context["request"]
+ if (
+ count == 0
+ and user.credentials_reviewed_at is None
+ and isinstance(getattr(request, "successful_authenticator", None), SessionAuthentication)
+ ):
+ user.credentials_reviewed_at = timezone.now()
+ user.save(update_fields=["credentials_reviewed_at"])
return personal_api_key
def roll(self, personal_api_key: PersonalAPIKey) -> PersonalAPIKey:
diff --git a/posthog/management/commands/setup_dev.py b/posthog/management/commands/setup_dev.py
index d7398aec7c4f..e61cbdf4cc6f 100644
--- a/posthog/management/commands/setup_dev.py
+++ b/posthog/management/commands/setup_dev.py
@@ -1,5 +1,6 @@
from django.core.management.base import BaseCommand
from django.db import transaction
+from django.utils import timezone
from posthog.demo.legacy import ORGANIZATION_NAME, TEAM_NAME, create_demo_data
from posthog.models import EventProperty, PersonalAPIKey, Plugin, PluginConfig, PluginSourceFile, Team, User
@@ -54,6 +55,13 @@ class Command(BaseCommand):
secure_value=hash_key_value("e2e_demo_api_key"),
scopes=["*"],
)
+ # Dev seed - mark the bootstrapped user's credential review as complete so
+ # the seed PAT doesn't bounce them into the partner-credential review screen
+ # on first login.
+ if user.credentials_reviewed_at is None:
+ user.credentials_reviewed_at = timezone.now()
+ user.save(update_fields=["credentials_reviewed_at"])
+
if not options["no_data"]:
create_demo_data(team)
diff --git a/posthog/management/commands/setup_local_api_key.py b/posthog/management/commands/setup_local_api_key.py
index 700277aabd93..19d318aaca47 100644
--- a/posthog/management/commands/setup_local_api_key.py
+++ b/posthog/management/commands/setup_local_api_key.py
@@ -12,6 +12,7 @@ Safety: Only runs when DEBUG=True and CLOUD_DEPLOYMENT is unset.
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
+from django.utils import timezone
from posthog.models import User
from posthog.models.personal_api_key import PersonalAPIKey
@@ -71,6 +72,12 @@ class Command(BaseCommand):
existing_key = PersonalAPIKey.objects.filter(secure_value=secure_value).first()
if existing_key:
+ # Idempotent rerun: stamp credentials_reviewed_at on the owner if it's
+ # still null. Without this, local DBs that already have the dev key from
+ # before this fix landed will still trip the review interstitial.
+ if existing_key.user.credentials_reviewed_at is None:
+ existing_key.user.credentials_reviewed_at = timezone.now()
+ existing_key.user.save(update_fields=["credentials_reviewed_at"])
if add_scopes:
current = set(existing_key.scopes)
merged = sorted(current | set(add_scopes))
@@ -101,6 +108,13 @@ class Command(BaseCommand):
scopes=create_scopes,
)
+ # The dev key is a local-only convenience, not a partner-issued credential the
+ # user needs to review. Stamp credentials_reviewed_at so the new key doesn't
+ # bounce the user into the credential review screen on their next login.
+ if user.credentials_reviewed_at is None:
+ user.credentials_reviewed_at = timezone.now()
+ user.save(update_fields=["credentials_reviewed_at"])
+
print(f"Created personal API key for '{email}'")
if create_scopes:
print(f"Scopes: {', '.join(create_scopes)}")