Browse Source

update alias pattern check

tags/v1.22.1.3
chenyifan01 3 years ago
parent
commit
58a6e421bf
4 changed files with 32 additions and 4 deletions
  1. +2
    -1
      modules/auth/repo_form.go
  2. +24
    -0
      modules/validation/binding.go
  3. +1
    -0
      options/locale/locale_zh-CN.ini
  4. +5
    -3
      routers/repo/repo.go

+ 2
- 1
modules/auth/repo_form.go View File

@@ -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


+ 24
- 0
modules/validation/binding.go View File

@@ -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)
}

+ 1
- 0
options/locale/locale_zh-CN.ini View File

@@ -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 个字符。


+ 5
- 3
routers/repo/repo.go View File

@@ -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


Loading…
Cancel
Save