Skip to content

Commit b93dc6b

Browse files
committed
fix(setup): improve password validation flow on first user setup
1 parent 7f98fa3 commit b93dc6b

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

coderd/userpassword/userpassword.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strconv"
1111
"strings"
1212

13-
passwordvalidator "github.com/wagslane/go-password-validator"
1413
"golang.org/x/crypto/pbkdf2"
1514
"golang.org/x/exp/slices"
1615
"golang.org/x/xerrors"
@@ -138,10 +137,8 @@ func hashWithSaltAndIter(password string, salt []byte, iter int) string {
138137
// It returns properly formatted errors for detailed form validation on the client.
139138
func Validate(password string) error {
140139
// Ensure passwords are secure enough!
141-
// See: https://github.com/wagslane/go-password-validator#what-entropy-value-should-i-use
142-
err := passwordvalidator.Validate(password, 52)
143-
if err != nil {
144-
return err
140+
if len(password) < 6 {
141+
return xerrors.Errorf("password must be at least %d characters", 6)
145142
}
146143
if len(password) > 64 {
147144
return xerrors.Errorf("password must be no more than %d characters", 64)

coderd/userpassword/userpassword_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package userpassword_test
66

77
import (
8+
"strings"
89
"testing"
910

1011
"github.com/stretchr/testify/require"
@@ -14,6 +15,32 @@ import (
1415

1516
func TestUserPassword(t *testing.T) {
1617
t.Parallel()
18+
19+
t.Run("Invalid - Too short password", func(t *testing.T) {
20+
t.Parallel()
21+
err := userpassword.Validate("pass")
22+
require.Error(t, err)
23+
})
24+
25+
t.Run("Invalid - Too long password", func(t *testing.T) {
26+
t.Parallel()
27+
28+
var sb strings.Builder
29+
for i := 0; i < 65; i++ {
30+
sb.WriteString("a")
31+
}
32+
33+
err := userpassword.Validate(sb.String())
34+
require.Error(t, err)
35+
})
36+
37+
t.Run("Ok", func(t *testing.T) {
38+
t.Parallel()
39+
40+
err := userpassword.Validate("CorrectPassword")
41+
require.NoError(t, err)
42+
})
43+
1744
t.Run("Legacy", func(t *testing.T) {
1845
t.Parallel()
1946
// Ensures legacy v1 passwords function for v2.

site/src/pages/SetupPage/SetupPageView.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const Language = {
3030
usernameLabel: "Username",
3131
emailInvalid: "Please enter a valid email address.",
3232
emailRequired: "Please enter an email address.",
33+
passwordTooShort: "Password should be at least 6 characters.",
34+
passwordTooLong: "Password should be no more than 64 characters.",
3335
passwordRequired: "Please enter a password.",
3436
create: "Create account",
3537
welcomeMessage: <>Welcome to Coder</>,
@@ -54,7 +56,10 @@ const validationSchema = Yup.object({
5456
.trim()
5557
.email(Language.emailInvalid)
5658
.required(Language.emailRequired),
57-
password: Yup.string().required(Language.passwordRequired),
59+
password: Yup.string()
60+
.min(6, Language.passwordTooShort)
61+
.max(64, Language.passwordTooLong)
62+
.required(Language.passwordRequired),
5863
username: nameValidator(Language.usernameLabel),
5964
trial: Yup.bool(),
6065
trial_info: Yup.object().when("trial", {

0 commit comments

Comments
 (0)