Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/validators/hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ def hostname(
if not value:
return False

# Determine the host part (strip port if present) for length validation
host_part = value
if may_have_port:
if (seg := _port_validator(value)):
host_part = seg

# Strip IPv6 brackets for length check
host_part = host_part.lstrip("[").rstrip("]")

# RFC 1123: total hostname length must not exceed 253 characters
# (excluding optional trailing dot)
if len(host_part.rstrip(".")) > 253:
return False

if may_have_port and (host_seg := _port_validator(value)):
return (
(_simple_hostname_regex().match(host_seg) if maybe_simple else False)
Expand Down