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): keep the session usable during a 2fa re-auth (#129012)

Yasen633517f7fc52
diff --git a/posthog/api/authentication.py b/posthog/api/authentication.py
index 8d8d9654ed60..a53708d872dc 100644
--- a/posthog/api/authentication.py
+++ b/posthog/api/authentication.py
@@ -429,7 +429,10 @@ class LoginSerializer(serializers.Serializer):
code="verified_domain_required",
)
- clear_two_factor_session_flags(request)
+ # A re-auth keeps the 2FA flag so that the signed-in session works while the user enters
+ # the second factor. Without the flag, enforced 2FA refuses every other request until logout.
+ if not was_authenticated_before_login_attempt:
+ clear_two_factor_session_flags(request)
if self._check_if_2fa_required(user):
request.session["user_authenticated_but_no_2fa"] = user.pk
diff --git a/posthog/api/test/test_authentication.py b/posthog/api/test/test_authentication.py
index 1f35d0f37f1b..2096b0950322 100644
--- a/posthog/api/test/test_authentication.py
+++ b/posthog/api/test/test_authentication.py
@@ -36,6 +36,7 @@ from rest_framework.request import Request
from rest_framework.test import APIRequestFactory
from social_django.models import UserSocialAuth
from two_factor.utils import totp_digits
+from two_factor.views.core import REMEMBER_COOKIE_PREFIX
from posthog.api.authentication import password_reset_token_generator, social_login_notification
from posthog.api.email_verification import is_email_verification_disabled
@@ -1077,6 +1078,42 @@ class TestTwoFactorAPI(APIBaseTest):
response = self.client.get("/api/users/@me/")
assert response.status_code == status.HTTP_401_UNAUTHORIZED, response.json()
+ def test_password_reauth_keeps_the_session_usable_until_the_2fa_step(self):
+ self.organization.enforce_2fa = True
+ self.organization.save()
+ self.organization_membership.level = OrganizationMembership.Level.ADMIN
+ self.organization_membership.save()
+ device = self.user.totpdevice_set.create(name="default", key=random_hex(), digits=6) # type: ignore
+ credentials = {"email": self.CONFIG_EMAIL, "password": self.CONFIG_PASSWORD}
+
+ now = datetime.now()
+ with time_machine.travel(now, tick=False):
+ assert self.client.post("/api/login", credentials).status_code == status.HTTP_401_UNAUTHORIZED
+ assert self.client.post("/api/login/token", {"token": totp_str(device.bin_key)}).status_code == 200
+
+ # A remembered device skips the 2FA step, so the re-auth below would never reach it.
+ for cookie_name in [name for name in self.client.cookies if name.startswith(REMEMBER_COOKIE_PREFIX)]:
+ del self.client.cookies[cookie_name]
+
+ with time_machine.travel(now + timedelta(seconds=settings.SESSION_SENSITIVE_ACTIONS_AGE + 10), tick=False):
+ response = self.client.patch("/api/organizations/@current", {"name": "new name"})
+ assert response.json()["code"] == "sensitive_action_required_reauth"
+
+ response = self.client.post("/api/login", credentials)
+ assert response.json()["code"] == "2fa_required"
+
+ response = self.client.get("/api/organizations/@current")
+ assert response.status_code == status.HTTP_200_OK, response.json()
+
+ response = self.client.patch("/api/organizations/@current", {"name": "new name"})
+ assert response.json()["code"] == "sensitive_action_required_reauth"
+
+ response = self.client.post("/api/login/token", {"token": totp_str(device.bin_key)})
+ assert response.status_code == status.HTTP_200_OK, response.json()
+
+ response = self.client.patch("/api/organizations/@current", {"name": "new name"})
+ assert response.status_code == status.HTTP_200_OK, response.json()
+
@parameterized.expand([("authenticator_code", "000000"), ("backup_code", "zzzz7777")])
def test_2fa_throttling(self, _name: str, wrong_token: str) -> None:
self.user.totpdevice_set.create(name="default", key=random_hex(), digits=6) # type: ignore