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(auth): match passkey session lookup to the model backend (#113790)

Tom Piccirello7e6afd9cc098
diff --git a/posthog/api/test/test_webauthn.py b/posthog/api/test/test_webauthn.py
index 8b9978ae5c74..6e60a9c7221a 100644
--- a/posthog/api/test/test_webauthn.py
+++ b/posthog/api/test/test_webauthn.py
@@ -14,6 +14,7 @@ from rest_framework.exceptions import AuthenticationFailed
from webauthn.helpers import bytes_to_base64url
from posthog.api.webauthn import WEBAUTHN_REGISTRATION_CHALLENGE_KEY, WebAuthnLoginViewSet, user_uuid_to_handle
+from posthog.auth import WebauthnBackend
from posthog.models import Organization, User
from posthog.models.identity_provider_config import IdentityProviderConfig
from posthog.models.linked_identity_provider_config import LinkedIdentityProviderConfig
@@ -256,6 +257,12 @@ class TestWebAuthnLogin(APIBaseTest):
self.assertEqual(me_response.status_code, status.HTTP_200_OK)
self.assertEqual(me_response.json()["email"], self.user.email)
+ def test_backend_does_not_restore_session_for_inactive_user(self):
+ # A queryset update skips the pre_save signal that revokes sessions on deactivation.
+ User.objects.filter(pk=self.user.pk).update(is_active=False)
+
+ self.assertIsNone(WebauthnBackend().get_user(self.user.pk))
+
@patch(
"posthog.api.webauthn.refuse_blocked_account",
side_effect=AuthenticationFailed("We couldn't sign you in.", code="access_blocked"),
diff --git a/posthog/auth.py b/posthog/auth.py
index e1b61f37fa2b..a4074f505075 100644
--- a/posthog/auth.py
+++ b/posthog/auth.py
@@ -1598,7 +1598,7 @@ class WebauthnBackend(BaseBackend):
Required by Django's authentication system to load the user on subsequent requests.
"""
try:
- return User.objects.get(pk=user_id)
+ return User.objects.get(pk=user_id, is_active=True)
except User.DoesNotExist:
return None