fix: safe type assertions in auth middleware + decode error check in password reset

- middleware/auth.go: session.Get() returns interface{} which can be nil;
  use safe type assertions with ok checks to prevent panics on corrupted sessions
- controller/misc.go: replace json.NewDecoder with common.DecodeJson per project
  convention, add error check before using decoded struct
- handle-server-error.ts: add optional chaining on error.response.data to prevent
  crash when response body is undefined

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-17 22:19:31 +08:00
co-authored by Claude Opus 4.6
parent ecaa107009
commit a3c6261d47
3 changed files with 41 additions and 9 deletions
+32 -5
View File
@@ -120,7 +120,16 @@ func authHelper(c *gin.Context, minRole int) {
c.Abort()
return
}
if status.(int) == common.UserStatusDisabled {
statusVal, ok := status.(int)
if !ok {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": common.TranslateMessage(c, i18n.MsgAuthUserInfoInvalid),
})
c.Abort()
return
}
if statusVal == common.UserStatusDisabled {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": common.TranslateMessage(c, i18n.MsgAuthUserBanned),
@@ -128,7 +137,16 @@ func authHelper(c *gin.Context, minRole int) {
c.Abort()
return
}
if role.(int) < minRole {
roleVal, ok := role.(int)
if !ok {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": common.TranslateMessage(c, i18n.MsgAuthUserInfoInvalid),
})
c.Abort()
return
}
if roleVal < minRole {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": common.TranslateMessage(c, i18n.MsgAuthInsufficientPrivilege),
@@ -136,7 +154,16 @@ func authHelper(c *gin.Context, minRole int) {
c.Abort()
return
}
if !validUserInfo(username.(string), role.(int)) {
usernameStr, ok := username.(string)
if !ok {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": common.TranslateMessage(c, i18n.MsgAuthUserInfoInvalid),
})
c.Abort()
return
}
if !validUserInfo(usernameStr, roleVal) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": common.TranslateMessage(c, i18n.MsgAuthUserInfoInvalid),
@@ -146,8 +173,8 @@ func authHelper(c *gin.Context, minRole int) {
}
// 防止不同newapi版本冲突,导致数据不通用
c.Header("Auth-Version", "864b7076dbcd0a3c01b5520316720ebf")
c.Set("username", username)
c.Set("role", role)
c.Set("username", usernameStr)
c.Set("role", roleVal)
c.Set("id", id)
c.Set("group", session.Get("group"))
c.Set("user_group", session.Get("group"))