From ee37674003944d5a8b01fd725f801c019d225423 Mon Sep 17 00:00:00 2001 From: Giuliano Scaglioni Date: Wed, 20 May 2020 21:08:58 -0300 Subject: [PATCH 1/3] add breaking test --- validator_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/validator_test.go b/validator_test.go index 2e4215f..deea780 100644 --- a/validator_test.go +++ b/validator_test.go @@ -8033,6 +8033,8 @@ func TestFQDNValidation(t *testing.T) { {"example24.com.", true}, {"test.example24.com.", true}, {"test24.example24.com.", true}, + {"24.example24.com", true}, + {"test.24.example.com", true}, {"test24.example24.com..", false}, {"example", false}, {"192.168.0.1", false}, From 7d6236a89848d52f496b8226473d372a79d2ef17 Mon Sep 17 00:00:00 2001 From: Giuliano Scaglioni Date: Wed, 20 May 2020 21:15:27 -0300 Subject: [PATCH 2/3] add updated regex for fqdn --- baked_in.go | 7 +------ regexes.go | 2 ++ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/baked_in.go b/baked_in.go index 4124c62..43e4e10 100644 --- a/baked_in.go +++ b/baked_in.go @@ -1990,12 +1990,7 @@ func isFQDN(fl FieldLevel) bool { return false } - if val[len(val)-1] == '.' { - val = val[0 : len(val)-1] - } - - return strings.ContainsAny(val, ".") && - hostnameRegexRFC952.MatchString(val) + return fqdnRegexRFC1123.MatchString(val) } // IsDir is the validation function for validating if the current field's value is a valid directory. diff --git a/regexes.go b/regexes.go index b8bf253..8ad81c1 100644 --- a/regexes.go +++ b/regexes.go @@ -38,6 +38,7 @@ const ( sSNRegexString = `^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$` hostnameRegexStringRFC952 = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952 hostnameRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123 + fqdnRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62})(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?(\.[a-zA-Z]{1}[a-zA-Z0-9]{0,62})\.?$` // same as hostnameRegexStringRFC1123 but must contain a non numerical TLD (possibly ending with '.') btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32 btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32 @@ -86,6 +87,7 @@ var ( sSNRegex = regexp.MustCompile(sSNRegexString) hostnameRegexRFC952 = regexp.MustCompile(hostnameRegexStringRFC952) hostnameRegexRFC1123 = regexp.MustCompile(hostnameRegexStringRFC1123) + fqdnRegexRFC1123 = regexp.MustCompile(fqdnRegexStringRFC1123) btcAddressRegex = regexp.MustCompile(btcAddressRegexString) btcUpperAddressRegexBech32 = regexp.MustCompile(btcAddressUpperRegexStringBech32) btcLowerAddressRegexBech32 = regexp.MustCompile(btcAddressLowerRegexStringBech32) From b610155475d41f86fcd6fc7df4ae96c876c9f4a6 Mon Sep 17 00:00:00 2001 From: Giuliano Scaglioni Date: Wed, 20 May 2020 21:16:17 -0300 Subject: [PATCH 3/3] format code --- regexes.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/regexes.go b/regexes.go index 8ad81c1..94a800c 100644 --- a/regexes.go +++ b/regexes.go @@ -36,12 +36,12 @@ const ( latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$" longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$" sSNRegexString = `^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$` - hostnameRegexStringRFC952 = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952 - hostnameRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123 + hostnameRegexStringRFC952 = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952 + hostnameRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123 fqdnRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62})(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?(\.[a-zA-Z]{1}[a-zA-Z0-9]{0,62})\.?$` // same as hostnameRegexStringRFC1123 but must contain a non numerical TLD (possibly ending with '.') - btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address - btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32 - btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32 + btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address + btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32 + btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32 ethAddressRegexString = `^0x[0-9a-fA-F]{40}$` ethAddressUpperRegexString = `^0x[0-9A-F]{40}$` ethAddressLowerRegexString = `^0x[0-9a-f]{40}$`