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): allow email change between an org's SSO-enforced domains (#62392)

Alex Lider80f6478a9056
diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.py
index 53beff218dfc..9714a023d53c 100644
--- a/posthog/api/test/test_user.py
+++ b/posthog/api/test/test_user.py
@@ -29,6 +29,7 @@ from posthog.models import Team, User
from posthog.models.instance_setting import set_instance_setting
from posthog.models.oauth import OAuthAccessToken, OAuthApplication
from posthog.models.organization import Organization, OrganizationMembership
+from posthog.models.organization_domain import OrganizationDomain
from posthog.models.personal_api_key import PersonalAPIKey
from posthog.models.utils import generate_random_token_personal, hash_key_value
@@ -755,6 +756,75 @@ class TestUserAPI(APIBaseTest):
assert self.user.pending_email is None
mock_send_email_change_emails.assert_not_called()
+ @patch("posthog.api.user.is_email_available", return_value=True)
+ @patch("posthog.api.user.EmailVerifier.create_token_and_send_email_verification")
+ def test_email_change_allowed_between_two_sso_enforced_domains_of_same_org(
+ self,
+ mock_send_email_verification,
+ mock_is_email_available,
+ ):
+ self.user.email = "[email protected]"
+ self.user.save()
+ for domain in ("example.com", "example.org"):
+ OrganizationDomain.objects.create(
+ organization=self.organization,
+ domain=domain,
+ verified_at=timezone.now(),
+ sso_enforcement="google-oauth2",
+ )
+
+ with patch(
+ "posthog.models.organization_domain.OrganizationDomainManager.get_sso_enforcement_for_email_address",
+ side_effect=lambda email, organization=None: "google-oauth2"
+ if email.split("@")[-1] in ("example.com", "example.org")
+ else None,
+ ):
+ with self.is_cloud(True):
+ response = self.client.patch("/api/users/@me/", {"email": "[email protected]"})
+
+ assert response.status_code == status.HTTP_200_OK
+ self.user.refresh_from_db()
+ assert self.user.pending_email == "[email protected]"
+ mock_send_email_verification.assert_called_once()
+
+ @patch("posthog.api.user.is_email_available", return_value=True)
+ @patch("posthog.tasks.email.send_email_change_emails.delay")
+ def test_email_change_blocked_to_sso_enforced_domain_of_another_org(
+ self,
+ mock_send_email_change_emails,
+ mock_is_email_available,
+ ):
+ self.user.email = "[email protected]"
+ self.user.save()
+ OrganizationDomain.objects.create(
+ organization=self.organization,
+ domain="example.com",
+ verified_at=timezone.now(),
+ sso_enforcement="google-oauth2",
+ )
+ other_org = Organization.objects.create(name="Attacker Org")
+ OrganizationDomain.objects.create(
+ organization=other_org,
+ domain="example.net",
+ verified_at=timezone.now(),
+ sso_enforcement="google-oauth2",
+ )
+
+ with patch(
+ "posthog.models.organization_domain.OrganizationDomainManager.get_sso_enforcement_for_email_address",
+ side_effect=lambda email, organization=None: "google-oauth2"
+ if email.split("@")[-1] in ("example.com", "example.net")
+ else None,
+ ):
+ with self.is_cloud(True):
+ response = self.client.patch("/api/users/@me/", {"email": "[email protected]"})
+
+ assert response.status_code == status.HTTP_400_BAD_REQUEST
+ assert response.json()["code"] == "sso_enforced_current_email"
+ self.user.refresh_from_db()
+ assert self.user.email == "[email protected]"
+ assert self.user.pending_email is None
+
@patch("posthog.tasks.email.send_email_change_emails.delay")
def test_verify_email_without_pending_email_keeps_social_auth_connections(self, mock_send_email_change_emails):
social_auth = UserSocialAuth.objects.create(
diff --git a/posthog/api/user.py b/posthog/api/user.py
index 897f423758cc..9a1b30ede6f0 100644
--- a/posthog/api/user.py
+++ b/posthog/api/user.py
@@ -662,14 +662,28 @@ class UserSerializer(serializers.ModelSerializer):
and is_email_available()
):
new_email = validated_data["email"]
+ # Moving between two SSO-enforced domains of the same org is a domain migration, not an SSO bypass.
+ # SSO enforcement can only be set on a verified domain, so an enforced domain is always verified.
+ current_sso_enforced = OrganizationDomain.objects.get_sso_enforcement_for_email_address(instance.email)
+ new_sso_enforced = OrganizationDomain.objects.get_sso_enforcement_for_email_address(new_email)
+ current_domain = OrganizationDomain.objects.get_verified_for_email_address(instance.email)
+ new_domain = OrganizationDomain.objects.get_verified_for_email_address(new_email)
+ is_same_org_migration = (
+ bool(current_sso_enforced)
+ and bool(new_sso_enforced)
+ and current_domain is not None
+ and new_domain is not None
+ and current_domain.organization_id == new_domain.organization_id
+ )
+
# Block bypass: a user on an SSO-enforced domain can't move off of it.
- if OrganizationDomain.objects.get_sso_enforcement_for_email_address(instance.email):
+ if current_sso_enforced and not is_same_org_migration:
raise serializers.ValidationError(
"You can't change your email because SSO is enforced on your current email's domain.",
code="sso_enforced_current_email",
)
# Block lockout: moving to an SSO-enforced domain blocks password reset and login.
- if OrganizationDomain.objects.get_sso_enforcement_for_email_address(new_email):
+ if new_sso_enforced and not is_same_org_migration:
raise serializers.ValidationError(
"You can't change your email to a domain where SSO is enforced.",
code="sso_enforced_new_email",