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(sso): match invites case-insensitively for saml and jit signups (#109032)

Alex Lider3ae8840659b9
diff --git a/posthog/api/signup.py b/posthog/api/signup.py
index 3ea9cc9ee98a..58dbbecf3a7a 100644
--- a/posthog/api/signup.py
+++ b/posthog/api/signup.py
@@ -866,7 +866,7 @@ def lookup_invite_for_saml(email: str, saml_relay_state: str) -> Optional[Organi
if config is None:
return None
return (
- OrganizationInvite.objects.filter(target_email=email, organization_id=config.organization_id)
+ OrganizationInvite.objects.filter(target_email__iexact=email, organization_id=config.organization_id)
.order_by("-created_at")
.first()
)
@@ -944,7 +944,7 @@ def process_social_domain_jit_provisioning_signup(
if not user:
try:
invite: OrganizationInvite = OrganizationInvite.objects.get(
- target_email=email, organization=domain_instance.organization
+ target_email__iexact=email, organization=domain_instance.organization
)
invite.validate(user=None, email=email)
# Capture before invite.use() deletes the invite row.
diff --git a/posthog/api/test/test_signup.py b/posthog/api/test/test_signup.py
index 2361f43c6cfb..1076d0298452 100644
--- a/posthog/api/test/test_signup.py
+++ b/posthog/api/test/test_signup.py
@@ -890,7 +890,13 @@ class TestSignupAPI(APIBaseTest):
) # show the user an error; operation not permitted
def run_test_for_allowed_domain(
- self, mock_sso_providers, mock_request, mock_capture, use_invite: bool = False, expired_invite: bool = False
+ self,
+ mock_sso_providers,
+ mock_request,
+ mock_capture,
+ use_invite: bool = False,
+ expired_invite: bool = False,
+ asserted_email: str = "[email protected]",
):
# Make sure Google Auth is valid for this test instance
mock_sso_providers.return_value = {"google-oauth2": True}
@@ -926,7 +932,7 @@ class TestSignupAPI(APIBaseTest):
mock_request.return_value.json.return_value = {
"email_verified": True,
"access_token": "123",
- "email": "[email protected]",
+ "email": asserted_email,
"sub": "123",
}
@@ -982,6 +988,18 @@ class TestSignupAPI(APIBaseTest):
def test_social_signup_with_allowed_domain_on_self_hosted(self, mock_sso_providers, mock_request, mock_capture):
self.run_test_for_allowed_domain(mock_sso_providers, mock_request, mock_capture)
+ @parameterized.expand(["[email protected]", "[email protected]"])
+ @patch("posthoganalytics.capture")
+ @mock.patch("social_core.backends.base.BaseAuth.request")
+ @mock.patch("posthog.api.authentication.get_instance_available_sso_providers")
+ @pytest.mark.ee
+ def test_social_signup_with_allowed_domain_uses_invite(
+ self, asserted_email, mock_sso_providers, mock_request, mock_capture
+ ):
+ self.run_test_for_allowed_domain(
+ mock_sso_providers, mock_request, mock_capture, use_invite=True, asserted_email=asserted_email
+ )
+
@mock.patch("social_core.backends.base.BaseAuth.request")
@mock.patch("posthog.api.authentication.get_instance_available_sso_providers")
@pytest.mark.ee
@@ -3497,13 +3515,14 @@ class TestSAMLInviteLookup(APIBaseTest):
assert config.saml_relay_state is not None
return config.saml_relay_state
- def test_finds_the_invite_for_the_config_that_signed_the_assertion(self):
+ @parameterized.expand(["[email protected]", "[email protected]"])
+ def test_finds_the_invite_for_the_config_that_signed_the_assertion(self, asserted_email):
identifier = self._saml_identifier_for("saml-invite.example.com")
invite = OrganizationInvite.objects.create(
organization=self.organization, target_email="[email protected]"
)
- found = lookup_invite_for_saml("[email protected]", identifier)
+ found = lookup_invite_for_saml(asserted_email, identifier)
assert found is not None
assert found.id == invite.id