mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-03 14:37:44 +01:00
add regular expression to check project name
This commit is contained in:
parent
19454bca74
commit
8af26bb951
@ -18,7 +18,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"regexp"
|
||||||
|
|
||||||
"github.com/vmware/harbor/dao"
|
"github.com/vmware/harbor/dao"
|
||||||
"github.com/vmware/harbor/models"
|
"github.com/vmware/harbor/models"
|
||||||
@ -285,19 +285,13 @@ func isProjectAdmin(userID int, pid int64) bool {
|
|||||||
|
|
||||||
func validateProjectReq(req projectReq) error {
|
func validateProjectReq(req projectReq) error {
|
||||||
pn := req.ProjectName
|
pn := req.ProjectName
|
||||||
if len(pn) == 0 {
|
|
||||||
return fmt.Errorf("Project name can not be empty")
|
|
||||||
}
|
|
||||||
if isIllegalLength(req.ProjectName, projectNameMinLen, projectNameMaxLen) {
|
if isIllegalLength(req.ProjectName, projectNameMinLen, projectNameMaxLen) {
|
||||||
return fmt.Errorf("project name is illegal in length. (greater than 4 or less than 30)")
|
return fmt.Errorf("Project name is illegal in length. (greater than 4 or less than 30)")
|
||||||
}
|
}
|
||||||
if isContainIllegalChar(req.ProjectName, []string{"~", "-", "$", "\\", "[", "]", "{", "}", "(", ")", "&", "^", "%", "*", "<", ">", "\"", "'", "/", "?", "@", " "}) {
|
validProjectName := regexp.MustCompile(`^[a-z0-9](?:-*[a-z0-9])*(?:[._][a-z0-9](?:-*[a-z0-9])*)*$`)
|
||||||
return fmt.Errorf("project name contains illegal characters")
|
legal := validProjectName.MatchString(pn)
|
||||||
|
if !legal {
|
||||||
|
return fmt.Errorf("Project name is not in lower case or contains illegal characters!")
|
||||||
}
|
}
|
||||||
|
|
||||||
if pn != strings.ToLower(pn) {
|
|
||||||
return fmt.Errorf("project name must be in lower case")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user