MCPcopy Index your code
hub / github.com/cloudflare/cloudflared / validateUrlString

Function validateUrlString

validation/validation.go:80–147  ·  view source on GitHub ↗
(originUrl string)

Source from the content-addressed store, hash-verified

78}
79
80func validateUrlString(originUrl string) (string, error) {
81 if originUrl == "" {
82 return "", fmt.Errorf("URL should not be empty")
83 }
84
85 if net.ParseIP(originUrl) != nil {
86 return validateIP("", originUrl, "")
87 } else if strings.HasPrefix(originUrl, "[") && strings.HasSuffix(originUrl, "]") {
88 // ParseIP doesn't recoginze [::1]
89 return validateIP("", originUrl[1:len(originUrl)-1], "")
90 }
91
92 host, port, err := net.SplitHostPort(originUrl)
93 // user might pass in an ip address like 127.0.0.1
94 if err == nil && net.ParseIP(host) != nil {
95 return validateIP("", host, port)
96 }
97
98 unescapedUrl, err := url.PathUnescape(originUrl)
99 if err != nil {
100 return "", fmt.Errorf("URL %s has invalid escape characters %s", originUrl, unescapedUrl)
101 }
102
103 parsedUrl, err := url.Parse(unescapedUrl)
104 if err != nil {
105 return "", fmt.Errorf("URL %s has invalid format", originUrl)
106 }
107
108 // if the url is in the form of host:port, IsAbs() will think host is the schema
109 var hostname string
110 hasScheme := parsedUrl.IsAbs() && parsedUrl.Host != ""
111 if hasScheme {
112 err := validateScheme(parsedUrl.Scheme)
113 if err != nil {
114 return "", err
115 }
116 // The earlier check for ip address will miss the case http://[::1]
117 // and http://[::1]:8080
118 if net.ParseIP(parsedUrl.Hostname()) != nil {
119 return validateIP(parsedUrl.Scheme, parsedUrl.Hostname(), parsedUrl.Port())
120 }
121 hostname, err = ValidateHostname(parsedUrl.Hostname())
122 if err != nil {
123 return "", fmt.Errorf("URL %s has invalid format", originUrl)
124 }
125 if parsedUrl.Port() != "" {
126 return fmt.Sprintf("%s://%s", parsedUrl.Scheme, net.JoinHostPort(hostname, parsedUrl.Port())), nil
127 }
128 return fmt.Sprintf("%s://%s", parsedUrl.Scheme, hostname), nil
129 } else {
130 if host == "" {
131 hostname, err = ValidateHostname(originUrl)
132 if err != nil {
133 return "", fmt.Errorf("URL no %s has invalid format", originUrl)
134 }
135 return fmt.Sprintf("%s://%s", defaultScheme, hostname), nil
136 } else {
137 hostname, err = ValidateHostname(host)

Callers 2

ValidateUrlFunction · 0.85
NewAccessValidatorFunction · 0.85

Calls 5

validateIPFunction · 0.85
validateSchemeFunction · 0.85
ValidateHostnameFunction · 0.85
ErrorfMethod · 0.80
HostnameMethod · 0.45

Tested by

no test coverage detected