Skip to content

Commit

Permalink
Fix stange behavior of DownloadPullDiffOrPatch in incorect index (go-…
Browse files Browse the repository at this point in the history
…gitea#17223)

Fix GetPullRequestByIndex by validate index > 1

Signed-off-by: Danila Kryukov <pricly_yellow@dismail.de>

Co-authored-by: a1012112796 <1012112796@qq.com>
  • Loading branch information
2 people authored and Stelios Malathouras committed Oct 15, 2021
1 parent 9f3558e commit 4cf35d9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
3 changes: 3 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest,

// GetPullRequestByIndex returns a pull request by the given index
func GetPullRequestByIndex(repoID, index int64) (*PullRequest, error) {
if index < 1 {
return nil, ErrPullRequestNotExist{}
}
pr := &PullRequest{
BaseRepoID: repoID,
Index: index,
Expand Down
4 changes: 4 additions & 0 deletions models/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func TestGetPullRequestByIndex(t *testing.T) {
_, err = GetPullRequestByIndex(9223372036854775807, 9223372036854775807)
assert.Error(t, err)
assert.True(t, IsErrPullRequestNotExist(err))

_, err = GetPullRequestByIndex(1, 0)
assert.Error(t, err)
assert.True(t, IsErrPullRequestNotExist(err))
}

func TestGetPullRequestByID(t *testing.T) {
Expand Down
21 changes: 4 additions & 17 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1322,29 +1322,16 @@ func DownloadPullPatch(ctx *context.Context) {

// DownloadPullDiffOrPatch render a pull's raw diff or patch
func DownloadPullDiffOrPatch(ctx *context.Context, patch bool) {
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.NotFound("GetIssueByIndex", err)
if models.IsErrPullRequestNotExist(err) {
ctx.NotFound("GetPullRequestByIndex", err)
} else {
ctx.ServerError("GetIssueByIndex", err)
ctx.ServerError("GetPullRequestByIndex", err)
}
return
}

// Return not found if it's not a pull request
if !issue.IsPull {
ctx.NotFound("DownloadPullDiff",
fmt.Errorf("Issue is not a pull request"))
return
}

if err = issue.LoadPullRequest(); err != nil {
ctx.ServerError("LoadPullRequest", err)
return
}

pr := issue.PullRequest
binary := ctx.FormBool("binary")

if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch, binary); err != nil {
Expand Down

0 comments on commit 4cf35d9

Please sign in to comment.