diff --git a/CHANGELOG.md b/CHANGELOG.md index 99ba0f8..fc4f177 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +In Development +-------------- + +* Internationalized domains with a "fake A-label", a non-canonical Punycode label such as `xn---bbk` that decodes to the same Unicode label as `xn--bbk`, are now rejected. With idna versions before 3.19, `me@xn---bbk.com` was accepted with the same normalized address as `me@xn--bbk.com` but with `ascii_email` holding the different domain `xn---bbk.com`. + 2.4.0 (November 14, 2026) ------------------------- diff --git a/email_validator/syntax.py b/email_validator/syntax.py index 6e3893f..b3de2b7 100644 --- a/email_validator/syntax.py +++ b/email_validator/syntax.py @@ -719,12 +719,20 @@ def validate_email_domain_name(domain: str, test_environment: bool = False, glob except idna.IDNAError as e: raise EmailSyntaxError(f"The part after the @-sign is not valid IDNA ({e}).") from e - # Check that it can be encoded back to IDNA ASCII. We have no test - # case for this. + # Check that it can be encoded back to IDNA ASCII, and that it encodes + # back to the same IDNA ASCII domain we started with. A "fake A-label" + # (RFC 5890 2.3.2.1) like xn---bbk is a non-canonical Punycode spelling + # of xn--bbk that decodes to the same Unicode label, so without this + # check two different domains could give the same normalized domain and + # ascii_domain would not be the IDNA ASCII form of domain (RFC 5891 5.4). + # idna 3.19 and later reject these in idna.decode but earlier versions + # do not. We have no test case for the exception. try: - idna.encode(domain_i18n) + canonical_ascii_domain = idna.encode(domain_i18n).decode("ascii") except idna.IDNAError as e: raise EmailSyntaxError(f"The part after the @-sign became invalid after normalizing to international characters ({e}).") from e + if canonical_ascii_domain != ascii_domain: + raise EmailSyntaxError(f"The part after the @-sign is not valid IDNA (Its canonical IDNA ASCII form is {canonical_ascii_domain}).") # Return the IDNA ASCII-encoded form of the domain, which is how it # would be transmitted on the wire (except when used with SMTPUTF8 diff --git a/tests/test_syntax.py b/tests/test_syntax.py index 257b246..c123582 100644 --- a/tests/test_syntax.py +++ b/tests/test_syntax.py @@ -579,6 +579,17 @@ def test_email_long_idna_domain_is_not_quadratic() -> None: assert time.perf_counter() - start < 2 +def test_email_fake_a_label() -> None: + # xn---bbk is a non-canonical Punycode spelling of xn--bbk (U+307E + # HIRAGANA LETTER MA). Before idna 3.19, idna.decode accepted it, so + # me@xn---bbk.com was valid with the same normalized address as + # me@xn--bbk.com but with ascii_email me@xn---bbk.com, a different domain. + assert validate_email("me@xn--bbk.com", check_deliverability=False).ascii_email == "me@xn--bbk.com" + for email in ("me@xn---bbk.com", "me@xn---4dbrk0ce.com"): + with pytest.raises(EmailSyntaxError, match="The part after the @-sign is not valid IDNA"): + validate_email(email, check_deliverability=False) + + @pytest.mark.parametrize( 'email_input,error_msg', [