2017-04-19 08:32:20 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-09-04 16:17:56 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
"gitea.com/go-chi/binding"
|
2019-09-09 11:18:21 +05:30
|
|
|
"github.com/gobwas/glob"
|
2017-04-19 08:32:20 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ErrGitRefName is git reference name error
|
|
|
|
ErrGitRefName = "GitRefNameError"
|
2019-09-09 11:18:21 +05:30
|
|
|
|
|
|
|
// ErrGlobPattern is returned when glob pattern is invalid
|
|
|
|
ErrGlobPattern = "GlobPattern"
|
2021-06-25 19:58:55 +05:30
|
|
|
|
|
|
|
// ErrRegexPattern is returned when a regex pattern is invalid
|
|
|
|
ErrRegexPattern = "RegexPattern"
|
2022-11-04 14:34:08 +05:30
|
|
|
|
|
|
|
// ErrUsername is username error
|
|
|
|
ErrUsername = "UsernameError"
|
2017-04-19 08:32:20 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// AddBindingRules adds additional binding rules
|
|
|
|
func AddBindingRules() {
|
|
|
|
addGitRefNameBindingRule()
|
|
|
|
addValidURLBindingRule()
|
2021-06-26 04:08:27 +05:30
|
|
|
addValidSiteURLBindingRule()
|
2019-09-09 11:18:21 +05:30
|
|
|
addGlobPatternRule()
|
2021-06-25 19:58:55 +05:30
|
|
|
addRegexPatternRule()
|
|
|
|
addGlobOrRegexPatternRule()
|
2022-11-04 14:34:08 +05:30
|
|
|
addUsernamePatternRule()
|
2017-04-19 08:32:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func addGitRefNameBindingRule() {
|
|
|
|
// Git refname validation rule
|
|
|
|
binding.AddRule(&binding.Rule{
|
|
|
|
IsMatch: func(rule string) bool {
|
|
|
|
return strings.HasPrefix(rule, "GitRefName")
|
|
|
|
},
|
|
|
|
IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
|
|
|
|
str := fmt.Sprintf("%v", val)
|
|
|
|
|
2022-09-04 16:17:56 +05:30
|
|
|
if !git.IsValidRefPattern(str) {
|
2017-04-19 08:32:20 +05:30
|
|
|
errs.Add([]string{name}, ErrGitRefName, "GitRefName")
|
|
|
|
return false, errs
|
|
|
|
}
|
|
|
|
return true, errs
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func addValidURLBindingRule() {
|
|
|
|
// URL validation rule
|
|
|
|
binding.AddRule(&binding.Rule{
|
|
|
|
IsMatch: func(rule string) bool {
|
|
|
|
return strings.HasPrefix(rule, "ValidUrl")
|
|
|
|
},
|
|
|
|
IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
|
|
|
|
str := fmt.Sprintf("%v", val)
|
2018-08-15 11:59:37 +05:30
|
|
|
if len(str) != 0 && !IsValidURL(str) {
|
|
|
|
errs.Add([]string{name}, binding.ERR_URL, "Url")
|
|
|
|
return false, errs
|
2017-04-19 08:32:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return true, errs
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-26 04:08:27 +05:30
|
|
|
func addValidSiteURLBindingRule() {
|
|
|
|
// URL validation rule
|
|
|
|
binding.AddRule(&binding.Rule{
|
|
|
|
IsMatch: func(rule string) bool {
|
|
|
|
return strings.HasPrefix(rule, "ValidSiteUrl")
|
|
|
|
},
|
|
|
|
IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
|
|
|
|
str := fmt.Sprintf("%v", val)
|
|
|
|
if len(str) != 0 && !IsValidSiteURL(str) {
|
|
|
|
errs.Add([]string{name}, binding.ERR_URL, "Url")
|
|
|
|
return false, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, errs
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-09 11:18:21 +05:30
|
|
|
func addGlobPatternRule() {
|
|
|
|
binding.AddRule(&binding.Rule{
|
|
|
|
IsMatch: func(rule string) bool {
|
|
|
|
return rule == "GlobPattern"
|
|
|
|
},
|
2021-06-25 19:58:55 +05:30
|
|
|
IsValid: globPatternValidator,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func globPatternValidator(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
|
|
|
|
str := fmt.Sprintf("%v", val)
|
|
|
|
|
|
|
|
if len(str) != 0 {
|
|
|
|
if _, err := glob.Compile(str); err != nil {
|
|
|
|
errs.Add([]string{name}, ErrGlobPattern, err.Error())
|
|
|
|
return false, errs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func addRegexPatternRule() {
|
|
|
|
binding.AddRule(&binding.Rule{
|
|
|
|
IsMatch: func(rule string) bool {
|
|
|
|
return rule == "RegexPattern"
|
|
|
|
},
|
|
|
|
IsValid: regexPatternValidator,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func regexPatternValidator(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
|
|
|
|
str := fmt.Sprintf("%v", val)
|
|
|
|
|
|
|
|
if _, err := regexp.Compile(str); err != nil {
|
|
|
|
errs.Add([]string{name}, ErrRegexPattern, err.Error())
|
|
|
|
return false, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func addGlobOrRegexPatternRule() {
|
|
|
|
binding.AddRule(&binding.Rule{
|
|
|
|
IsMatch: func(rule string) bool {
|
|
|
|
return rule == "GlobOrRegexPattern"
|
|
|
|
},
|
2019-09-09 11:18:21 +05:30
|
|
|
IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
|
2021-06-25 19:58:55 +05:30
|
|
|
str := strings.TrimSpace(fmt.Sprintf("%v", val))
|
2019-09-09 11:18:21 +05:30
|
|
|
|
2021-06-25 19:58:55 +05:30
|
|
|
if len(str) >= 2 && strings.HasPrefix(str, "/") && strings.HasSuffix(str, "/") {
|
|
|
|
return regexPatternValidator(errs, name, str[1:len(str)-1])
|
2019-09-09 11:18:21 +05:30
|
|
|
}
|
2021-06-25 19:58:55 +05:30
|
|
|
return globPatternValidator(errs, name, val)
|
2019-09-09 11:18:21 +05:30
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-04 14:34:08 +05:30
|
|
|
func addUsernamePatternRule() {
|
|
|
|
binding.AddRule(&binding.Rule{
|
|
|
|
IsMatch: func(rule string) bool {
|
|
|
|
return rule == "Username"
|
|
|
|
},
|
|
|
|
IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
|
|
|
|
str := fmt.Sprintf("%v", val)
|
|
|
|
if !IsValidUsername(str) {
|
|
|
|
errs.Add([]string{name}, ErrUsername, "invalid username")
|
|
|
|
return false, errs
|
|
|
|
}
|
|
|
|
return true, errs
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-19 08:32:20 +05:30
|
|
|
func portOnly(hostport string) string {
|
|
|
|
colon := strings.IndexByte(hostport, ':')
|
|
|
|
if colon == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if i := strings.Index(hostport, "]:"); i != -1 {
|
|
|
|
return hostport[i+len("]:"):]
|
|
|
|
}
|
|
|
|
if strings.Contains(hostport, "]") {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return hostport[colon+len(":"):]
|
|
|
|
}
|
|
|
|
|
|
|
|
func validPort(p string) bool {
|
|
|
|
for _, r := range []byte(p) {
|
|
|
|
if r < '0' || r > '9' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|