: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): make password login case-insensitive on email (#57880)
Alex Liderff34272f8836
diff --git a/posthog/models/test/test_user_model.py b/posthog/models/test/test_user_model.pyindex 00315a1d9981..3cbe57ca8f1f 100644--- a/posthog/models/test/test_user_model.py+++ b/posthog/models/test/test_user_model.py@@ -1,3 +1,5 @@+import datetime+from posthog.test.base import BaseTestfrom posthog.constants import AvailableFeature@@ -145,3 +147,40 @@ class TestUser(BaseTest):self.assertFalse(loaded._original_is_active)self.assertEqual(loaded._original_is_active, loaded.is_active)++ def test_get_by_natural_key_exact_match(self):+ user = User.objects.create(email="[email protected]")+ self.assertEqual(User.objects.get_by_natural_key("[email protected]"), user)++ def test_get_by_natural_key_is_case_insensitive(self):+ user = User.objects.create(email="[email protected]")++ self.assertEqual(User.objects.get_by_natural_key("[email protected]"), user)+ self.assertEqual(User.objects.get_by_natural_key("[email protected]"), user)+ self.assertEqual(User.objects.get_by_natural_key("[email protected]"), user)++ def test_get_by_natural_key_raises_does_not_exist_when_missing(self):+ with self.assertRaises(User.DoesNotExist):+ User.objects.get_by_natural_key("[email protected]")++ def test_get_by_natural_key_finds_inactive_user(self):+ # Active-state filtering is the responsibility of ModelBackend.user_can_authenticate, not+ # this lookup. Mirror Django's default get_by_natural_key, which doesn't filter by is_active.+ user = User.objects.create(email="[email protected]", is_active=False)+ self.assertEqual(User.objects.get_by_natural_key("[email protected]"), user)++ def test_get_by_natural_key_with_multiple_case_variants_picks_most_recent_login(self):+ older = User.objects.create(email="[email protected]")+ older.last_login = datetime.datetime(2024, 1, 1, tzinfo=datetime.UTC)+ older.save(update_fields=["last_login"])++ newer = User.objects.create(email="[email protected]")+ newer.last_login = datetime.datetime(2025, 1, 1, tzinfo=datetime.UTC)+ newer.save(update_fields=["last_login"])++ # Exact match wins over case-insensitive fallback when a row matches the typed casing.+ self.assertEqual(User.objects.get_by_natural_key("[email protected]"), older)+ self.assertEqual(User.objects.get_by_natural_key("[email protected]"), newer)++ # When the typed casing matches no row exactly, fallback picks the most recent login.+ self.assertEqual(User.objects.get_by_natural_key("[email protected]"), newer)diff --git a/posthog/models/user.py b/posthog/models/user.pyindex 3277f4bec8d2..58afb83f3afd 100644--- a/posthog/models/user.py+++ b/posthog/models/user.py@@ -12,7 +12,7 @@ from rest_framework.exceptions import ValidationErrorfrom posthog.cloud_utils import get_cached_instance_license, is_cloudfrom posthog.constants import AvailableFeaturefrom posthog.exceptions_capture import capture_exception-from posthog.helpers.email_utils import EmailNormalizer+from posthog.helpers.email_utils import EmailLookupHandler, EmailNormalizerfrom posthog.models.activity_logging.model_activity import ModelActivityMixinfrom posthog.settings import INSTANCE_TAG, SITE_URLfrom posthog.utils import get_instance_realm@@ -90,6 +90,14 @@ class UserManager(BaseUserManager):use_in_migrations = True+ def get_by_natural_key(self, username: str | None) -> "User": # type: ignore[override]+ # Case-insensitive lookup, ModelBackend.authenticate calls this method,+ # is_active filtering happens later in ModelBackend.user_can_authenticate, so we don't filter on it here.+ user = EmailLookupHandler.get_user_by_email(username, is_active=None) if username else None+ if user is None:+ raise User.DoesNotExist("User with that email does not exist.")+ return user+def create_user(self, email: str, password: Optional[str], first_name: str, **extra_fields) -> "User":"""Create and save a User with the given email and password."""if email is None: