-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: reject invalid dotted-decimal in hostname_rfc1123 validation #1562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2798,8 +2798,33 @@ func isHostnameRFC952(fl FieldLevel) bool { | |||||
| return hostnameRegexRFC952().MatchString(fl.Field().String()) | ||||||
| } | ||||||
|
|
||||||
| // looksLikeDottedDecimal returns true if s looks like a dotted-decimal address | ||||||
| // (e.g. "277.168.0.1") — composed entirely of digits and dots, with at least one dot. | ||||||
| func looksLikeDottedDecimal(s string) bool { | ||||||
| hasDot := false | ||||||
| for _, c := range s { | ||||||
| if c == '.' { | ||||||
| hasDot = true | ||||||
| } else if c < '0' || c > '9' { | ||||||
| return false | ||||||
| } | ||||||
| } | ||||||
| return hasDot | ||||||
| } | ||||||
|
|
||||||
| func isHostnameRFC1123(fl FieldLevel) bool { | ||||||
| return hostnameRegexRFC1123().MatchString(fl.Field().String()) | ||||||
| val := fl.Field().String() | ||||||
| if !hostnameRegexRFC1123().MatchString(val) { | ||||||
| return false | ||||||
| } | ||||||
| // RFC 1123 §2.1: "a valid host name can never have the dotted-decimal | ||||||
| // form #.#.#.#, since at least the highest-level component label will | ||||||
| // be alphabetic." Reject strings that look like dotted-decimal but are | ||||||
| // not valid IPv4 addresses (e.g. 277.168.0.1). | ||||||
| if net.ParseIP(val) == nil && looksLikeDottedDecimal(val) { | ||||||
|
||||||
| if net.ParseIP(val) == nil && looksLikeDottedDecimal(val) { | |
| if looksLikeDottedDecimal(val) && net.ParseIP(val) == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looksLikeDottedDecimalreturns true for any string containing only digits and dots with at least one dot (e.g. "1.2" or "1.2.3"). That meansisHostnameRFC1123will now reject these values even thoughhostnameRegexRFC1123allows them, and the RFC 1123 quote/comment here specifically refers to the IPv4-like form#.#.#.#. Consider tightening the check to only treat the input as “dotted-decimal” when it has exactly 4 numeric labels (3 dots, no empty labels), so you only gate IPv4-shaped inputs behindnet.ParseIP.