Skip to content

Commit 4fe1c90

Browse files
committed
Reject domains longer than 253 characters
domain() validated each label (<= 63 chars) but never checked the total length, so a long multi-label name (e.g. 256 chars of valid labels) was accepted, and hostname() inherited the same gap. Enforce the RFC 1035 253-character limit on the textual domain name. Fixes #413
1 parent 70de324 commit 4fe1c90

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/validators/domain.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def domain(
7676
if not value:
7777
return False
7878

79+
# The textual representation of a domain name is limited to 253 characters
80+
# (RFC 1035), regardless of the individual label lengths.
81+
if len(value.rstrip(".")) > 253:
82+
return False
83+
7984
if consider_tld and not _IanaTLD.check(value.rstrip(".").rsplit(".", 1)[-1].upper()):
8085
return False
8186

tests/test_domain.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ def test_returns_failed_validation_on_invalid_domain(value: str, rfc_1034: bool,
8484
assert isinstance(domain(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782), ValidationError)
8585

8686

87+
def test_returns_failed_validation_on_too_long_domain():
88+
"""A domain over 253 characters is invalid regardless of label lengths."""
89+
label = "a" * 49
90+
too_long = ".".join([label] * 5) + ".aaaaa" # 255 characters, each label <= 63
91+
assert len(too_long) > 253
92+
assert isinstance(domain(too_long), ValidationError)
93+
94+
at_limit = ".".join([label] * 5) + ".aaa" # exactly 253 characters
95+
assert len(at_limit) == 253
96+
assert domain(at_limit)
97+
98+
8799
@pytest.mark.parametrize(
88100
("value", "consider_tld", "rfc_1034", "rfc_2782"),
89101
[

0 commit comments

Comments
 (0)