Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

external_auth: Reject syncing delivery_email onto a system-bot address.

Alex Vandiver6f0dd7804b47
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index e1f79927a2d..3c2c41387dd 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -4184,6 +4184,53 @@ class SAMLAuthBackendTest(SocialAuthBase):
any("Can't sync email" in output for output in m.output),
)
+ def test_external_auth_id_saml_cross_realm_bot_email(self) -> None:
+ """Test that email is NOT synced when the target email is reserved
+ for a cross-realm system bot."""
+ hamlet = self.example_user("hamlet")
+
+ idps_config_dict = copy.deepcopy(settings.SOCIAL_AUTH_SAML_ENABLED_IDPS)
+ idps_config_dict["test_idp"]["attr_user_permanent_id"] = "uid"
+ uid_value = "testuid"
+
+ # Create ExternalAuthID for hamlet.
+ account_data_dict = self.get_account_data_dict(email=self.email, name=self.name)
+ with (
+ self.settings(SOCIAL_AUTH_SAML_ENABLED_IDPS=idps_config_dict),
+ self.assertLogs(self.logger_string, level="INFO"),
+ ):
+ result = self.social_auth_test(
+ account_data_dict,
+ subdomain="zulip",
+ extra_attributes={"uid": [uid_value]},
+ )
+ self.assertEqual(result.status_code, 302)
+
+ # Try to login with a cross-realm bot email - should find hamlet by
+ # ExternalAuthID but refuse to sync the email onto a reserved address.
+ bot_email = "[email protected]"
+ assert bot_email in settings.CROSS_REALM_BOT_EMAILS
+ bot_data = self.get_account_data_dict(email=bot_email, name=self.name)
+ with (
+ self.settings(SOCIAL_AUTH_SAML_ENABLED_IDPS=idps_config_dict),
+ self.assertLogs(self.logger_string, level="INFO") as m,
+ ):
+ result = self.social_auth_test(
+ bot_data,
+ subdomain="zulip",
+ extra_attributes={"uid": [uid_value]},
+ )
+ self.assertEqual(result.status_code, 302)
+ data = load_subdomain_token(result)
+ self.assertEqual(data["email"], hamlet.delivery_email)
+
+ hamlet.refresh_from_db()
+ # Email should NOT have changed.
+ self.assertEqual(hamlet.delivery_email, self.email)
+ self.assertTrue(
+ any("reserved for system bots" in output for output in m.output),
+ )
+
def test_external_auth_id_saml_deactivated_user(self) -> None:
"""Test that a deactivated user can't log in even with ExternalAuthID."""
hamlet = self.example_user("hamlet")
diff --git a/zproject/backends.py b/zproject/backends.py
index c19965e1f20..89eb6e4ec11 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -121,6 +121,7 @@ from zerver.models.users import (
PasswordTooWeakError,
get_user_by_delivery_email,
get_user_profile_by_id,
+ is_cross_realm_bot_email,
remote_user_to_email,
)
from zproject.settings_types import OIDCIdPConfigDict
@@ -816,7 +817,17 @@ def get_and_sync_user_profile_by_external_auth_id(
user_profile.delivery_email,
email,
)
- if (
+ if is_cross_realm_bot_email(email):
+ # Cross-realm bot emails are reserved for system bots; we
+ # must never let an external auth directory sync a regular
+ # user's delivery_email onto one, since is_cross_realm_bot_email
+ # is used as an authorization shortcut in several places.
+ logger.warning(
+ "Can't sync email for user %s: %s is reserved for system bots",
+ user_profile.id,
+ email,
+ )
+ elif (
UserProfile.objects.filter(realm=realm, delivery_email__iexact=email)
# The EXISTS query has a somewhat strange shape because we need
# to again consider the edge case where we might be simply