Skip to content

fix: reject hostnames exceeding 253 characters#445

Open
Jo2234 wants to merge 1 commit intopython-validators:masterfrom
Jo2234:fix/hostname-length-validation
Open

fix: reject hostnames exceeding 253 characters#445
Jo2234 wants to merge 1 commit intopython-validators:masterfrom
Jo2234:fix/hostname-length-validation

Conversation

@Jo2234
Copy link

@Jo2234 Jo2234 commented Mar 1, 2026

Summary

Add RFC 1123 conformant total hostname length validation. Hostnames exceeding 253 characters are now correctly rejected.

Problem

>>> import validators
>>> long = 'a' * 50 + '.' + 'b' * 50 + '.' + 'c' * 50 + '.' + 'd' * 50 + '.' + 'e' * 50 + '.com'
>>> len(long)
256
>>> validators.hostname(long)
True  # Should be False — exceeds 253 chars

The existing hostname validator checked individual label length (≤63 chars via regex) but never validated the total hostname length. Per RFC 1123, a hostname must not exceed 253 characters.

Fix

Added an early length check in hostname() that:

  1. Strips port (if present) to get the host part
  2. Strips IPv6 brackets
  3. Strips optional trailing dot (per RFC 1034)
  4. Rejects if the result exceeds 253 characters

This runs before the existing label/domain/IP validation.

Fixes #413

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 python-validators#413
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validators.hostname lets long hostnames through if they contain periods

1 participant