github.com/prebid/prebid-server/v2@v2.18.0/config/requestvalidation.go (about) 1 package config 2 3 import ( 4 "errors" 5 "fmt" 6 "net" 7 "strings" 8 ) 9 10 // RequestValidation specifies the request validation options. 11 type RequestValidation struct { 12 IPv4PrivateNetworks []string `mapstructure:"ipv4_private_networks,flow"` 13 IPv4PrivateNetworksParsed []net.IPNet 14 15 IPv6PrivateNetworks []string `mapstructure:"ipv6_private_networks,flow"` 16 IPv6PrivateNetworksParsed []net.IPNet 17 } 18 19 // Parse converts the CIDR representation of the IPv4 and IPv6 private networks as net.IPNet structs, or returns an error if at least one is invalid. 20 func (r *RequestValidation) Parse() error { 21 ipv4Nets, err := parseNetworks(r.IPv4PrivateNetworks, net.IPv4len) 22 if err != nil { 23 return errors.New("Invalid private IPv4 network: " + err.Error()) 24 } 25 26 ipv6Nets, err := parseNetworks(r.IPv6PrivateNetworks, net.IPv6len) 27 if err != nil { 28 return errors.New("Invalid private IPv6 network: " + err.Error()) 29 } 30 31 r.IPv4PrivateNetworksParsed = ipv4Nets 32 r.IPv6PrivateNetworksParsed = ipv6Nets 33 return nil 34 } 35 36 func parseNetworks(networks []string, networksLen int) ([]net.IPNet, error) { 37 ipNetworks := make([]net.IPNet, 0, len(networks)) 38 errMsg := strings.Builder{} 39 40 for _, v := range networks { 41 v := strings.TrimSpace(v) 42 43 if _, ipNet, err := net.ParseCIDR(v); err != nil || len(ipNet.IP) != networksLen { 44 fmt.Fprintf(&errMsg, "'%s',", v) 45 } else { 46 ipNetworks = append(ipNetworks, *ipNet) 47 } 48 } 49 50 if errMsg.Len() > 0 { 51 return nil, errors.New(errMsg.String()[:errMsg.Len()-1]) 52 } 53 54 return ipNetworks, nil 55 }