Skip to content

Accept trailing dot on simple hostnames with rfc_1034#462

Open
assinscreedFC wants to merge 1 commit into
python-validators:masterfrom
assinscreedFC:fix/442-tld-trailing-dot
Open

Accept trailing dot on simple hostnames with rfc_1034#462
assinscreedFC wants to merge 1 commit into
python-validators:masterfrom
assinscreedFC:fix/442-tld-trailing-dot

Conversation

@assinscreedFC
Copy link
Copy Markdown

Summary

Refs #442.

rfc_1034 is documented as "Allow trailing dot in domain/host name", but it only worked for dotted names (handled by domain()):

>>> import validators
>>> validators.hostname("ie",  rfc_1034=True)   # True
>>> validators.hostname("ie.", rfc_1034=True)   # False  <- inconsistent
>>> validators.hostname("defo.ie.", rfc_1034=True)  # True

A simple, single-label hostname goes through _simple_hostname_regex(), which does not permit a trailing dot, so "ie." failed even with rfc_1034=True, while "ie" passed.

Fix

Strip an optional trailing dot before the simple-hostname check when rfc_1034 is set, so a single-label hostname behaves the same with and without the dot. The trailing dot is still rejected when rfc_1034 is False.

>>> validators.hostname("ie.",  rfc_1034=True)   # now True
>>> validators.hostname("ie.",  rfc_1034=False)  # still False

Scope

This addresses the hostname() inconsistency from #442. I deliberately left domain()'s rejection of bare single-label names (domain("ie") → False with or without a dot) unchanged, since whether a single label counts as a "domain" is a separate policy question — happy to follow up if you'd like it changed too.

Test plan

  • Added single-label trailing-dot cases to tests/test_hostname.py (valid with rfc_1034=True, invalid without).
  • pytest tests/test_hostname.py tests/test_domain.py — 90 passed.
  • ruff check / ruff format --check — clean.
  • Note: the full suite has 17 pre-existing, unrelated failures in tests/crypto_addresses/test_eth_address.py (present on a clean checkout, before this change).

hostname('ie', rfc_1034=True) is valid, but hostname('ie.', rfc_1034=True)
was rejected. The RFC 1034 trailing dot was only handled by domain() (for
dotted names); a simple, single-label hostname went through
_simple_hostname_regex(), which does not allow a trailing dot, so it failed.

Strip an optional trailing dot before the simple-hostname check when
rfc_1034 is set, so a single-label hostname behaves consistently with and
without the dot. The dot is still rejected when rfc_1034 is False.

Refs python-validators#442
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.

1 participant