Skip to content

Commit 98233ed

Browse files
GustedStelios Malathouras
Gusted
authored and
Stelios Malathouras
committed
Only allow returned deleted branche to be on repo (go-gitea#17570)
- This will only allow `GetDeletedBranchByID` to return deletedBranch which are on the repo, and thus don't return a deletedBranch from another repo. - This just should prevent possible bugs in the futher when a code is passing the wrong ID into this function.
1 parent 4aceb72 commit 98233ed

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

models/branches.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) {
536536
// GetDeletedBranchByID get a deleted branch by its ID
537537
func (repo *Repository) GetDeletedBranchByID(id int64) (*DeletedBranch, error) {
538538
deletedBranch := &DeletedBranch{}
539-
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(deletedBranch)
539+
has, err := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repo.ID).And("id = ?", id).Get(deletedBranch)
540540
if err != nil {
541541
return nil, err
542542
}

models/branches_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,28 @@ func TestRenameBranch(t *testing.T) {
128128
BranchName: "main",
129129
})
130130
}
131+
132+
func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
133+
assert.NoError(t, db.PrepareTestDatabase())
134+
135+
// Get deletedBranch with ID of 1 on repo with ID 2.
136+
// This should return a nil branch as this deleted branch
137+
// is actually on repo with ID 1.
138+
repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
139+
140+
deletedBranch, err := repo2.GetDeletedBranchByID(1)
141+
142+
// Expect no error, and the returned branch is nil.
143+
assert.NoError(t, err)
144+
assert.Nil(t, deletedBranch)
145+
146+
// Now get the deletedBranch with ID of 1 on repo with ID 1.
147+
// This should return the deletedBranch.
148+
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
149+
150+
deletedBranch, err = repo1.GetDeletedBranchByID(1)
151+
152+
// Expect no error, and the returned branch to be not nil.
153+
assert.NoError(t, err)
154+
assert.NotNil(t, deletedBranch)
155+
}

0 commit comments

Comments
 (0)