Skip to content

Commit 2051f85

Browse files
authored
Ensure complexity, minlength and ispwned are checked on password setting (go-gitea#18005) (go-gitea#18015)
Backport go-gitea#18005 It appears that there are several places that password length, complexity and ispwned are not currently been checked when changing passwords. This PR adds these. Fix go-gitea#17977 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 3ae4c48 commit 2051f85

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

cmd/admin.go

+4
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ func runChangePassword(c *cli.Context) error {
335335
if err := initDB(); err != nil {
336336
return err
337337
}
338+
if len(c.String("password")) < setting.MinPasswordLength {
339+
return fmt.Errorf("Password is not long enough. Needs to be at least %d", setting.MinPasswordLength)
340+
}
341+
338342
if !pwd.IsComplexEnough(c.String("password")) {
339343
return errors.New("Password does not meet complexity requirements")
340344
}

routers/api/v1/admin/user.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/convert"
1717
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/password"
19+
"code.gitea.io/gitea/modules/setting"
1920
api "code.gitea.io/gitea/modules/structs"
2021
"code.gitea.io/gitea/modules/web"
2122
"code.gitea.io/gitea/routers/api/v1/user"
@@ -167,6 +168,10 @@ func EditUser(ctx *context.APIContext) {
167168
}
168169

169170
if len(form.Password) != 0 {
171+
if len(form.Password) < setting.MinPasswordLength {
172+
ctx.Error(http.StatusBadRequest, "PasswordTooShort", fmt.Errorf("password must be at least %d characters", setting.MinPasswordLength))
173+
return
174+
}
170175
if !password.IsComplexEnough(form.Password) {
171176
err := errors.New("PasswordComplexity")
172177
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)

routers/web/user/auth.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -1748,8 +1748,23 @@ func MustChangePasswordPost(ctx *context.Context) {
17481748
ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplMustChangePassword, &form)
17491749
return
17501750
}
1751+
if !password.IsComplexEnough(form.Password) {
1752+
ctx.Data["Err_Password"] = true
1753+
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplMustChangePassword, &form)
1754+
return
1755+
}
1756+
pwned, err := password.IsPwned(ctx, form.Password)
1757+
if pwned {
1758+
ctx.Data["Err_Password"] = true
1759+
errMsg := ctx.Tr("auth.password_pwned")
1760+
if err != nil {
1761+
log.Error(err.Error())
1762+
errMsg = ctx.Tr("auth.password_pwned_err")
1763+
}
1764+
ctx.RenderWithErr(errMsg, tplMustChangePassword, &form)
1765+
return
1766+
}
17511767

1752-
var err error
17531768
if err = u.SetPassword(form.Password); err != nil {
17541769
ctx.ServerError("UpdateUser", err)
17551770
return

0 commit comments

Comments
 (0)