Skip to content

Commit 262ece0

Browse files
committed
fix: check oauthUser.Username length
1 parent 0427ddd commit 262ece0

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

controller/oauth.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ func findOrCreateOAuthUser(c *gin.Context, provider oauth.Provider, oauthUser *o
240240

241241
if oauthUser.Username != "" {
242242
if exists, err := model.CheckUserExistOrDeleted(oauthUser.Username, ""); err == nil && !exists {
243-
user.Username = oauthUser.Username
243+
// 防止索引退化
244+
if len(oauthUser.Username) <= model.UserNameMaxLength {
245+
user.Username = oauthUser.Username
246+
}
244247
}
245248
}
246249

@@ -302,12 +305,12 @@ func findOrCreateOAuthUser(c *gin.Context, provider oauth.Provider, oauthUser *o
302305
// Set the provider user ID on the user model and update
303306
provider.SetProviderUserID(user, oauthUser.ProviderUserID)
304307
if err := tx.Model(user).Updates(map[string]interface{}{
305-
"github_id": user.GitHubId,
306-
"discord_id": user.DiscordId,
307-
"oidc_id": user.OidcId,
308-
"linux_do_id": user.LinuxDOId,
309-
"wechat_id": user.WeChatId,
310-
"telegram_id": user.TelegramId,
308+
"github_id": user.GitHubId,
309+
"discord_id": user.DiscordId,
310+
"oidc_id": user.OidcId,
311+
"linux_do_id": user.LinuxDOId,
312+
"wechat_id": user.WeChatId,
313+
"telegram_id": user.TelegramId,
311314
}).Error; err != nil {
312315
return err
313316
}

model/user.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"gorm.io/gorm"
1616
)
1717

18+
const UserNameMaxLength = 20
19+
1820
// User if you add sensitive fields, don't forget to clean them in setupLogin function.
1921
// Otherwise, the sensitive information will be saved on local storage in plain text!
2022
type User struct {

0 commit comments

Comments
 (0)