1 parent
b777b2b101
commit
99b4194598
6 files changed
+91
-25
No files matched your search
@@ -16,7 +16,7 @@ import (
|
||||
|
||||
var errPasswordChangeVerification = errors.New("password change verification unavailable")
|
||||
|
||||
// 从数据库读取当前身份和主要安全邮箱,不允许客户端指定验证码接收人。
|
||||
// 从数据库读取当前身份和已验证安全邮箱,不允许客户端指定验证码接收人。
|
||||
func passwordChangeIdentity(ctx *context.Context, runtime securityRuntimeState) (*models.User, *models.ExternalEmail, string, error) {
|
||||
if ctx == nil || db.Instance == nil || runtime.external == nil || runtime.verification == nil {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
@@ -26,10 +26,27 @@ func passwordChangeIdentity(ctx *context.Context, runtime securityRuntimeState)
|
||||
if err != nil || !found || user.Disabled != 0 || user.EffectiveRole() != access.RoleSuperAdmin || user.Account != ctx.UserAccount {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
}
|
||||
binding, err := runtime.external.FindVerifiedPrimaryByUser(user.ID)
|
||||
bindings, err := runtime.external.List(user.ID)
|
||||
if err != nil {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
}
|
||||
var binding *models.ExternalEmail
|
||||
for i := range bindings {
|
||||
item := &bindings[i]
|
||||
if item.Status != models.ExternalEmailStatusVerified {
|
||||
continue
|
||||
}
|
||||
if item.EmailType == models.ExternalEmailTypePrimary {
|
||||
binding = item
|
||||
break
|
||||
}
|
||||
if binding == nil && item.EmailType == models.ExternalEmailTypeBackup {
|
||||
binding = item
|
||||
}
|
||||
}
|
||||
if binding == nil {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
}
|
||||
email, err := runtime.external.Decrypt(*binding)
|
||||
if err != nil || strings.TrimSpace(email) == "" {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
@@ -40,11 +57,11 @@ func passwordChangeIdentity(ctx *context.Context, runtime securityRuntimeState)
|
||||
func requestPasswordChangeVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request, input verificationRequest, runtime securityRuntimeState) {
|
||||
user, binding, email, err := passwordChangeIdentity(ctx, runtime)
|
||||
if err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "请确认超级管理员已绑定并验证主要外部安全邮箱")
|
||||
writeSecurityError(w, response.ParamsError, "请确认超级管理员已绑定并验证外部安全邮箱")
|
||||
return
|
||||
}
|
||||
if input.Email != "" && !strings.EqualFold(strings.TrimSpace(input.Email), email) {
|
||||
writeSecurityError(w, response.ParamsError, "邮箱地址与绑定的主安全邮箱不匹配")
|
||||
writeSecurityError(w, response.ParamsError, "邮箱地址与当前验证邮箱不匹配")
|
||||
return
|
||||
}
|
||||
challenge, err := runtime.verification.Issue(verification.IssueRequest{
|
||||
|
||||
@@ -219,7 +219,7 @@ func TestSuperAdminCannotChangePasswordWithoutEmailVerification(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSuperAdminPasswordChangeVerification(t *testing.T) {
|
||||
for _, scenario := range []string{"success", "missing", "wrong-code", "wrong-purpose", "wrong-email", "unverified", "expired", "security-version", "disabled", "invalid-password", "replay"} {
|
||||
for _, scenario := range []string{"success", "backup", "primary-priority", "binding-disabled", "binding-changed", "missing", "wrong-code", "wrong-purpose", "wrong-email", "unverified", "expired", "security-version", "disabled", "invalid-password", "replay"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
testRoot := t.TempDir()
|
||||
oldConfig, oldDB, oldSession := config.Instance, db.Instance, session.Instance
|
||||
@@ -249,7 +249,11 @@ func TestSuperAdminPasswordChangeVerification(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
binding, err := repo.Bind(externalemail.BindRequest{UserID: user.ID, Email: "owner@example.com", Type: models.ExternalEmailTypePrimary})
|
||||
bindingType := models.ExternalEmailTypePrimary
|
||||
if scenario == "backup" || scenario == "primary-priority" || scenario == "binding-changed" {
|
||||
bindingType = models.ExternalEmailTypeBackup
|
||||
}
|
||||
binding, err := repo.Bind(externalemail.BindRequest{UserID: user.ID, Email: "owner@example.com", Type: bindingType})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -274,6 +278,30 @@ func TestSuperAdminPasswordChangeVerification(t *testing.T) {
|
||||
}
|
||||
ConfigureSecurityRuntime(service, repo, nil, nil)
|
||||
ctx := &appcontext.Context{UserID: user.ID, UserAccount: user.Account, Role: access.RoleSuperAdmin, Lang: "zhCn"}
|
||||
// 客户端既不能替换收件地址,也不能为其他账号申请改密验证码。
|
||||
for _, body := range []string{
|
||||
`{"purpose":"password_change","account":"verified-super","email":"attacker@example.com"}`,
|
||||
`{"purpose":"password_change","account":"another-user"}`,
|
||||
} {
|
||||
RequestVerification(ctx, httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body)))
|
||||
if delivered.ID != "" {
|
||||
t.Fatal("issued verification for client-controlled recipient or account")
|
||||
}
|
||||
}
|
||||
addPrimary := func() {
|
||||
primary, err := repo.Bind(externalemail.BindRequest{UserID: user.ID, Email: "primary@example.com", Type: models.ExternalEmailTypePrimary})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repo.Verify(user.ID, primary.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
expectedEmail := "owner@example.com"
|
||||
if scenario == "primary-priority" {
|
||||
addPrimary()
|
||||
expectedEmail = "primary@example.com"
|
||||
}
|
||||
issueRecorder := httptest.NewRecorder()
|
||||
RequestVerification(ctx, issueRecorder, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"purpose":"password_change","account":"verified-super"}`)))
|
||||
if scenario == "unverified" {
|
||||
@@ -282,11 +310,15 @@ func TestSuperAdminPasswordChangeVerification(t *testing.T) {
|
||||
}
|
||||
return
|
||||
}
|
||||
if delivered.ID == "" || delivered.Email != "owner@example.com" {
|
||||
if delivered.ID == "" || delivered.Email != expectedEmail {
|
||||
t.Fatalf("issue failed: %s", issueRecorder.Body.String())
|
||||
}
|
||||
input := modifyPasswordRequest{Password: "NewPass123", Code: code, ChallengeID: delivered.ID}
|
||||
switch scenario {
|
||||
case "binding-disabled":
|
||||
err = repo.Disable(user.ID, binding.ID)
|
||||
case "binding-changed":
|
||||
addPrimary()
|
||||
case "missing":
|
||||
input.Code = ""
|
||||
case "wrong-code":
|
||||
@@ -340,7 +372,7 @@ func TestSuperAdminPasswordChangeVerification(t *testing.T) {
|
||||
}
|
||||
return result.ErrorNo
|
||||
}
|
||||
succeeded := scenario == "success" || scenario == "replay"
|
||||
succeeded := scenario == "success" || scenario == "backup" || scenario == "primary-priority" || scenario == "replay"
|
||||
if result := change(); (result == 0) != succeeded {
|
||||
t.Fatalf("unexpected result: %d", result)
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ func ModifyPassword(ctx *context.Context, w http.ResponseWriter, req *http.Reque
|
||||
loginIPAttempts.recordFailure(ipKey)
|
||||
log.WithContext(ctx).Info("password change authentication failed")
|
||||
if ctx.EffectiveRole() == access.RoleSuperAdmin {
|
||||
writeSecurityError(w, response.ParamsError, "请使用已绑定主安全邮箱的有效验证码,失败后可重新获取")
|
||||
writeSecurityError(w, response.ParamsError, "请使用已绑定安全邮箱的有效验证码,失败后可重新获取")
|
||||
} else {
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "aperror"), "").FPrint(w)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user