Skip to content

Let hostname() accept a bare single-label name with the RFC 1034 dot - #479

Open
afonsojanu wants to merge 1 commit into
python-validators:masterfrom
afonsojanu:fix/hostname-rfc1034-single-label-trailing-dot
Open

Let hostname() accept a bare single-label name with the RFC 1034 dot#479
afonsojanu wants to merge 1 commit into
python-validators:masterfrom
afonsojanu:fix/hostname-rfc1034-single-label-trailing-dot

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #442, at least the hostname() half of it.

hostname("yu", rfc_1034=True) already passes today, through the maybe_simple fast path, since _simple_hostname_regex doesn't care whether a lone alphanumeric label has a dot after it or not. hostname("yu.", rfc_1034=True) doesn't pass, though, because that regex has no notion of a trailing dot at all, and falling back to domain() needs at least a second label before the TLD. So the exact same name gets accepted or rejected depending only on whether it carries the dot that rfc_1034 is supposed to permit, which is the wrong way around.

Fix: strip a lone trailing dot before running the simple-hostname match when rfc_1034 is set, the same thing domain() already does for its own regex a bit further down. Everything else about the simple path is untouched, so this shouldn't move the needle for the vast majority of callers who never pass rfc_1034.

I looked at domain()'s own half of the issue too (a bare TLD with no second label at all, e.g. "com" or "com."), but that's a bigger question about whether a single label should count as a "domain" on its own, separate from the hostname() inconsistency this PR closes. Left that alone rather than guess at the intended scope.

Added two new cases to the existing parametrized true/false tests in test_hostname.py rather than a separate test function, since they slot into the same shape already there. Confirmed via git stash that both new true-cases fail on unpatched code with the same ValidationError from the issue, and pass with the fix. Full local suite: 882 passing (17 unrelated failures from crypto_addresses/test_eth_address.py needing an optional extra that isn't installed here, same before and after this change).

hostname("yu", rfc_1034=True) passes today through the maybe_simple
fast path, since _simple_hostname_regex has no problem with a lone
alphanumeric label. hostname("yu.", rfc_1034=True) doesn't, because
that regex has no notion of a trailing dot at all, and the fallback to
domain() requires at least a second label before the TLD. So the exact
same name is accepted or rejected purely based on whether it carries
the dot RFC 1034 is supposed to permit, which is backwards from what
that flag promises.

Added a small wrapper around the simple-hostname match that strips a
lone trailing dot first when rfc_1034 is set, mirroring what domain()
already does for its own regex. Left everything else about the simple
path untouched, so this doesn't change hostname() for the vast
majority of callers who never pass rfc_1034.

Fixes python-validatorsGH-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.

Top-level domain names are rejected erroneously by both hostname() and domain()

1 participant