fix(domain): accept single-label TLDs erroneously rejected by domain() and hostname()#476
Open
eeshsaxena wants to merge 1 commit into
Open
Conversation
domain() and hostname() with rfc_1034=True silently rejected valid single-label domain names (ie, com, yu, ie.) because the regex used a + quantifier on the label group, requiring at least one dot. RFC 1034 s2.3.1 permits single-label names. Add a second match path for bare TLDs (2-63 alpha chars, optional trailing dot) after the existing multi-label path so that both cases are handled. Fixes python-validators#442
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #442.
domain() and hostname() with
fc_1034=True rejected valid single-label domain names (ie, com, yu, ie.) because the internal regex used a + quantifier on the label group — which forces at least one dot separator. So a bare TLD like ie never matched.
Multi-label names like defo.ie and yugoslavia.yu were fine; only the single-label case was broken.
RFC 1034 §2.3.1 explicitly permits single-label names. The fix adds a second match path after the existing multi-label path: a bare TLD must be 2-63 alpha characters (optionally followed by a trailing dot when
fc_1034=True). Pure-digit strings like 123 are still rejected, consistent with the multi-label path.
Before:
\\python
domain('ie') # ValidationError (wrong)
domain('ie.') # ValidationError (wrong)
domain('com') # ValidationError (wrong)
hostname('ie.', rfc_1034=True) # ValidationError (wrong)
\\
After:
\\python
domain('ie') # True
domain('ie.', rfc_1034=True) # True
domain('ie.') # ValidationError (correct — trailing dot only allowed with rfc_1034)
domain('com') # True
hostname('ie.', rfc_1034=True) # True
domain('123') # ValidationError (still rejected)
\\
Multi-label domains, IDN, and all existing behaviour unchanged.