Update github.go

在该函数中,有一行注释显示了一个严重错误:github.go:203-204GitHubBind

错误在第 204 行,代码从会话中检索用户 ID,但有一个带注释的第 203 行显示了原始(有问题的)实现:github.go:203// id := c.GetInt("id") // critical bug!

问题
原始的 bug 代码会尝试从 Gin 上下文中获取用户 ID,但这将失败,因为:c.GetInt("id")

用户 ID 不会在此端点的 Gin 上下文中自动设置
这可能会返回 0 或在尝试绑定 GitHub 帐户时导致 panic
然后,该函数将尝试更新 ID 为 0 的用户,而该 ID 不存在
This commit is contained in:
ayuan 2025-07-03 11:33:35 +08:00 committed by GitHub
parent 5f1c5945f8
commit a2d95f62c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -198,10 +198,26 @@ func GitHubBind(c *gin.Context) {
})
return
}
session := sessions.Default(c)
id := session.Get("id")
// id := c.GetInt("id") // critical bug!
user.Id = id.(int)
func GitHubBind(c *gin.Context) {
session := sessions.Default(c)
idInterface := session.Get("id")
if idInterface == nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "用户未登录",
})
return
}
id, ok := idInterface.(int)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "用户ID类型错误",
})
return
}
user.Id = id
err = user.FillUserById()
if err != nil {
c.JSON(http.StatusOK, gin.H{