From b38eefa68af3283ff269c1ce35aa444b217c6257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E6=98=B1=E6=88=90?= <300126469+yuchengshen931122-creator@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:45:27 +0800 Subject: [PATCH] Reject IDN labels that start or end with a hyphen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit domain() rejects "-bucher.de" and "bucher-.de", but accepted the same names once the label holds a non-ASCII character: domain("-bücher.de") # True domain("bücher-.de") # True The hyphen rule is applied to the encoded name, where such a label has become "xn---bcher-4ya" and starts with "x". RFC 5891 (4.2.3.1) does not allow a label to start or end with a hyphen, so the labels are now checked before they are encoded. Verdicts change only for labels that contain a non-ASCII character and have a hyphen at either end (checked against master on 60,000 generated names); hostname(), email() and url() follow through domain(). Co-Authored-By: Claude Fable 5.2 --- src/validators/domain.py | 5 +++++ tests/test_domain.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/validators/domain.py b/src/validators/domain.py index ee6db4e7..b9340839 100644 --- a/src/validators/domain.py +++ b/src/validators/domain.py @@ -98,6 +98,11 @@ def domain( if _relies_on_idna_mapping(value): return False + if any(label.startswith("-") or label.endswith("-") for label in value.split(".")): + # ref: RFC 5891 (4.2.3.1), a label does not start or end with a hyphen. Once + # encoded, such an IDN label hides its hyphen behind the "xn--" prefix. + return False + if value.rstrip(".").rsplit(".", 1)[-1].isdecimal(): # ref: RFC 1123 (2.1) and RFC 3696 (2), a TLD is not all-numeric. An # all-digit IDN label like "\u0661" is, but encodes to a letter-final A-label. diff --git a/tests/test_domain.py b/tests/test_domain.py index 533dde97..a853e8e7 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -237,3 +237,32 @@ def test_returns_failed_validation_on_all_numeric_idn_tld_with_trailing_dot(): def test_returns_true_on_digits_outside_or_mixed_in_tld(value: str): """Test returns true when digits appear only in other labels or alongside letters.""" assert domain(value) + + +@pytest.mark.parametrize( + "value", + [ + "-bücher.de", + "bücher-.de", + "www.-bücher.de", + "www.bücher-.de", + "-пример.рф", + "example.рф-", + "-例子.中国", + ], +) +@pytest.mark.parametrize("consider_tld", [False, True]) +def test_returns_failed_validation_on_idn_label_with_edge_hyphen(value: str, consider_tld: bool): + """Test returns failed validation on an IDN label that starts or ends with a hyphen.""" + assert isinstance(domain(value, consider_tld=consider_tld), ValidationError) + dotted = domain(value + ".", consider_tld=consider_tld, rfc_1034=True) + assert isinstance(dotted, ValidationError) + + +@pytest.mark.parametrize( + "value", + ["bü-cher.de", "b-ü-cher.de", "xn--bcher-kva.de", "a-b.bücher.de", "при-мер.рф"], +) +def test_returns_true_on_idn_label_with_inner_hyphen(value: str): + """Test returns true on an IDN label with a hyphen that is not at either end.""" + assert domain(value)