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/hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,18 @@ def hostname(
return False

if may_have_port and (host_seg := _port_validator(value)):
if len(host_seg) > 253:
return False
return (
(_simple_hostname_regex().match(host_seg) if maybe_simple else False)
or domain(host_seg, consider_tld=consider_tld, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
or (False if skip_ipv4_addr else ipv4(host_seg, cidr=False, private=private))
or (False if skip_ipv6_addr else ipv6(host_seg, cidr=False))
)

if len(value) > 253:
return False

return (
(_simple_hostname_regex().match(value) if maybe_simple else False)
or domain(value, consider_tld=consider_tld, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def test_returns_true_on_valid_hostname(value: str, rfc_1034: bool, rfc_2782: bo
@pytest.mark.parametrize(
("value", "rfc_1034", "rfc_2782"),
[
# bad (hostname exceeding 253 chars)
(
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc."
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc."
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc."
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc."
"kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXcab",
False,
False,
),
# bad (simple hostname w/ optional ports)
("ubuntu-pc:443080", False, False),
("this-pc-is-sh*t", False, False),
Expand Down