Skip to content

Commit 7e9a895

Browse files
GiteaBotlunny
andauthored
Make GetRepositoryByName more safer (go-gitea#31712) (go-gitea#31718)
Backport go-gitea#31712 by @lunny Fix go-gitea#31708 Co-authored-by: Lunny Xiao <[email protected]>
1 parent 0fb1c1f commit 7e9a895

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

models/repo/repo.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -741,17 +741,18 @@ func GetRepositoryByOwnerAndName(ctx context.Context, ownerName, repoName string
741741

742742
// GetRepositoryByName returns the repository by given name under user if exists.
743743
func GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*Repository, error) {
744-
repo := &Repository{
745-
OwnerID: ownerID,
746-
LowerName: strings.ToLower(name),
747-
}
748-
has, err := db.GetEngine(ctx).Get(repo)
744+
var repo Repository
745+
has, err := db.GetEngine(ctx).
746+
Where("`owner_id`=?", ownerID).
747+
And("`lower_name`=?", strings.ToLower(name)).
748+
NoAutoCondition().
749+
Get(&repo)
749750
if err != nil {
750751
return nil, err
751752
} else if !has {
752753
return nil, ErrRepoNotExist{0, ownerID, "", name}
753754
}
754-
return repo, err
755+
return &repo, err
755756
}
756757

757758
// getRepositoryURLPathSegments returns segments (owner, reponame) extracted from a url

0 commit comments

Comments
 (0)