From 9295f4ef8a5f7187003b100130bd3136abff7de3 Mon Sep 17 00:00:00 2001 From: Woolsen <41519915+woolsen@users.noreply.github.com> Date: Fri, 23 Oct 2026 18:26:25 +0800 Subject: [PATCH] fix: correct 1pctl password complexity validation (#14169) * fix: correct 1pctl password complexity validation `isValidPassword("newPassword")` passed a literal string and the condition was inverted, so the complexity check never ran. Extract the validation into a testable helper and cover both the enabled and disabled paths. Refs #12640 * Delete core/cmd/server/cmd/update_test.go --------- Co-authored-by: ssongliu --- core/cmd/server/cmd/update.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/core/cmd/server/cmd/update.go b/core/cmd/server/cmd/update.go index 1c2bf470c..2fe9f60e1 100644 --- a/core/cmd/server/cmd/update.go +++ b/core/cmd/server/cmd/update.go @@ -272,13 +272,11 @@ func password() { return } complexSetting := getSettingByKey(db, "ComplexityVerification") - if complexSetting == constant.StatusEnable { - if isValidPassword("newPassword") { - fmt.Println("\n" + i18n.GetMsgByKeyForCmd("UpdatePasswordFormat")) - return - } - } - if len(newPassword) < 6 { + switch validatePassword(newPassword, complexSetting) { + case passwordTooWeak: + fmt.Println("\n" + i18n.GetMsgByKeyForCmd("UpdatePasswordFormat")) + return + case passwordTooShort: fmt.Println(i18n.GetMsgByKeyForCmd("UpdatePasswordLen")) return } @@ -377,6 +375,25 @@ func port() { fmt.Println(std) } } + +type passwordValidation int + +const ( + passwordValid passwordValidation = iota + passwordTooShort + passwordTooWeak +) + +func validatePassword(password, complexitySetting string) passwordValidation { + if complexitySetting == constant.StatusEnable && !isValidPassword(password) { + return passwordTooWeak + } + if len(password) < 6 { + return passwordTooShort + } + return passwordValid +} + func isValidPassword(password string) bool { numCount := 0 alphaCount := 0