Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zilencer: Disambiguate no MX records from the domain not existing.
Alex Vandivere5a0b3b3c543
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex 49d85436ba6..bd206c0bffc 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -8,7 +8,6 @@ from typing import Any, Dict, Iterator, List, Mapping, Optional, Tuple, Unionfrom unittest import mock, skipUnlessimport aioapns-import DNSimport orjsonimport responsesimport time_machine@@ -19,6 +18,7 @@ from django.http.response import ResponseHeadersfrom django.test import override_settingsfrom django.utils.crypto import get_random_stringfrom django.utils.timezone import now+from dns.resolver import NoAnswer as DNSNoAnswerfrom requests.exceptions import ConnectionErrorfrom requests.models import PreparedRequestfrom typing_extensions import override@@ -5247,17 +5247,24 @@ class PushBouncerSignupTest(ZulipTestCase):self.assert_json_error(result, "Please use your real email address.")request["contact_email"] = "[email protected]"- with mock.patch("DNS.mxlookup", side_effect=DNS.Base.ServerError("test", 1)):+ with mock.patch("zilencer.views.dns_resolver.Resolver") as resolver:+ resolver.return_value.resolve.side_effect = DNSNoAnswer+ resolver.return_value.resolve_name.return_value = ["whee"]result = self.client_post("/api/v1/remotes/server/register", request)self.assert_json_error(- result, "zulip.com does not exist or is not configured to accept email."+ result, "zulip.com is invalid because it does not have any MX records")- with mock.patch("DNS.mxlookup", return_value=[]):+ with mock.patch("zilencer.views.dns_resolver.Resolver") as resolver:+ resolver.return_value.resolve.side_effect = DNSNoAnswer+ resolver.return_value.resolve_name.side_effect = DNSNoAnswerresult = self.client_post("/api/v1/remotes/server/register", request)- self.assert_json_error(- result, "zulip.com does not exist or is not configured to accept email."- )+ self.assert_json_error(result, "zulip.com does not exist")++ with mock.patch("zilencer.views.dns_resolver.Resolver") as resolver:+ resolver.return_value.resolve.return_value = ["whee"]+ result = self.client_post("/api/v1/remotes/server/register", request)+ self.assert_json_success(result)class TestUserPushIdentityCompat(ZulipTestCase):diff --git a/zilencer/views.py b/zilencer/views.pyindex d9f09cc1800..3ebf445d0ae 100644--- a/zilencer/views.py+++ b/zilencer/views.py@@ -5,7 +5,6 @@ from email.headerregistry import Addressfrom typing import Any, Dict, List, Optional, Type, TypedDict, TypeVar, Unionfrom uuid import UUID-import DNSimport orjsonfrom django.conf import settingsfrom django.core.exceptions import ValidationError@@ -18,6 +17,8 @@ from django.utils.timezone import now as timezone_nowfrom django.utils.translation import gettext as _from django.utils.translation import gettext as err_from django.views.decorators.csrf import csrf_exempt+from dns import resolver as dns_resolver+from dns.exception import DNSExceptionfrom pydantic import BaseModel, ConfigDict, Json, StringConstraintsfrom pydantic.functional_validators import AfterValidatorfrom typing_extensions import Annotated@@ -170,19 +171,25 @@ def register_remote_server(raise JsonableError(_("Invalid email address."))# Check if the domain has an MX record+ resolver = dns_resolver.Resolver()+ resolver.timeout = 3+ dns_mx_check_successful = Falsetry:- records = DNS.mxlookup(contact_email_domain)- dns_mx_check_successful = True- if not records:- dns_mx_check_successful = False- except DNS.Base.ServerError:- dns_mx_check_successful = False+ if resolver.resolve(contact_email_domain, "MX"):+ dns_mx_check_successful = True+ except DNSException:+ passif not dns_mx_check_successful:- raise JsonableError(- _("{domain} does not exist or is not configured to accept email.").format(- domain=contact_email_domain+ # Check if the A/AAAA exist, for better error reporting+ try:+ resolver.resolve_name(contact_email_domain)+ raise JsonableError(+ _("{domain} is invalid because it does not have any MX records").format(+ domain=contact_email_domain+ ))- )+ except DNSException:+ raise JsonableError(_("{domain} does not exist").format(domain=contact_email_domain))try:validate_uuid(zulip_org_id)