Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve valid user name check #20136

Merged
merged 23 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions models/db/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (

var (
// ErrNameEmpty name is empty error
ErrNameEmpty = errors.New("Name is empty")
ErrNameEmpty = errors.New("name is empty")

// AlphaDashDotPattern characters prohibited in a user name (anything except A-Za-z0-9_.-)
AlphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
validUsernamePattern = regexp.MustCompile(`^[\da-zA-Z][-.\w]+$`)
)

// ErrNameReserved represents a "reserved name" error.
Expand Down Expand Up @@ -89,3 +88,8 @@ func IsUsableName(names, patterns []string, name string) error {

return nil
}

// IsValidUsername checks if name is valid
func IsValidUsername(name string) bool {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
return validUsernamePattern.MatchString(name)
}
19 changes: 19 additions & 0 deletions models/db/name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2022 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 db

package db

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsValidUsername(t *testing.T) {
assert.True(t, IsValidUsername("abc"))
assert.True(t, IsValidUsername("0.b-c"))

assert.False(t, IsValidUsername(".abc"))
assert.False(t, IsValidUsername("a/bc"))
}
2 changes: 1 addition & 1 deletion models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (

// IsUsableRepoName returns true when repository is usable
func IsUsableRepoName(name string) error {
if db.AlphaDashDotPattern.MatchString(name) {
if db.IsValidUsername(name) {
// Note: usually this error is normally caught up earlier in the UI
return db.ErrNameCharsNotAllowed{Name: name}
}
Expand Down
2 changes: 1 addition & 1 deletion models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ var (
// IsUsableUsername returns an error when a username is reserved
func IsUsableUsername(name string) error {
// Validate username make sure it satisfies requirement.
if db.AlphaDashDotPattern.MatchString(name) {
if db.IsValidUsername(name) {
// Note: usually this error is normally caught up earlier in the UI
return db.ErrNameCharsNotAllowed{Name: name}
}
Expand Down