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 5 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: 2 additions & 8 deletions models/db/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@ package db
import (
"errors"
"fmt"
"regexp"
"strings"
"unicode/utf8"
)

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

// AlphaDashDotPattern characters prohibited in a user name (anything except A-Za-z0-9_.-)
AlphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
)
// ErrNameEmpty name is empty error
var ErrNameEmpty = errors.New("name is empty")

// ErrNameReserved represents a "reserved name" error.
type ErrNameReserved struct {
Expand Down
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 user_model.IsValidUsername(name) {
// Note: usually this error is normally caught up earlier in the UI
return db.ErrNameCharsNotAllowed{Name: name}
}
Expand Down
14 changes: 14 additions & 0 deletions models/user/name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 user

import "regexp"

var validUsernamePattern = regexp.MustCompile(`^[\da-zA-Z][-.\w]*$`)

// IsValidUsername checks if name is valid
func IsValidUsername(name string) bool {
return validUsernamePattern.MatchString(name)
}
22 changes: 22 additions & 0 deletions models/user/name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 user

import (
"testing"

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

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

assert.False(t, IsValidUsername(""))
assert.False(t, IsValidUsername(".abc"))
assert.False(t, IsValidUsername("a/bc"))
assert.False(t, IsValidUsername("☁️"))
}
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 IsValidUsername(name) {
// Note: usually this error is normally caught up earlier in the UI
return db.ErrNameCharsNotAllowed{Name: name}
}
Expand Down