Make GetRepositoryByName more safer (#31712)

Fix #31708
pull/31033/head^2
Lunny Xiao 4 months ago committed by GitHub
parent 88decb6913
commit d109923ed8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 13
      models/repo/repo.go

@ -745,17 +745,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

Loading…
Cancel
Save