From 4712031528263661db04ffaaa3de1cea96e9fabf Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Tue, 27 Feb 2024 15:16:50 +0000 Subject: [PATCH] email_validation: Merge two implementations of a disposable-email check. Only one of them properly checked for improperly-formed email addresses, thanks to #22723. --- zerver/forms.py | 11 ++++------- zerver/lib/email_validation.py | 8 +++++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/zerver/forms.py b/zerver/forms.py index fdf253787b..27181eb8f5 100644 --- a/zerver/forms.py +++ b/zerver/forms.py @@ -1,6 +1,5 @@ import logging import re -from email.errors import HeaderParseError from email.headerregistry import Address from typing import Any, Dict, List, Optional, Tuple @@ -26,10 +25,11 @@ from zerver.actions.user_settings import do_change_password from zerver.lib.email_validation import ( email_allowed_for_realm, email_reserved_for_system_bots_error, + validate_is_not_disposable, ) from zerver.lib.exceptions import JsonableError, RateLimitedError from zerver.lib.i18n import get_language_list -from zerver.lib.name_restrictions import is_disposable_domain, is_reserved_subdomain +from zerver.lib.name_restrictions import is_reserved_subdomain from zerver.lib.rate_limiter import RateLimitedObject, rate_limit_request_by_ip from zerver.lib.send_email import FromAddress, send_email from zerver.lib.soft_deactivation import queue_soft_reactivation @@ -126,11 +126,8 @@ def email_not_system_bot(email: str) -> None: def email_is_not_disposable(email: str) -> None: try: - domain = Address(addr_spec=email).domain - except (HeaderParseError, ValueError): - raise ValidationError(_("Please use your real email address.")) - - if is_disposable_domain(domain): + validate_is_not_disposable(email) + except DisposableEmailError: raise ValidationError(_("Please use your real email address.")) diff --git a/zerver/lib/email_validation.py b/zerver/lib/email_validation.py index 31890dbfbd..8cd2340a77 100644 --- a/zerver/lib/email_validation.py +++ b/zerver/lib/email_validation.py @@ -1,3 +1,4 @@ +from email.errors import HeaderParseError from email.headerregistry import Address from typing import Callable, Dict, Optional, Set, Tuple @@ -18,7 +19,12 @@ from zerver.models.users import get_users_by_delivery_email, is_cross_realm_bot_ def validate_is_not_disposable(email: str) -> None: - if is_disposable_domain(Address(addr_spec=email).domain): + try: + domain = Address(addr_spec=email).domain + except (HeaderParseError, ValueError): + raise DisposableEmailError + + if is_disposable_domain(domain): raise DisposableEmailError