Make GetRepositoryByName more safer (#31712)

Fix #31708

(cherry picked from commit d109923ed8e58bce0ad26b47385edbc79403803d)
This commit is contained in:
Lunny Xiao 2024-07-29 09:32:54 +08:00 committed by Earl Warren
parent 170c1c5152
commit 7850fa30a5
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00

View file

@ -766,17 +766,18 @@ func GetRepositoryByOwnerAndName(ctx context.Context, ownerName, repoName string
// GetRepositoryByName returns the repository by given name under user if exists. // GetRepositoryByName returns the repository by given name under user if exists.
func GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*Repository, error) { func GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*Repository, error) {
repo := &Repository{ var repo Repository
OwnerID: ownerID, has, err := db.GetEngine(ctx).
LowerName: strings.ToLower(name), Where("`owner_id`=?", ownerID).
} And("`lower_name`=?", strings.ToLower(name)).
has, err := db.GetEngine(ctx).Get(repo) NoAutoCondition().
Get(&repo)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrRepoNotExist{0, ownerID, "", name} return nil, ErrRepoNotExist{0, ownerID, "", name}
} }
return repo, err return &repo, err
} }
// getRepositoryURLPathSegments returns segments (owner, reponame) extracted from a url // getRepositoryURLPathSegments returns segments (owner, reponame) extracted from a url