From 0989e03aa84808e85352b5fa578b6cf686f39270 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Sat, 24 Feb 2024 21:58:34 +0200 Subject: [PATCH] trim quotes in the validation functions (#206) --- pkg/validate/validate.go | 4 +++- pkg/validate/validate_test.go | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/validate/validate.go b/pkg/validate/validate.go index 039b9dc..bcad4f4 100644 --- a/pkg/validate/validate.go +++ b/pkg/validate/validate.go @@ -226,6 +226,7 @@ func NetworkName(name string) error { // NameServer validates a given server name func NameServer(name string) error { + name = strings.Trim(name, "\"") exp := "((^\\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\\s*$)|(^\\s*((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$))" r := regexp.MustCompile(exp) if !r.MatchString(name) { @@ -244,6 +245,7 @@ func PrefixLengthV4(prefix int64) error { // Prefix validates a given prefix func Prefix(pr string) error { + pr = strings.Trim(pr, "\"") exp := "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\/(3[0-2]|2[0-9]|1[0-9]|[0-9]))$|^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/((1(1[0-9]|2[0-8]))|([0-9][0-9])|([0-9])))?$" r := regexp.MustCompile(exp) if !r.MatchString(pr) { @@ -273,4 +275,4 @@ func NetworkID(id string) error { } return nil -} \ No newline at end of file +} diff --git a/pkg/validate/validate_test.go b/pkg/validate/validate_test.go index d358a1b..fd1df13 100644 --- a/pkg/validate/validate_test.go +++ b/pkg/validate/validate_test.go @@ -337,6 +337,7 @@ func TestNameServer(t *testing.T) { {"not ok [2]", args{"GHT:::1200:::0000"}, true}, {"ok [1]", args{"192.168.1.1"}, false}, {"ok [2]", args{"2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, false}, + {"ok [2]", args{"\"1.1.1.1\""}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -382,6 +383,7 @@ func TestPrefix(t *testing.T) { {"not ok", args{"999.999.999.999/32"}, true}, {"ok [1]", args{"192.168.1.1/24"}, false}, {"ok [2]", args{"fe80::1ff:fe23:4567:890a/64"}, false}, + {"ok [1]", args{"\"192.168.1.1/24\""}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {