fix: reject invalid dotted-decimal in hostname_rfc1123 validation - #1607
Open
deepakganesh78 wants to merge 1 commit into
Open
fix: reject invalid dotted-decimal in hostname_rfc1123 validation#1607deepakganesh78 wants to merge 1 commit into
deepakganesh78 wants to merge 1 commit into
Conversation
Per RFC 1123 §2.1, a valid hostname can never have the dotted-decimal form #.#.#.# since the highest-level component label must be alphabetic. The hostname_rfc1123 validator accepted strings like '277.168.0.1' because the regex allows all-digit labels. Now, when the input consists entirely of digits and dots (dotted-decimal form), it is validated as an IPv4 address via net.ParseIP and rejected if invalid. Fixes go-playground#1561 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
deepakganesh78
force-pushed
the
fix/issue1561-hostname-ipv4-octet
branch
from
August 1, 2026 16:54
9ace67d to
859cb5a
Compare
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 #1561
Problem
hostname_rfc1123accepted strings like277.168.0.1as valid hostnames. Per RFC 1123 §2.1, a valid hostname can never have the dotted-decimal form#.#.#.#since the highest-level component label must be alphabetic.Root Cause
The
hostnameRegexRFC1123regex allows all-digit labels, so a four-part all-numeric string like277.168.0.1passes the regex even though it is neither a valid IPv4 address nor a legitimate hostname.Fix
After the regex matches, a new
looksLikeIPv4helper checks whether the string has exactly four dot-separated all-numeric parts (the dotted-decimal form). Only in that case isnet.ParseIPcalled; invalid addresses are rejected. The guard is deliberately narrow:192.168.0.10.0.0.0255.255.255.255277.168.0.1999.999.999.9991.2.3.2563com.com1and1.com7-eleven.com1234123451.2.31.2.3.4.5This avoids calling
net.ParseIPfor the vast majority of inputs and does not affectfqdn,hostname_port, or other validators (fqdnalready requires an alphabetic TLD;hostname_portcallshostnameRegexRFC1123on the host part only after stripping the port).Validation
go test ./...— all 24 packages passgo vet ./...— cleangofmt -l— clean on touched filesCompatibility
No breaking change. Valid IPv4 addresses continue to be accepted. Only invalid four-part dotted-decimal strings that were incorrectly accepted are now rejected.