Skip to content

Commit b38eefa

Browse files
Reject IDN labels that start or end with a hyphen
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 <noreply@anthropic.com>
1 parent 22786cc commit b38eefa

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎src/validators/domain.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ def domain(
9898
if _relies_on_idna_mapping(value):
9999
return False
100100

101+
if any(label.startswith("-") or label.endswith("-") for label in value.split(".")):
102+
# ref: RFC 5891 (4.2.3.1), a label does not start or end with a hyphen. Once
103+
# encoded, such an IDN label hides its hyphen behind the "xn--" prefix.
104+
return False
105+
101106
if value.rstrip(".").rsplit(".", 1)[-1].isdecimal():
102107
# ref: RFC 1123 (2.1) and RFC 3696 (2), a TLD is not all-numeric. An
103108
# all-digit IDN label like "\u0661" is, but encodes to a letter-final A-label.

‎tests/test_domain.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,32 @@ def test_returns_failed_validation_on_all_numeric_idn_tld_with_trailing_dot():
237237
def test_returns_true_on_digits_outside_or_mixed_in_tld(value: str):
238238
"""Test returns true when digits appear only in other labels or alongside letters."""
239239
assert domain(value)
240+
241+
242+
@pytest.mark.parametrize(
243+
"value",
244+
[
245+
"-bücher.de",
246+
"bücher-.de",
247+
"www.-bücher.de",
248+
"www.bücher-.de",
249+
"-пример.рф",
250+
"example.рф-",
251+
"-例子.中国",
252+
],
253+
)
254+
@pytest.mark.parametrize("consider_tld", [False, True])
255+
def test_returns_failed_validation_on_idn_label_with_edge_hyphen(value: str, consider_tld: bool):
256+
"""Test returns failed validation on an IDN label that starts or ends with a hyphen."""
257+
assert isinstance(domain(value, consider_tld=consider_tld), ValidationError)
258+
dotted = domain(value + ".", consider_tld=consider_tld, rfc_1034=True)
259+
assert isinstance(dotted, ValidationError)
260+
261+
262+
@pytest.mark.parametrize(
263+
"value",
264+
["bü-cher.de", "b-ü-cher.de", "xn--bcher-kva.de", "a-b.bücher.de", "при-мер.рф"],
265+
)
266+
def test_returns_true_on_idn_label_with_inner_hyphen(value: str):
267+
"""Test returns true on an IDN label with a hyphen that is not at either end."""
268+
assert domain(value)

0 commit comments

Comments
 (0)