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: don't auto-login SSO-enforced users on email verification (#62453)

Alex Lider23b158efb5be
diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.py
index 9714a023d53c..71bfcd7557fd 100644
--- a/posthog/api/test/test_user.py
+++ b/posthog/api/test/test_user.py
@@ -844,6 +844,44 @@ class TestUserAPI(APIBaseTest):
assert UserSocialAuth.objects.filter(id=social_auth.id).exists()
mock_send_email_change_emails.assert_not_called()
+ @patch("posthog.api.user.login")
+ @patch("posthog.tasks.email.send_email_change_emails.delay")
+ def test_email_change_verification_skips_auto_login_for_sso_enforced_domain(self, _, mock_login):
+ self.user.pending_email = "[email protected]"
+ self.user.save()
+
+ with patch(
+ "posthog.models.organization_domain.OrganizationDomainManager.get_sso_enforcement_for_email_address",
+ side_effect=lambda email, organization=None: "google-oauth2" if email == "[email protected]" else None,
+ ):
+ token = email_verification_token_generator.make_token(self.user)
+ response = self.client.post("/api/users/verify_email/", {"uuid": self.user.uuid, "token": token})
+
+ assert response.status_code == status.HTTP_200_OK
+ assert response.json()["requires_sso"] is True
+ self.user.refresh_from_db()
+ assert self.user.email == "[email protected]"
+ # No password-backend session is handed out for an SSO-enforced account; they must log in via SSO.
+ mock_login.assert_not_called()
+
+ @patch("posthog.api.user.login")
+ @patch("posthog.tasks.email.send_email_change_emails.delay")
+ def test_initial_email_verification_skips_auto_login_for_sso_enforced_domain(self, _, mock_login):
+ self.user.email = "[email protected]"
+ self.user.pending_email = None
+ self.user.save()
+
+ with patch(
+ "posthog.models.organization_domain.OrganizationDomainManager.get_sso_enforcement_for_email_address",
+ side_effect=lambda email, organization=None: "google-oauth2" if email == "[email protected]" else None,
+ ):
+ token = email_verification_token_generator.make_token(self.user)
+ response = self.client.post("/api/users/verify_email/", {"uuid": self.user.uuid, "token": token})
+
+ assert response.status_code == status.HTTP_200_OK
+ assert response.json()["requires_sso"] is True
+ mock_login.assert_not_called()
+
@patch("posthog.api.user.is_email_available", return_value=True)
@patch("posthog.tasks.email.send_email_change_emails.delay")
def test_no_notifications_when_user_email_is_changed_and_only_case_differs(
diff --git a/posthog/api/user.py b/posthog/api/user.py
index 9a1b30ede6f0..98a83d4483b4 100644
--- a/posthog/api/user.py
+++ b/posthog/api/user.py
@@ -934,6 +934,11 @@ class UserViewSet(
if default_device(user) or passkeys_enabled_for_2fa:
return Response({"success": True, "token": token, "requires_2fa": True})
+ # Don't hand a non-SSO session to an account whose domain enforces SSO — verifying an email
+ # must not become a password-backend login path around the IdP. The user logs in via SSO.
+ if OrganizationDomain.objects.get_sso_enforcement_for_email_address(user.email):
+ return Response({"success": True, "token": token, "requires_sso": True})
+
login(self.request, user, backend="django.contrib.auth.backends.ModelBackend")
set_two_factor_verified_in_session(self.request)
report_user_logged_in(user)