: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(auth): skip pbkdf2 for tokens that cannot be personal api keys (#120088)
Paul D'Ambrada8405caa3d2
diff --git a/posthog/api/secret_revocation.py b/posthog/api/secret_revocation.pyindex 31100178fea9..2aedbb8c1e4c 100644--- a/posthog/api/secret_revocation.py+++ b/posthog/api/secret_revocation.py@@ -1,11 +1,10 @@-import refrom typing import Literalfrom posthog.api.personal_api_key import PersonalAPIKeySerializerfrom posthog.api.project_secret_api_key import revoke_exposed_project_secret_api_keyfrom posthog.dataclasses import frozenfrom posthog.models.oauth import find_oauth_access_token, find_oauth_refresh_token, revoke_oauth_token_session-from posthog.models.personal_api_key import find_personal_api_key+from posthog.models.personal_api_key import find_personal_api_key, looks_like_legacy_personal_api_keyfrom posthog.models.project_secret_api_key import find_project_secret_api_keyfrom posthog.models.utils import (OAUTH_ACCESS_TOKEN_PREFIX,@@ -108,7 +107,7 @@ _REVOKERS = {## Personal API keys are the one exception: those issued before the prefix existed have# none, so the map alone would leave the fleet's oldest keys unrevocable.-# _looks_like_legacy_personal_api_key covers them by shape instead. That widens what+# looks_like_legacy_personal_api_key covers them by shape instead. That widens what# reaches the personal-key lookup — any 43-character URL-safe string qualifies,# including legacy unprefixed Team.api_token values, which simply miss. That shape# check only narrows *which* strings pay the expensive lookup, not how many times a@@ -140,26 +139,11 @@ NON_PERSONAL_SECRET_PREFIXES = ()-_LEGACY_PERSONAL_API_KEY_PATTERN = re.compile(r"^[A-Za-z0-9_-]{43}$")---def _looks_like_legacy_personal_api_key(token: str) -> bool:- """Personal API keys created before the phx_ prefix (2021-06-23, commit- 6c9bb2db0fb) are bare secrets.token_urlsafe(32) output: exactly 43 URL-safe- base64 characters, no prefix. They're still valid today — PERSONAL_API_KEY_MODES_TO_TRY- exists specifically to authenticate them via their legacy hash. This exact- length+charset check keeps auto-detect's cost bound intact: only a token- matching this precise shape reaches the expensive personal-key lookup,- not every unrecognized string.- """- return bool(_LEGACY_PERSONAL_API_KEY_PATTERN.match(token))--def _detect_canonical_type(token: str) -> str | None:for prefix, canonical_type in _PREFIX_TO_CANONICAL_TYPE.items():if token.startswith(prefix):return canonical_type- if _looks_like_legacy_personal_api_key(token):+ if looks_like_legacy_personal_api_key(token):return CANONICAL_PERSONAL_API_KEYreturn Nonediff --git a/posthog/auth.py b/posthog/auth.pyindex 02dfe228de81..380b34fee114 100644--- a/posthog/auth.py+++ b/posthog/auth.py@@ -49,8 +49,8 @@ from posthog.models.organization import Organization, OrganizationMembershipfrom posthog.models.personal_api_key import (LEGACY_PERSONAL_API_KEY_SALT,PERSONAL_API_KEY_AUTH_COUNTER,- PERSONAL_API_KEY_MODES_TO_TRY,PersonalAPIKey,+ personal_api_key_modes_to_try,)from posthog.models.project_secret_api_key import ProjectSecretAPIKey, find_project_secret_api_keyfrom posthog.models.sharing_configuration import SharingConfiguration@@ -335,7 +335,7 @@ class PersonalAPIKeyAuthentication(ActivityCredentialMixin, authentication.BaseAmodes_tried = 0with tracer.start_as_current_span("posthog.auth.personal_api_key.db_lookup") as db_span:- for mode, iterations in PERSONAL_API_KEY_MODES_TO_TRY:+ for mode, iterations in personal_api_key_modes_to_try(personal_api_key):modes_tried += 1secure_value = hash_key_value(personal_api_key, mode=mode, legacy_salt=LEGACY_PERSONAL_API_KEY_SALT, iterations=iterationsdiff --git a/posthog/models/personal_api_key.py b/posthog/models/personal_api_key.pyindex acce1ecf44b1..988eba47a398 100644--- a/posthog/models/personal_api_key.py+++ b/posthog/models/personal_api_key.py@@ -1,3 +1,4 @@+import refrom typing import TYPE_CHECKING, Optionalfrom django.contrib.auth.hashers import PBKDF2PasswordHasher@@ -10,13 +11,16 @@ from prometheus_client import Counterfrom posthog.migration_helpers import deprecate_fieldfrom posthog.models.activity_logging.model_activity import ModelActivityMixin-from posthog.models.utils import EncryptionModeType, generate_random_token, hash_key_value+from posthog.models.utils import PERSONAL_API_KEY_PREFIX, EncryptionModeType, generate_random_token, hash_key_valueif TYPE_CHECKING:from posthog.models.organization import Organization-PERSONAL_API_KEY_MODES_TO_TRY: tuple[tuple[EncryptionModeType, Optional[int]], ...] = (- ("sha256", None), # Moved to simple hashing in 2024-02+HashMode = tuple[EncryptionModeType, Optional[int]]++CURRENT_PERSONAL_API_KEY_MODE: HashMode = ("sha256", None) # Moved to simple hashing in 2024-02+PERSONAL_API_KEY_MODES_TO_TRY: tuple[HashMode, ...] = (+ CURRENT_PERSONAL_API_KEY_MODE,("pbkdf2", 260000), # This is the iteration count used by PostHog since the beginning of time.("pbkdf2", 390000), # This is the iteration count used briefly on some API keys.)@@ -62,8 +66,33 @@ class PersonalAPIKey(ModelActivityMixin, models.Model):)+_LEGACY_PERSONAL_API_KEY_PATTERN = re.compile(r"^[A-Za-z0-9_-]{43}$")+++def looks_like_legacy_personal_api_key(token: str) -> bool:+ """Personal API keys created before the phx_ prefix (2021-06-23, commit+ 6c9bb2db0fb) are bare secrets.token_urlsafe(32) output: exactly 43 URL-safe+ base64 characters, no prefix. They're still valid today — PERSONAL_API_KEY_MODES_TO_TRY+ exists specifically to authenticate them via their legacy hash. This exact+ length+charset check keeps auto-detect's cost bound intact: only a token+ matching this precise shape reaches the expensive personal-key lookup,+ not every unrecognized string.+ """+ return bool(_LEGACY_PERSONAL_API_KEY_PATTERN.match(token))+++def personal_api_key_modes_to_try(token: str) -> tuple[HashMode, ...]:+ """Keys issued before 2024-02 can still carry a pbkdf2 hash, and those keys are either+ phx_-prefixed or legacy-shaped. Any other token can only match sha256, so it skips the+ pbkdf2 rounds that make a failed lookup cost hundreds of milliseconds.+ """+ if token.startswith(PERSONAL_API_KEY_PREFIX) or looks_like_legacy_personal_api_key(token):+ return PERSONAL_API_KEY_MODES_TO_TRY+ return (CURRENT_PERSONAL_API_KEY_MODE,)++def find_personal_api_key(token: str) -> tuple[PersonalAPIKey, str] | None:- for mode, iterations in PERSONAL_API_KEY_MODES_TO_TRY:+ for mode, iterations in personal_api_key_modes_to_try(token):secure_value = hash_key_value(token, mode=mode, legacy_salt=LEGACY_PERSONAL_API_KEY_SALT, iterations=iterations)try:obj = (diff --git a/posthog/test/test_api_key_search.py b/posthog/test/test_api_key_search.pyindex e5673ceccc1f..ce6553619ecb 100644--- a/posthog/test/test_api_key_search.py+++ b/posthog/test/test_api_key_search.py@@ -1,3 +1,5 @@+import secrets+from posthog.test.base import BaseTestfrom django.test import Client, override_settings@@ -7,7 +9,6 @@ from parameterized import parameterizedfrom posthog.models import PersonalAPIKeyfrom posthog.models.personal_api_key import LEGACY_PERSONAL_API_KEY_SALTfrom posthog.models.utils import (- generate_random_token,generate_random_token_personal,generate_random_token_secret,hash_key_value,@@ -65,7 +66,7 @@ class TestApiKeySearchView(BaseTest):])def test_finds_personal_api_key(self, _name: str, prefixed: bool, mode: str, iterations: int | None):- token = generate_random_token_personal() if prefixed else generate_random_token(32)+ token = generate_random_token_personal() if prefixed else secrets.token_urlsafe(32)self._create_personal_api_key(token, mode=mode, iterations=iterations)response = self._search(token)diff --git a/posthog/test/test_auth_spans.py b/posthog/test/test_auth_spans.pyindex dec489ace4db..c3f190efafa6 100644--- a/posthog/test/test_auth_spans.py+++ b/posthog/test/test_auth_spans.py@@ -5,6 +5,7 @@ from opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.sdk.trace.export import SimpleSpanProcessorfrom opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporterfrom parameterized import parameterized+from rest_framework.exceptions import AuthenticationFailedfrom rest_framework.request import Requestfrom rest_framework.test import APIRequestFactory@@ -116,3 +117,25 @@ class TestAuthSpans(BaseTest):assert len(db_lookup) == 1assert db_lookup[0].attributes["auth.hash_mode_used"] == "sha256"assert db_lookup[0].attributes["auth.modes_tried"] == 1++ @parameterized.expand(+ [+ ("prefixed_key", "phx_" + "a" * 47, 3),+ ("short_prefixed_key", "phx_" + "a" * 32, 3),+ ("unprefixed_legacy_key", "a" * 43, 3),+ ("project_token", "phc_" + "a" * 43, 1),+ ("wrong_length_unprefixed", "a" * 44, 1),+ ("jwt_like", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.c2lnbmF0dXJl", 1),+ ]+ )+ def test_unknown_key_only_pays_for_pbkdf2_when_it_looks_like_a_personal_api_key(+ self, _name, token, expected_modes_tried+ ):+ request = self.factory.get("/api/users/@me/", HTTP_AUTHORIZATION=f"Bearer {token}")++ with self.assertRaises(AuthenticationFailed):+ PersonalAPIKeyAuthentication().authenticate(request)++ db_lookup = self._spans_named("posthog.auth.personal_api_key.db_lookup")+ assert len(db_lookup) == 1+ assert db_lookup[0].attributes["auth.modes_tried"] == expected_modes_tried