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

fix(api): validate username with regex #6026

Merged
merged 3 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion engine/api/authentication/local/dao_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func LoadRegistrationByID(ctx context.Context, db gorp.SqlExecutor, id string) (

// InsertRegistration in database.
func InsertRegistration(ctx context.Context, db gorpmapper.SqlExecutorWithTx, ur *sdk.UserRegistration) error {
if !sdk.UsernameRegex.MatchString(ur.Username) {
if err := sdk.IsValidUsername(ur.Username); err != nil {
return sdk.WithStack(sdk.ErrInvalidUsername)
richardlt marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
6 changes: 5 additions & 1 deletion engine/api/user/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func CountAdmin(db gorp.SqlExecutor) (int64, error) {

// Insert a user in database.
func Insert(ctx context.Context, db gorpmapper.SqlExecutorWithTx, au *sdk.AuthentifiedUser) error {
if !sdk.UsernameRegex.MatchString(au.Username) {
if err := sdk.IsValidUsername(au.Username); err != nil {
return sdk.WithStack(sdk.ErrInvalidUsername)
richardlt marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -146,6 +146,10 @@ func Insert(ctx context.Context, db gorpmapper.SqlExecutorWithTx, au *sdk.Authen

// Update a user in database.
func Update(ctx context.Context, db gorpmapper.SqlExecutorWithTx, au *sdk.AuthentifiedUser) error {
if err := sdk.IsValidUsername(au.Username); err != nil {
return sdk.WithStack(sdk.ErrInvalidUsername)
richardlt marked this conversation as resolved.
Show resolved Hide resolved
}

u := authentifiedUser{AuthentifiedUser: *au}
if err := gorpmapping.UpdateAndSign(ctx, db, &u); err != nil {
return sdk.WrapError(err, "unable to update authentified user with id: %s", au.ID)
Expand Down
13 changes: 10 additions & 3 deletions sdk/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type UserRegistration struct {
Hash string `json:"-" db:"hash"` // do no return hash in json
}

var UsernameRegex = regexp.MustCompile("[a-z0-9._-]{3,32}")
var usernameRegex = regexp.MustCompile("[a-z0-9._-]{3,32}")

// AuthentifiedUser struct contains all information about a cds user.
type AuthentifiedUser struct {
Expand All @@ -49,10 +49,17 @@ type AuthentifiedUser struct {
Organization string `json:"organization,omitempty" yaml:"organization,omitempty" cli:"organization" db:"-"`
}

func IsValidUsername(username string) error {
if username == "" || username == "me" || usernameRegex.MatchString(username) {
return NewErrorFrom(ErrInvalidUsername, "invalid given username")
richardlt marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

// IsValid returns an error if given user's infos are not valid.
func (u AuthentifiedUser) IsValid() error {
if u.Username == "" || u.Username == "me" {
return NewErrorFrom(ErrWrongRequest, "invalid given username")
if err := IsValidUsername(u.Username); err != nil {
return err
}
if u.Fullname == "" {
return NewErrorFrom(ErrWrongRequest, "invalid given fullname")
Expand Down