Skip to content

Commit eb4da0b

Browse files
Jo2234Jo2234
authored andcommitted
fix: reject hostnames exceeding 253 characters
Per RFC 1123, a hostname must not exceed 253 characters. The existing validator only checked individual label length (≤63 chars via regex) but never validated the total hostname length. This allowed hostnames with multiple short labels separated by dots to pass validation even when the total length exceeded 253 characters. The fix adds an early length check after stripping port and IPv6 brackets, before proceeding with label/domain/IP validation. Fixes #413
1 parent 9bc7e82 commit eb4da0b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/validators/hostname.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ def hostname(
113113
if not value:
114114
return False
115115

116+
# Determine the host part (strip port if present) for length validation
117+
host_part = value
118+
if may_have_port:
119+
if (seg := _port_validator(value)):
120+
host_part = seg
121+
122+
# Strip IPv6 brackets for length check
123+
host_part = host_part.lstrip("[").rstrip("]")
124+
125+
# RFC 1123: total hostname length must not exceed 253 characters
126+
# (excluding optional trailing dot)
127+
if len(host_part.rstrip(".")) > 253:
128+
return False
129+
116130
if may_have_port and (host_seg := _port_validator(value)):
117131
return (
118132
(_simple_hostname_regex().match(host_seg) if maybe_simple else False)

0 commit comments

Comments
 (0)