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.

email_validation: Reject domains that can't be IDNA-encoded.

Alex Vandiver9ff17c9e0c9c
diff --git a/pyproject.toml b/pyproject.toml
index f81e4dd0a78..350e8091b64 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -129,6 +129,9 @@ prod = [
# For checking whether email of the user is from a disposable email provider.
"disposable-email-domains",
+ # For validating that email domains can be IDNA-encoded.
+ "idna",
+
# Needed for parsing YAML with JSON references from the REST API spec files
"jsonref",
diff --git a/uv.lock b/uv.lock
index 2265b070b56..6b7c410ef72 100644
--- a/uv.lock
+++ b/uv.lock
@@ -9,7 +9,7 @@ resolution-markers = [
]
[options]
-exclude-newer = "2026-04-23T17:19:05.217120702Z"
+exclude-newer = "2026-04-27T18:33:44.012006393Z"
exclude-newer-span = "PT24H"
[[package]]
@@ -6551,6 +6551,7 @@ dev = [
{ name = "google-re2-stubs" },
{ name = "greenlet" },
{ name = "html2text" },
+ { name = "idna" },
{ name = "ipython" },
{ name = "jinja2" },
{ name = "jsonref" },
@@ -6691,6 +6692,7 @@ prod = [
{ name = "google-re2" },
{ name = "greenlet" },
{ name = "html2text" },
+ { name = "idna" },
{ name = "ipython" },
{ name = "jinja2" },
{ name = "jsonref" },
@@ -6795,6 +6797,7 @@ dev = [
{ name = "google-re2-stubs" },
{ name = "greenlet" },
{ name = "html2text" },
+ { name = "idna" },
{ name = "ipython", specifier = "<9" },
{ name = "jinja2" },
{ name = "jsonref" },
@@ -6937,6 +6940,7 @@ prod = [
{ name = "google-re2" },
{ name = "greenlet" },
{ name = "html2text" },
+ { name = "idna" },
{ name = "ipython", specifier = "<9" },
{ name = "jinja2" },
{ name = "jsonref" },
diff --git a/version.py b/version.py
index 154abfc81eb..e3abb636073 100644
--- a/version.py
+++ b/version.py
@@ -48,4 +48,4 @@ API_FEATURE_LEVEL = 502
# historical commits sharing the same major version, in which case a
# minor version bump suffices.
-PROVISION_VERSION = (377, 1) # bumped 2026-04-22 to upgrade django-bitfield
+PROVISION_VERSION = (377, 2) # bumped 2026-04-28 to add idna
diff --git a/zerver/lib/email_validation.py b/zerver/lib/email_validation.py
index ad068097400..454df9bbf54 100644
--- a/zerver/lib/email_validation.py
+++ b/zerver/lib/email_validation.py
@@ -2,6 +2,7 @@ from collections.abc import Callable
from email.errors import HeaderParseError
from email.headerregistry import Address
+import idna
from django.core import validators
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _
@@ -103,6 +104,17 @@ def validate_email_is_valid(
except ValidationError:
return _("Invalid address.")
+ # Django's EmailValidator is looser than IDNA 2008 about what it
+ # accepts in a domain, and also accepts RFC 5321 address-literal
+ # domains (user@[192.168.0.1]) which we don't want to deliver to.
+ # Require the domain to IDNA-encode cleanly, which is what SMTP
+ # delivery will need to do.
+ try:
+ domain = Address(addr_spec=email).domain
+ idna.encode(domain, uts46=True)
+ except (HeaderParseError, ValueError, idna.IDNAError):
+ return _("Invalid address.")
+
try:
validate_email_allowed_in_realm(email)
except DomainNotAllowedForRealmError:
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index 2807b1799d8..08568925cc0 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -9244,6 +9244,25 @@ class EmailValidatorTestCase(ZulipTestCase):
self.assertEqual(True, is_deactivated)
self.assertEqual(error, "Account has been deactivated.")
+ def test_reject_malformed_domain(self) -> None:
+ realm = self.example_user("hamlet").realm
+ validator = get_realm_email_validator(realm)
+
+ for email in [
+ # Trailing Unicode format characters.
+ "[email protected]\u202c\u200f",
+ # Domain label starting with a hyphen.
+ # Empty label.
+ # IP address literal.
+ "user@[192.168.0.1]",
+ ]:
+ self.assertEqual(validate_email_is_valid(email, validator), "Invalid address.")
+
+ # Non-ASCII IDN domains are still accepted.
+ self.assertIsNone(validate_email_is_valid("user@münchen.de", validator))
+
class LDAPBackendTest(ZulipTestCase):
@override_settings(AUTHENTICATION_BACKENDS=("zproject.backends.ZulipLDAPAuthBackend",))