diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index 5c5d1ba5a..6fefc135d 100755 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -29,7 +29,7 @@ import ( type CreateRepoForm struct { UID int64 `binding:"Required"` RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` - Alias string `binding:"Required;MaxSize(100)"` + Alias string `binding:"Required;MaxSize(100);AlphaDashDotChinese"` Private bool Description string `binding:"MaxSize(1024)"` DefaultBranch string `binding:"GitRefName;MaxSize(100)"` @@ -110,6 +110,7 @@ func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) { // RepoSettingForm form for changing repository settings type RepoSettingForm struct { RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` + Alias string `binding:"Required;AlphaDashDotChinese;MaxSize(100)"` Description string `binding:"MaxSize(255)"` Website string `binding:"ValidUrl;MaxSize(255)"` Interval string diff --git a/modules/validation/binding.go b/modules/validation/binding.go index 1c67878ea..b608cdea2 100644 --- a/modules/validation/binding.go +++ b/modules/validation/binding.go @@ -19,6 +19,8 @@ const ( // ErrGlobPattern is returned when glob pattern is invalid ErrGlobPattern = "GlobPattern" + + ErrAlphaDashDotChinese = "AlphaDashDotChineseError" ) var ( @@ -26,6 +28,8 @@ var ( // They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere. // They cannot have question-mark ?, asterisk *, or open bracket [ anywhere GitRefNamePatternInvalid = regexp.MustCompile(`[\000-\037\177 \\~^:?*[]+`) + + AlphaDashDotChinese = regexp.MustCompile("^[\u4e00-\u9fa5\\.\\-_A-Za-z0-9]+$") ) // CheckGitRefAdditionalRulesValid check name is valid on additional rules @@ -53,6 +57,7 @@ func AddBindingRules() { addGitRefNameBindingRule() addValidURLBindingRule() addGlobPatternRule() + addAlphaDashDotChineseRule() } func addGitRefNameBindingRule() { @@ -117,6 +122,21 @@ func addGlobPatternRule() { }) } +func addAlphaDashDotChineseRule() { + binding.AddRule(&binding.Rule{ + IsMatch: func(rule string) bool { + return strings.HasPrefix(rule, "AlphaDashDotChinese") + }, + IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) { + if !ValidAlphaDashDotChinese(fmt.Sprintf("%v", val)) { + errs.Add([]string{name}, ErrAlphaDashDotChinese, "ErrAlphaDashDotChinese") + return false, errs + } + return true, errs + }, + }) +} + func portOnly(hostport string) string { colon := strings.IndexByte(hostport, ':') if colon == -1 { @@ -139,3 +159,7 @@ func validPort(p string) bool { } return true } + +func ValidAlphaDashDotChinese(value string) bool { + return AlphaDashDotChinese.MatchString(value) +} diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 0389471c2..053884c2b 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -344,6 +344,7 @@ require_error=不能为空。 alpha_dash_error=应该只包含字母数字、破折号 ('-') 和下划线 ('_') 字符。 alpha_dash_dot_error=应该只包含字母数字, 破折号 ('-'), 下划线 ('_') 和点 ('. ') 。 git_ref_name_error=` 必须是格式良好的 git 引用名称。` +alpha_dash_dot_chinese_error=应该只包含字母数字中文, 破折号 ('-'), 下划线 ('_') 和点 ('. ') size_error=长度必须为 %s。 min_size_error=长度最小为 %s 个字符。 max_size_error=长度最大为 %s 个字符。 diff --git a/routers/repo/repo.go b/routers/repo/repo.go index ac132c21a..e20bc7fb3 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -6,6 +6,7 @@ package repo import ( + "code.gitea.io/gitea/modules/validation" "fmt" "net/url" "os" @@ -556,19 +557,20 @@ func Status(ctx *context.Context) { }) } -var AlphaDashDotPattern = regexp.MustCompile("[^\\d\\w-_\\.]") +var repoNamePattern = regexp.MustCompile("^[0-9a-zA-Z\\.\\-_]{1,100}$") +var repoAliasPattern = regexp.MustCompile("^[\u4e00-\u9fa5\\.\\-_A-Za-z0-9]{1,100}$") // CheckName returns repository's default name(by given alias) func CheckName(ctx *context.Context) { var r = make(map[string]string, 1) q := ctx.Query("q") owner := ctx.Query("owner") - if q == "" || owner == "" { + if q == "" || owner == "" || len(q) > 100 || !validation.ValidAlphaDashDotChinese(q) { r["name"] = "" ctx.JSON(200, r) return } - if !AlphaDashDotPattern.MatchString(q) { + if repoNamePattern.MatchString(q) { r["name"] = q ctx.JSON(200, r) return