forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
49 lines (44 loc) · 1.3 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package catalog
import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
)
var (
controlChars = regexp.MustCompile("[[:cntrl:]]")
controlEncoded = regexp.MustCompile("%[0-1][0-9,a-f,A-F]")
)
func Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
url, ok := data["url"].(string)
if ok && len(url) > 0 {
sanitizedURL, err := validateURL(url)
if err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent, fmt.Sprintf("%v", err))
}
data["url"] = sanitizedURL
return nil
} else if request.Method == http.MethodPost {
return httperror.NewAPIError(httperror.MissingRequired, "Catalog URL not specified")
}
return nil
}
func validateURL(pathURL string) (string, error) {
// Remove inline control character
pathURL = controlChars.ReplaceAllString(pathURL, "")
// Remove control characters that have been urlencoded such as %0d, %1B
pathURL = controlEncoded.ReplaceAllString(pathURL, "")
// Validate scheme
parsedURL, err := url.Parse(pathURL)
if err != nil {
return "", err
}
if !strings.HasPrefix(parsedURL.Scheme, "http") {
return "", errors.Errorf("unsupported protocol scheme '%s'", parsedURL.Scheme)
}
return parsedURL.String(), nil
}