Skip to content

Commit 3d688bd

Browse files
authored
Fix missing password length check when change password (#3039) (#3071)
* fix missing password length check when change password * add tests for change password
1 parent ce4a52c commit 3d688bd

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

modules/test/context_tests.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/modules/context"
1313

14+
"github.com/go-macaron/session"
1415
"github.com/stretchr/testify/assert"
1516
macaron "gopkg.in/macaron.v1"
1617
)
@@ -33,6 +34,9 @@ func MockContext(t *testing.T) *context.Context {
3334
macaronContext.Render = &mockRender{ResponseWriter: macaronContext.Resp}
3435
return &context.Context{
3536
Context: macaronContext,
37+
Flash: &session.Flash{
38+
Values: make(url.Values),
39+
},
3640
}
3741
}
3842

routers/user/setting.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ func SettingsSecurityPost(ctx *context.Context, form auth.ChangePasswordForm) {
223223
return
224224
}
225225

226-
if ctx.User.IsPasswordSet() && !ctx.User.ValidatePassword(form.OldPassword) {
226+
if len(form.Password) < setting.MinPasswordLength {
227+
ctx.Flash.Error(ctx.Tr("auth.password_too_short", setting.MinPasswordLength))
228+
} else if ctx.User.IsPasswordSet() && !ctx.User.ValidatePassword(form.OldPassword) {
227229
ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
228230
} else if form.Password != form.Retype {
229231
ctx.Flash.Error(ctx.Tr("form.password_not_match"))

routers/user/setting_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package user
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/auth"
13+
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/test"
15+
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
func TestChangePassword(t *testing.T) {
20+
oldPassword := "password"
21+
setting.MinPasswordLength = 6
22+
23+
for _, req := range []struct {
24+
OldPassword string
25+
NewPassword string
26+
Retype string
27+
Message string
28+
}{
29+
{
30+
OldPassword: oldPassword,
31+
NewPassword: "123456",
32+
Retype: "123456",
33+
Message: "",
34+
},
35+
{
36+
OldPassword: oldPassword,
37+
NewPassword: "12345",
38+
Retype: "12345",
39+
Message: "auth.password_too_short",
40+
},
41+
{
42+
OldPassword: "12334",
43+
NewPassword: "123456",
44+
Retype: "123456",
45+
Message: "settings.password_incorrect",
46+
},
47+
{
48+
OldPassword: oldPassword,
49+
NewPassword: "123456",
50+
Retype: "12345",
51+
Message: "form.password_not_match",
52+
},
53+
} {
54+
models.PrepareTestEnv(t)
55+
ctx := test.MockContext(t, "user/settings/security")
56+
test.LoadUser(t, ctx, 2)
57+
test.LoadRepo(t, ctx, 1)
58+
59+
SettingsSecurityPost(ctx, auth.ChangePasswordForm{
60+
OldPassword: req.OldPassword,
61+
Password: req.NewPassword,
62+
Retype: req.Retype,
63+
})
64+
65+
assert.EqualValues(t, req.Message, ctx.Flash.ErrorMsg)
66+
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
67+
}
68+
}

0 commit comments

Comments
 (0)