mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-26 11:56:38 +08:00
Update user.go
在 函数中发现一个严重的安全问题: user.go:541-561UpdateSelf 代码使用了硬编码的魔法字符串 来绕过密码验证,这可能被恶意利用。如果用户提交这个特殊字符串作为密码,验证器会认为密码有效,但实际上密码会被设置为空。"$I_LOVE_U"
This commit is contained in:
parent
a2d95f62c4
commit
c7371f62bc
@ -538,27 +538,52 @@ 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 := common.Validate.Struct(&user); err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "输入不合法 " + err.Error(),
|
"message": "无效的请求数据",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanUser := model.User{
|
// 移除魔法字符串,使用更安全的验证方式
|
||||||
Id: c.GetInt("id"),
|
passwordEmpty := user.Password == ""
|
||||||
Username: user.Username,
|
|
||||||
Password: user.Password,
|
if err := common.Validate.Struct(&user); err != nil {
|
||||||
DisplayName: user.DisplayName,
|
// 如果密码为空且验证失败,检查是否只是密码字段的问题
|
||||||
}
|
if passwordEmpty {
|
||||||
if user.Password == "$I_LOVE_U" {
|
// 创建临时用户对象进行验证,排除密码字段
|
||||||
user.Password = "" // rollback to what it should be
|
tempUser := user
|
||||||
cleanUser.Password = ""
|
tempUser.Password = "temp_password_for_validation"
|
||||||
}
|
if tempErr := common.Validate.Struct(&tempUser); tempErr != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": "输入不合法 " + err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": "输入不合法 " + err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanUser := model.User{
|
||||||
|
Id: c.GetInt("id"),
|
||||||
|
Username: user.Username,
|
||||||
|
DisplayName: user.DisplayName,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只有当密码不为空时才设置密码
|
||||||
|
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 {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
Loading…
Reference in New Issue
Block a user