37 Star 403 Fork 75

GVPrancher/rancher

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
validator.go 1.04 KB
Copy Edit Raw Blame History
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.13

Search