refactor: Use db.Get[] instead of db.GetEngine(ctx).Get(bean) to avoid zero value fetching wrong database record (#37977)

This PR replaces a set of struct-based `Get` lookups with explicit
`db.Get` / `db.Exist` conditions in places where zero-value fields can
lead to ambiguous matches or incorrect records being returned.

The main goal is to make read paths deterministic and avoid accidentally
matching the wrong row when only part of a struct is populated.

### What changed

- replace many `db.GetEngine(ctx).Get(bean)` calls with explicit
`builder.Eq` conditions across models such as actions, admin tasks,
issues, pull requests, repositories, users, packages, redirects,
watches, stars, and follows
- use quoted column names where needed for reserved fields like `index`,
`type`, and `name`
- add dedicated user lookup helpers for:
  - primary email
  - OAuth login source / login name
- update sign-in and OAuth-related flows to use explicit individual-user
lookups instead of partially populated `User` structs
- tighten package property and Terraform lock lookups to avoid ambiguous
reads and updates
- keep existing fallback behavior where needed, while removing reliance
on zero-value struct matching

### User-facing impact

These changes primarily affect authentication and account lookup paths:

- email/username sign-in now re-fetches users through explicit keys
- OAuth2 auto-linking now resolves users by name or primary email
explicitly
- OAuth2 login/sync now looks up users by login source, login type, and
login name explicitly
- non-individual accounts are no longer implicitly matched through
partial user lookups in these flows

This should reduce the risk of incorrect account matches and make query
behavior more predictable across the codebase.

---------

Co-authored-by: bircni <bircni@icloud.com>
This commit is contained in:
Lunny Xiao
2026-06-27 10:24:02 -07:00
committed by GitHub
parent d5e6f273f0
commit cbe1b703dc
35 changed files with 161 additions and 153 deletions
+20 -5
View File
@@ -19,6 +19,8 @@ import (
_ "gitea.dev/services/auth/source/ldap" // register the ldap source
_ "gitea.dev/services/auth/source/pam" // register the pam source
_ "gitea.dev/services/auth/source/sspi" // register the sspi source
"xorm.io/builder"
)
// UserSignIn validates user name and password.
@@ -27,9 +29,8 @@ func UserSignIn(ctx context.Context, username, password string) (*user_model.Use
isEmail := false
if strings.Contains(username, "@") {
isEmail = true
emailAddress := user_model.EmailAddress{LowerEmail: strings.ToLower(strings.TrimSpace(username))}
// check same email
has, err := db.GetEngine(ctx).Get(&emailAddress)
emailAddress, has, err := db.Get[user_model.EmailAddress](ctx, builder.Eq{"lower_email": strings.ToLower(strings.TrimSpace(username))})
if err != nil {
return nil, nil, err
}
@@ -51,9 +52,23 @@ func UserSignIn(ctx context.Context, username, password string) (*user_model.Use
}
if user != nil {
hasUser, err := user_model.GetIndividualUser(ctx, user)
if err != nil {
return nil, nil, err
var hasUser bool
var err error
if user.ID > 0 {
user, err = user_model.GetUserByID(ctx, user.ID)
if err != nil && !user_model.IsErrUserNotExist(err) {
return nil, nil, err
}
if user != nil && user.Type != user_model.UserTypeIndividual {
return nil, nil, user_model.ErrUserNotExist{Name: username}
}
hasUser = user != nil
} else if user.LowerName != "" {
user, err = user_model.GetIndividualUserByName(ctx, user.LowerName)
if err != nil && !user_model.IsErrUserNotExist(err) {
return nil, nil, err
}
hasUser = user != nil
}
if hasUser {
+3 -11
View File
@@ -61,13 +61,7 @@ func (source *Source) refresh(ctx context.Context, provider goth.Provider, u *us
}
}
user := &user_model.User{
LoginName: u.ExternalID,
LoginType: auth.OAuth2,
LoginSource: u.LoginSourceID,
}
hasUser, err := user_model.GetIndividualUser(ctx, user)
user, hasUser, err := user_model.GetIndividualUserByLoginSource(ctx, auth.OAuth2, u.LoginSourceID, u.ExternalID)
if err != nil {
return err
}
@@ -77,13 +71,11 @@ func (source *Source) refresh(ctx context.Context, provider goth.Provider, u *us
// recognizes them as a valid user, they will be able to login
// via their provider and reactivate their account.
if shouldDisable {
log.Info("SyncExternalUsers[%s] disabling user %d", source.AuthSource.Name, user.ID)
return db.WithTx(ctx, func(ctx context.Context) error {
if hasUser {
log.Info("SyncExternalUsers[%s] disabling user %d", source.AuthSource.Name, user.ID)
user.IsActive = false
err := user_model.UpdateUserCols(ctx, user, "is_active")
if err != nil {
if err := user_model.UpdateUserCols(ctx, user, "is_active"); err != nil {
return err
}
}
+6 -4
View File
@@ -19,6 +19,8 @@ import (
"gitea.dev/modules/timeutil"
git_service "gitea.dev/services/git"
notify_service "gitea.dev/services/notify"
"xorm.io/builder"
)
// CreateRefComment creates a commit reference comment to issue.
@@ -34,10 +36,10 @@ func CreateRefComment(ctx context.Context, doer *user_model.User, repo *repo_mod
}
// Check if same reference from same commit has already existed.
has, err := db.GetEngine(ctx).Get(&issues_model.Comment{
Type: issues_model.CommentTypeCommitRef,
IssueID: issue.ID,
CommitSHA: commitSHA,
has, err := db.Exist[issues_model.Comment](ctx, builder.Eq{
"`type`": issues_model.CommentTypeCommitRef,
"issue_id": issue.ID,
"commit_sha": commitSHA,
})
if err != nil {
return fmt.Errorf("check reference comment: %w", err)