API returns strange things when getting api/v1/repos/{owner}/{repo}/pulls/{index}.{any extensions except diff or patch} #7869

Closed
opened 2025-11-02 07:39:50 -06:00 by GiteaMirror · 5 comments
Owner

Originally created by @qwerty287 on GitHub (Sep 20, 2021).

Gitea Version

1.16.0+dev-267-g5ac857f4d

Git Version

No response

Operating System

No response

How are you running Gitea?

Built from source code

Database

No response

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Description

If I enter an URL like https://codeberg.org/api/v1/repos/gitnex/Gitnex/pulls/975.test, it returns a strange result, the PR with the number 139. It should return a 404 error.

Screenshots

No response

Originally created by @qwerty287 on GitHub (Sep 20, 2021). ### Gitea Version 1.16.0+dev-267-g5ac857f4d ### Git Version _No response_ ### Operating System _No response_ ### How are you running Gitea? Built from source code ### Database _No response_ ### Can you reproduce the bug on the Gitea demo site? Yes ### Log Gist _No response_ ### Description If I enter an URL like https://codeberg.org/api/v1/repos/gitnex/Gitnex/pulls/975.test, it returns a strange result, the PR with the number 139. It should return a 404 error. ### Screenshots _No response_
GiteaMirror added the type/bugtype/upstreammodifies/api labels 2025-11-02 07:39:50 -06:00
Author
Owner

@6543 commented on GitHub (Sep 21, 2021):

looks like chi router do some unexpected magic ...

@6543 commented on GitHub (Sep 21, 2021): looks like chi router do some unexpected magic ...
Author
Owner

@lunny commented on GitHub (Sep 22, 2021):

This is upstream related I think.

@lunny commented on GitHub (Sep 22, 2021): This is upstream related I think.
Author
Owner

@a1012112796 commented on GitHub (Oct 5, 2021):

https://codeberg.org/api/v1/repos/gitnex/Gitnex/pulls/975.test

The reason is that GET /repos/{owner}/{repo}/pulls/{index} matched this link,
that's right. isn't it? but the indx is 975.test which isn't a number. so ctx.ParseInt64(":index") will return 0.

But I wonder why can get value from database by indx = 0 (GetPullRequestByIndex()) ?
/cc @lunny

@a1012112796 commented on GitHub (Oct 5, 2021): > https://codeberg.org/api/v1/repos/gitnex/Gitnex/pulls/975.test The reason is that `GET /repos/{owner}/{repo}/pulls/{index}` matched this link, that's right. isn't it? but the indx is `975.test` which isn't a number. so `ctx.ParseInt64(":index")` will return 0. But I wonder why can get value from database by `indx = 0` (`GetPullRequestByIndex()`) ? /cc @lunny
Author
Owner

@a1012112796 commented on GitHub (Oct 7, 2021):

another option choice:

maybe we can limit the type of indx in routes, then this link willn't be matched. example:

diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go
index 0a967e3c5..2ec72342d 100644
--- a/routers/api/v1/api.go
+++ b/routers/api/v1/api.go
@@ -897,7 +897,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
 				m.Group("/pulls", func() {
 					m.Combo("").Get(repo.ListPullRequests).
 						Post(reqToken(), mustNotBeArchived, bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)
-					m.Group("/{index}", func() {
+					m.Group("/{index:^[1-9]\\d*}", func() {
 						m.Combo("").Get(repo.GetPullRequest).
 							Patch(reqToken(), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
 						m.Get(".{diffType:diff|patch}", repo.DownloadPullDiffOrPatch)

result:
image

@a1012112796 commented on GitHub (Oct 7, 2021): another option choice: maybe we can limit the type of `indx` in routes, then this link willn't be matched. example: ```DIFF diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 0a967e3c5..2ec72342d 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -897,7 +897,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route { m.Group("/pulls", func() { m.Combo("").Get(repo.ListPullRequests). Post(reqToken(), mustNotBeArchived, bind(api.CreatePullRequestOption{}), repo.CreatePullRequest) - m.Group("/{index}", func() { + m.Group("/{index:^[1-9]\\d*}", func() { m.Combo("").Get(repo.GetPullRequest). Patch(reqToken(), bind(api.EditPullRequestOption{}), repo.EditPullRequest) m.Get(".{diffType:diff|patch}", repo.DownloadPullDiffOrPatch) ``` result: ![image](https://user-images.githubusercontent.com/25342410/136346932-2cbf3348-6042-441c-8729-a3f4c56254ac.png)
Author
Owner

@lunny commented on GitHub (Oct 7, 2021):

https://codeberg.org/api/v1/repos/gitnex/Gitnex/pulls/975.test

The reason is that GET /repos/{owner}/{repo}/pulls/{index} matched this link, that's right. isn't it? but the indx is 975.test which isn't a number. so ctx.ParseInt64(":index") will return 0.

But I wonder why can get value from database by indx = 0 (GetPullRequestByIndex()) ? /cc @lunny

Where("index = ?", index) is always safe than Get(&Example{Index: index}) because `Get will ignore all zero value fields as query conditions.

@lunny commented on GitHub (Oct 7, 2021): > > https://codeberg.org/api/v1/repos/gitnex/Gitnex/pulls/975.test > > The reason is that `GET /repos/{owner}/{repo}/pulls/{index}` matched this link, that's right. isn't it? but the indx is `975.test` which isn't a number. so `ctx.ParseInt64(":index")` will return 0. > > But I wonder why can get value from database by `indx = 0` (`GetPullRequestByIndex()`) ? /cc @lunny `Where("index = ?", index)` is always safe than `Get(&Example{Index: index})` because `Get will ignore all zero value fields as query conditions.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#7869