Update user.go

在 函数中发现一个严重的安全问题: user.go:541-561UpdateSelf

代码使用了硬编码的魔法字符串 来绕过密码验证,这可能被恶意利用。如果用户提交这个特殊字符串作为密码,验证器会认为密码有效,但实际上密码会被设置为空。"$I_LOVE_U"
This commit is contained in:
ayuan 2025-07-03 11:38:25 +08:00 committed by GitHub
parent a2d95f62c4
commit c7371f62bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -538,26 +538,51 @@ func UpdateSelf(c *gin.Context) {
}) })
return return
} }
if user.Password == "" { func UpdateSelf(c *gin.Context) {
user.Password = "$I_LOVE_U" // make Validator happy :) var user model.User
err := json.NewDecoder(c.Request.Body).Decode(&user)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "无效的请求数据",
})
return
} }
// 移除魔法字符串,使用更安全的验证方式
passwordEmpty := user.Password == ""
if err := common.Validate.Struct(&user); err != nil { if err := common.Validate.Struct(&user); err != nil {
// 如果密码为空且验证失败,检查是否只是密码字段的问题
if passwordEmpty {
// 创建临时用户对象进行验证,排除密码字段
tempUser := user
tempUser.Password = "temp_password_for_validation"
if tempErr := common.Validate.Struct(&tempUser); tempErr != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,
"message": "输入不合法 " + err.Error(), "message": "输入不合法 " + err.Error(),
}) })
return return
} }
} else {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
})
return
}
}
cleanUser := model.User{ cleanUser := model.User{
Id: c.GetInt("id"), Id: c.GetInt("id"),
Username: user.Username, Username: user.Username,
Password: user.Password,
DisplayName: user.DisplayName, DisplayName: user.DisplayName,
} }
if user.Password == "$I_LOVE_U" {
user.Password = "" // rollback to what it should be // 只有当密码不为空时才设置密码
cleanUser.Password = "" if !passwordEmpty {
cleanUser.Password = user.Password
} }
updatePassword := user.Password != "" updatePassword := user.Password != ""
if err := cleanUser.Update(updatePassword); err != nil { if err := cleanUser.Update(updatePassword); err != nil {