Skip to content

Commit 40f8853

Browse files
author
Jeff McCormick
authored
add better password validation and prevent users from being created with an invalid password being passed in (#802)
1 parent b3212e2 commit 40f8853

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

apiserver/userservice/userimpl.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ func User(request *msgs.UserRequest, ns string) msgs.UserResponse {
141141
resp.Results = append(resp.Results, msg)
142142
newPassword := util.GeneratePassword(request.PasswordLength)
143143
if request.Password != "" {
144-
parts := strings.Split(request.Password, " ")
145-
if len(parts) > 1 {
144+
err := validPassword(request.Password)
145+
if err != nil {
146146
resp.Status.Code = msgs.Error
147-
resp.Status.Msg = "invalid password format, can not contain spaces"
147+
resp.Status.Msg = "invalid password format, can not contain non-alphanumerics or start with numbers"
148148
return resp
149149
}
150150
newPassword = request.Password
@@ -594,6 +594,14 @@ func CreateUser(request *msgs.CreateUserRequest, ns string) msgs.CreateUserRespo
594594
resp.Status.Msg = "user name is required to be lowercase letters and numbers only."
595595
return resp
596596
}
597+
if request.Password != "" {
598+
err := validPassword(request.Password)
599+
if err != nil {
600+
resp.Status.Code = msgs.Error
601+
resp.Status.Msg = err.Error()
602+
return resp
603+
}
604+
}
597605

598606
for _, c := range clusterList.Items {
599607
info, err := getPostgresUserInfo(ns, c.Name)
@@ -931,16 +939,33 @@ func reconfigurePgpool(clusterName, ns string) error {
931939
return err
932940
}
933941

934-
func validPassword(instr string) error {
935-
if len(instr) > 16 {
942+
func validPassword(psw string) error {
943+
944+
if len(psw) > 16 {
936945
return errors.New("valid passwords are less than 16 chars")
937946
}
938947

939-
matched, err := regexp.MatchString(`^[A-Za-z_][A-Za-z\d_]*$`, instr)
940-
log.Debugf("password valid %t", matched)
941-
if !matched {
942-
return errors.New("the password format was invalid")
948+
numbers := "0123456789"
949+
isAlpha := regexp.MustCompile(`^[A-Za-z0-9]+$`).MatchString
950+
951+
if len(psw) < 1 {
952+
return errors.New("passwords can not be zero length")
943953
}
944-
return err
954+
955+
firstChar := string(psw[0])
956+
log.Debugf("1st char is %s", firstChar)
957+
if strings.Contains(numbers, firstChar) {
958+
//log.Debugf("%s is not valid due to starting with a number", username)
959+
return errors.New("passwords can not start with a number")
960+
} else if !isAlpha(psw) {
961+
//log.Debugf("%q is not valid\n", username)
962+
return errors.New("password does not match standard pattern")
963+
964+
} else {
965+
//log.Debugf("%q is valid\n", username)
966+
return nil
967+
}
968+
969+
return nil
945970

946971
}

0 commit comments

Comments
 (0)