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.

auth: Reject OIDC logins with an unverified email address.

Mateusz Manderab40f191a8c71
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index 0eb518635da..68f03e87a77 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -5225,6 +5225,40 @@ class GenericOpenIdConnectTest(SocialAuthBaseWithSyncAttrTest):
expect_full_name_prepopulated=False,
)
+ def test_social_auth_with_email_verified_claim(self) -> None:
+ account_data_dict = self.get_account_data_dict(email=self.email, name=self.name)
+ # An IdP explicitly reporting the email as unverified must be rejected.
+ account_data_dict["email_verified"] = False
+ subdomain = "zulip"
+ realm = get_realm(subdomain)
+ with self.assertLogs(self.logger_string, level="WARNING") as m:
+ result = self.social_auth_test(account_data_dict, subdomain=subdomain)
+ self.assertEqual(result.status_code, 302)
+ self.assertEqual(result["Location"], realm.url + "/login/")
+ self.assertEqual(
+ m.output,
+ [
+ self.logger_output(
+ "Social auth ({}) failed because user has no verified emails".format(
+ "OpenID Connect"
+ ),
+ "warning",
+ )
+ ],
+ )
+
+ # An explicit email_verified=True claim is accepted. The case where the
+ # optional claim is absent entirely is covered by the other success
+ # tests in this class, which don't set it.
+ account_data_dict = self.get_account_data_dict(email=self.email, name=self.name)
+ account_data_dict["email_verified"] = True
+
+ result = self.social_auth_test(account_data_dict, subdomain="zulip")
+ data = load_subdomain_token(result)
+ self.assertEqual(data["email"], self.example_email("hamlet"))
+ self.assertEqual(data["subdomain"], "zulip")
+ self.assertEqual(result.status_code, 302)
+
@override
def test_social_auth_no_key(self) -> None:
"""
diff --git a/zproject/backends.py b/zproject/backends.py
index 86c5cfc0567..fbf07bfab9c 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -4162,10 +4162,29 @@ class GenericOpenIdConnectBackend(SocialAuthMixin, OpenIdConnectAuth):
if value is not None and (isinstance(value, str) and value.strip() != ""):
extra_attrs[extra_attr_name] = value
- result = {**user_details, "extra_attrs": extra_attrs}
+ result = {
+ **user_details,
+ "email_verified": get_value("email_verified"),
+ "extra_attrs": extra_attrs,
+ }
self.logger.debug("get_user_details for <%s>: %s", self.name, result)
return result
+ def get_verified_emails(self, *args: Any, **kwargs: Any) -> list[str]:
+ # Unlike the shared social IdPs (Google/GitHub), a generic OIDC IdP
+ # is a single, administrator-configured identity source. email_verified
+ # is an optional OIDC claim: when the IdP omits it, we accept the email.
+ # We reject only when the IdP explicitly tells us the address is
+ # reported as unverified, since otherwise a user could log in with
+ # an email they have not proven they control.
+ # We expected this to be a very rare situation - a reasonably-configured
+ # IdP for OIDC should only send us trustable email values.
+ details = kwargs["details"]
+ email_verified = details.get("email_verified")
+ if email_verified is not None and not email_verified:
+ return []
+ return [details["email"]]
+
@override
def get_key_and_secret(self) -> tuple[str, str]:
client_id = self.settings_dict.get("client_id", "")