Validate domain hostname rules per label - #320
Open
gaoflow wants to merge 1 commit into
Open
Conversation
valid_domain? enforced hostname rules positionally across the whole
domain string (start_with?('-'), include?('-.'), and no length check)
rather than per label, so the mirror positions leaked through:
- a leading hyphen on any non-first label (foo@sub.-example.com)
- a trailing hyphen on the final label (foo@example.com-)
- a label longer than 63 octets
- a domain longer than 253 octets
all validated as true. The existing spec already asserts that a domain
may not begin or end with a dash; these were asymmetric gaps in that
same rule.
Split the domain on '.' and validate each label (1-63 octets, no
leading or trailing hyphen) plus the overall 253-octet limit. Internal
hyphens, double hyphens and punycode (xn--) labels stay valid.
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.
valid_domain?checks the hostname rules positionally against the whole domain string instead of per label. It rejects a leading hyphen only viastart_with?('-')(first label only) and a trailing hyphen only viainclude?('-.')(a hyphen followed by a dot, i.e. non-final labels only), and it has no length check at all. So the mirror positions leak through:The spec already asserts that a domain may not begin or end with a dash (
"is invalid if the domain begins with a dash"/"...ends with a dash"). These are the same rule applied to every label rather than only the first and last position of the domain string, plus the RFC 1035 length limits (63 per label, 253 total). Python'semail_validatorrejects all four shapes.The fix splits the domain on
.and validates each label (1–63 octets, no leading or trailing hyphen) plus the overall 253-octet limit. Internal hyphens, double hyphens (a--b) and punycode labels (xn--bcher-kva) stay valid — only the boundary positions of each label are checked, so no legitimate hostname is affected.This is scoped to label structure and length; it does not touch
PROHIBITED_DOMAIN_CHARACTERS_REGEX(the separate character-set surface). It uses the existing string/regex approach rather than a full IDNA library — labels are counted in characters, which matches how the rest ofvalid_domain?already works.Tests: added a parametrized table of the malformed shapes (invalid) alongside the internal-hyphen / double-hyphen / punycode / 63-octet / 253-octet cases that must stay valid. Full suite green (
bundle exec rake).