Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/validators/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def domain(
if not value:
return False

# The textual representation of a domain name is limited to 253 characters
# (RFC 1035), regardless of the individual label lengths.
if len(value.rstrip(".")) > 253:
return False

if consider_tld and not _IanaTLD.check(value.rstrip(".").rsplit(".", 1)[-1].upper()):
return False

Expand Down
12 changes: 12 additions & 0 deletions tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ def test_returns_failed_validation_on_invalid_domain(value: str, rfc_1034: bool,
assert isinstance(domain(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782), ValidationError)


def test_returns_failed_validation_on_too_long_domain():
"""A domain over 253 characters is invalid regardless of label lengths."""
label = "a" * 49
too_long = ".".join([label] * 5) + ".aaaaa" # 255 characters, each label <= 63
assert len(too_long) > 253
assert isinstance(domain(too_long), ValidationError)

at_limit = ".".join([label] * 5) + ".aaa" # exactly 253 characters
assert len(at_limit) == 253
assert domain(at_limit)


@pytest.mark.parametrize(
("value", "consider_tld", "rfc_1034", "rfc_2782"),
[
Expand Down