forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
39 lines (34 loc) · 1.04 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
package catalog
import (
"net/http"
"net/url"
"regexp"
"strings"
"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 {
if pathURL, _ := data["url"].(string); pathURL != "" {
if err := validateURL(pathURL); err != nil {
return err
}
if u, err := url.Parse(pathURL); err == nil {
u.Scheme = strings.ToLower(u.Scheme) // git commands are case-sensitive
data["url"] = u.String()
}
return nil
} else if request.Method == http.MethodPost {
return httperror.NewAPIError(httperror.MissingRequired, "Catalog URL not specified")
}
return nil
}
func validateURL(pathURL string) error {
if controlChars.FindStringIndex(pathURL) != nil || controlEncoded.FindStringIndex(pathURL) != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent, "Invalid characters in catalog URL")
}
return nil
}