Skip to content

Commit e8c3e12

Browse files
committed
fix hostname accepting values longer than 253 chars when they contain dots
Fixes #413 The hostname validator was only checking individual label lengths via regex but had no overall length limit. Hostnames with dots that had each label under 63 chars but total length over 253 would slip through. Added a 253 character check on the host segment both with and without a port, per RFC 1123. Added a test case for this.
1 parent 9bc7e82 commit e8c3e12

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/validators/hostname.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,18 @@ def hostname(
114114
return False
115115

116116
if may_have_port and (host_seg := _port_validator(value)):
117+
if len(host_seg) > 253:
118+
return False
117119
return (
118120
(_simple_hostname_regex().match(host_seg) if maybe_simple else False)
119121
or domain(host_seg, consider_tld=consider_tld, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
120122
or (False if skip_ipv4_addr else ipv4(host_seg, cidr=False, private=private))
121123
or (False if skip_ipv6_addr else ipv6(host_seg, cidr=False))
122124
)
123125

126+
if len(value) > 253:
127+
return False
128+
124129
return (
125130
(_simple_hostname_regex().match(value) if maybe_simple else False)
126131
or domain(value, consider_tld=consider_tld, rfc_1034=rfc_1034, rfc_2782=rfc_2782)

tests/test_hostname.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ def test_returns_true_on_valid_hostname(value: str, rfc_1034: bool, rfc_2782: bo
4040
@pytest.mark.parametrize(
4141
("value", "rfc_1034", "rfc_2782"),
4242
[
43+
# bad (hostname exceeding 253 chars)
44+
(
45+
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc."
46+
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc."
47+
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc."
48+
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc."
49+
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXcab",
50+
False,
51+
False,
52+
),
4353
# bad (simple hostname w/ optional ports)
4454
("ubuntu-pc:443080", False, False),
4555
("this-pc-is-sh*t", False, False),

0 commit comments

Comments
 (0)