From 69f53087611b3b3fc558f6656bf68ad8603f9776 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 23 Jul 2016 18:58:18 +0800 Subject: [PATCH] #2903 use different reversed words and patterns for repository and user --- README.md | 2 +- gogs.go | 2 +- models/org.go | 2 +- models/repo.go | 42 +++++++++++------------------------------- models/user.go | 39 +++++++++++++++++++++++++++++++++++++-- templates/.VERSION | 2 +- 6 files changed, 52 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 527a9f732..e63b25c95 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) -##### Current tip version: 0.9.51 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions) +##### Current tip version: 0.9.52 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions) | Web | UI | Preview | |:-------------:|:-------:|:-------:| diff --git a/gogs.go b/gogs.go index d8da7069e..64bb1980e 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.9.51.0723" +const APP_VER = "0.9.52.0723" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/org.go b/models/org.go index d2cb18893..9cb862dec 100644 --- a/models/org.go +++ b/models/org.go @@ -94,7 +94,7 @@ func (org *User) RemoveOrgRepo(repoID int64) error { // CreateOrganization creates record of a new organization. func CreateOrganization(org, owner *User) (err error) { - if err = IsUsableName(org.Name); err != nil { + if err = IsUsableUsername(org.Name); err != nil { return err } diff --git a/models/repo.go b/models/repo.go index e1073419a..d5ca030d9 100644 --- a/models/repo.go +++ b/models/repo.go @@ -19,7 +19,6 @@ import ( "strings" "sync" "time" - "unicode/utf8" "github.com/Unknwon/cae/zip" "github.com/Unknwon/com" @@ -517,34 +516,6 @@ func (repo *Repository) CloneLink() (cl *CloneLink) { return repo.cloneLink(false) } -var ( - reservedNames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new", ".", ".."} - reservedPatterns = []string{"*.git", "*.keys", "*.wiki"} -) - -// IsUsableName checks if name is reserved or pattern of name is not allowed. -func IsUsableName(name string) error { - name = strings.TrimSpace(strings.ToLower(name)) - if utf8.RuneCountInString(name) == 0 { - return ErrNameEmpty - } - - for i := range reservedNames { - if name == reservedNames[i] { - return ErrNameReserved{name} - } - } - - for _, pat := range reservedPatterns { - if pat[0] == '*' && strings.HasSuffix(name, pat[1:]) || - (pat[len(pat)-1] == '*' && strings.HasPrefix(name, pat[:len(pat)-1])) { - return ErrNamePatternNotAllowed{pat} - } - } - - return nil -} - // Mirror represents a mirror information of repository. type Mirror struct { ID int64 `xorm:"pk autoincr"` @@ -940,8 +911,17 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C return nil } +var ( + reservedRepoNames = []string{".", ".."} + reservedRepoPatterns = []string{"*.git", "*.wiki"} +) + +func IsUsableRepoName(name string) error { + return isUsableName(reservedRepoNames, reservedRepoPatterns, name) +} + func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) { - if err = IsUsableName(repo.Name); err != nil { + if err = IsUsableRepoName(repo.Name); err != nil { return err } @@ -1209,7 +1189,7 @@ func TransferOwnership(u *User, newOwnerName string, repo *Repository) error { func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) { oldRepoName = strings.ToLower(oldRepoName) newRepoName = strings.ToLower(newRepoName) - if err = IsUsableName(newRepoName); err != nil { + if err = IsUsableRepoName(newRepoName); err != nil { return err } diff --git a/models/user.go b/models/user.go index e8e79f247..a7c877140 100644 --- a/models/user.go +++ b/models/user.go @@ -18,6 +18,7 @@ import ( "path/filepath" "strings" "time" + "unicode/utf8" "github.com/Unknwon/com" "github.com/go-xorm/xorm" @@ -468,9 +469,43 @@ func NewFakeUser() *User { } } +var ( + reversedUsernames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new", ".", ".."} + reversedUserPatterns = []string{"*.keys"} +) + +// isUsableName checks if name is reserved or pattern of name is not allowed +// based on given reversed names and patterns. +// Names are exact match, patterns can be prefix or suffix match with placeholder '*'. +func isUsableName(names, patterns []string, name string) error { + name = strings.TrimSpace(strings.ToLower(name)) + if utf8.RuneCountInString(name) == 0 { + return ErrNameEmpty + } + + for i := range names { + if name == names[i] { + return ErrNameReserved{name} + } + } + + for _, pat := range patterns { + if pat[0] == '*' && strings.HasSuffix(name, pat[1:]) || + (pat[len(pat)-1] == '*' && strings.HasPrefix(name, pat[:len(pat)-1])) { + return ErrNamePatternNotAllowed{pat} + } + } + + return nil +} + +func IsUsableUsername(name string) error { + return isUsableName(reversedUsernames, reversedUserPatterns, name) +} + // CreateUser creates record of a new user. func CreateUser(u *User) (err error) { - if err = IsUsableName(u.Name); err != nil { + if err = IsUsableUsername(u.Name); err != nil { return err } @@ -583,7 +618,7 @@ func VerifyActiveEmailCode(code, email string) *EmailAddress { // ChangeUserName changes all corresponding setting from old user name to new one. func ChangeUserName(u *User, newUserName string) (err error) { - if err = IsUsableName(newUserName); err != nil { + if err = IsUsableUsername(newUserName); err != nil { return err } diff --git a/templates/.VERSION b/templates/.VERSION index 33af4b850..82c3ae6af 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.9.51.0723 \ No newline at end of file +0.9.52.0723 \ No newline at end of file