Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in storageHandler #27446

Merged
merged 3 commits into from
Oct 6, 2023
Merged

Conversation

sryze
Copy link
Contributor

@sryze sryze commented Oct 4, 2023

storageHandler() is written as a middleware but is used as an endpoint handler, and thus next is actually nil, which causes a null pointer dereference when a request URL does not match the pattern (where it calls next.ServerHTTP()).

Example CURL command to trigger the panic:

curl -I "http:https://yourhost/gitea//avatars/a"

Fixes #27409


Note: the diff looks big but it's actually a small change - all I did was to remove the outer closure (and one level of indentation) and removed the HTTP method and pattern checks as they seem redundant because go-chi already does those checks. You might want to check "Hide whitespace" when reviewing it.

Alternative solution (a bit simpler): append , misc.DummyOK to the route declarations that utilize storageHandler() - this makes it return an empty response when the URL is invalid. I've tested this one and it works too. Or maybe it would be better to return a 400 error in that case (?)

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 4, 2023
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 4, 2023
@lunny lunny added type/bug type/enhancement An improvement of existing functionality backport/v1.20 This PR should be backported to Gitea 1.20 backport/v1.21 This PR should be backported to Gitea 1.21 and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 5, 2023
@lunny lunny added this to the 1.22.0 milestone Oct 5, 2023
@lunny
Copy link
Member

lunny commented Oct 5, 2023

The prefix parameter could be removed.

@sryze
Copy link
Contributor Author

sryze commented Oct 5, 2023

@lunny Do you mean that it could be extracted from request URL?

@lunny
Copy link
Member

lunny commented Oct 5, 2023

If it's a http.HandlerFunc function, It's OK.

@lunny lunny requested a review from wxiaoguang October 5, 2023 08:22
@wxiaoguang
Copy link
Contributor

if req.Method != "GET" && req.Method != "HEAD" {
   405 Method Not Allowed
}

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Oct 5, 2023
storageHandler() is written as a middleware but is used as an endpoint
handler, and thus `next` is actually `nil`, which causes a null pointer
dereference when a request URL does not match the pattern (where it
calls `next.ServerHTTP()`).

Example CURL command to trigger the panic:

```
curl -I "http:https://yourhost/gitea//avatars/a"
```

Fixes go-gitea#27409
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 5, 2023
@sryze
Copy link
Contributor Author

sryze commented Oct 5, 2023

if req.Method != "GET" && req.Method != "HEAD" {
   405 Method Not Allowed
}

OK, added it back

@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 5, 2023
@sryze
Copy link
Contributor Author

sryze commented Oct 5, 2023

The prefix parameter could be removed.

If the prefix parameter is removed, funcInfo would be looked up on every request - is that OK?

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Oct 5, 2023
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Oct 6, 2023
@lunny lunny enabled auto-merge (squash) October 6, 2023 12:53
@lunny lunny merged commit 7b79be2 into go-gitea:main Oct 6, 2023
25 checks passed
GiteaBot pushed a commit to GiteaBot/gitea that referenced this pull request Oct 6, 2023
storageHandler() is written as a middleware but is used as an endpoint
handler, and thus `next` is actually `nil`, which causes a null pointer
dereference when a request URL does not match the pattern (where it
calls `next.ServerHTTP()`).

Example CURL command to trigger the panic:

```
curl -I "http:https://yourhost/gitea//avatars/a"
```

Fixes go-gitea#27409

---

Note: the diff looks big but it's actually a small change - all I did
was to remove the outer closure (and one level of indentation) ~and
removed the HTTP method and pattern checks as they seem redundant
because go-chi already does those checks~. You might want to check "Hide
whitespace" when reviewing it.

Alternative solution (a bit simpler): append `, misc.DummyOK` to the
route declarations that utilize `storageHandler()` - this makes it
return an empty response when the URL is invalid. I've tested this one
and it works too. Or maybe it would be better to return a 400 error in
that case (?)
GiteaBot pushed a commit to GiteaBot/gitea that referenced this pull request Oct 6, 2023
storageHandler() is written as a middleware but is used as an endpoint
handler, and thus `next` is actually `nil`, which causes a null pointer
dereference when a request URL does not match the pattern (where it
calls `next.ServerHTTP()`).

Example CURL command to trigger the panic:

```
curl -I "http:https://yourhost/gitea//avatars/a"
```

Fixes go-gitea#27409

---

Note: the diff looks big but it's actually a small change - all I did
was to remove the outer closure (and one level of indentation) ~and
removed the HTTP method and pattern checks as they seem redundant
because go-chi already does those checks~. You might want to check "Hide
whitespace" when reviewing it.

Alternative solution (a bit simpler): append `, misc.DummyOK` to the
route declarations that utilize `storageHandler()` - this makes it
return an empty response when the URL is invalid. I've tested this one
and it works too. Or maybe it would be better to return a 400 error in
that case (?)
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Oct 6, 2023
silverwind pushed a commit that referenced this pull request Oct 6, 2023
Backport #27446 by @sryze

storageHandler() is written as a middleware but is used as an endpoint
handler, and thus `next` is actually `nil`, which causes a null pointer
dereference when a request URL does not match the pattern (where it
calls `next.ServerHTTP()`).

Example CURL command to trigger the panic:

```
curl -I "http:https://yourhost/gitea//avatars/a"
```

Fixes #27409

---

Note: the diff looks big but it's actually a small change - all I did
was to remove the outer closure (and one level of indentation) ~and
removed the HTTP method and pattern checks as they seem redundant
because go-chi already does those checks~. You might want to check "Hide
whitespace" when reviewing it.

Alternative solution (a bit simpler): append `, misc.DummyOK` to the
route declarations that utilize `storageHandler()` - this makes it
return an empty response when the URL is invalid. I've tested this one
and it works too. Or maybe it would be better to return a 400 error in
that case (?)

Co-authored-by: Sergey Zolotarev <[email protected]>
silverwind pushed a commit that referenced this pull request Oct 6, 2023
Backport #27446 by @sryze

storageHandler() is written as a middleware but is used as an endpoint
handler, and thus `next` is actually `nil`, which causes a null pointer
dereference when a request URL does not match the pattern (where it
calls `next.ServerHTTP()`).

Example CURL command to trigger the panic:

```
curl -I "http:https://yourhost/gitea//avatars/a"
```

Fixes #27409

---

Note: the diff looks big but it's actually a small change - all I did
was to remove the outer closure (and one level of indentation) ~and
removed the HTTP method and pattern checks as they seem redundant
because go-chi already does those checks~. You might want to check "Hide
whitespace" when reviewing it.

Alternative solution (a bit simpler): append `, misc.DummyOK` to the
route declarations that utilize `storageHandler()` - this makes it
return an empty response when the URL is invalid. I've tested this one
and it works too. Or maybe it would be better to return a 400 error in
that case (?)

Co-authored-by: Sergey Zolotarev <[email protected]>
@GiteaBot
Copy link
Contributor

GiteaBot commented Oct 6, 2023

I was unable to create a backport for 1.20. @sryze, please send one manually. 🍵

go run ./contrib/backport 27446
...  // fix git conflicts if any
go run ./contrib/backport --continue

@GiteaBot GiteaBot added the backport/manual No power to the bots! Create your backport yourself! label Oct 6, 2023
@GiteaBot
Copy link
Contributor

GiteaBot commented Oct 6, 2023

I was unable to create a backport for 1.21. @sryze, please send one manually. 🍵

go run ./contrib/backport 27446
...  // fix git conflicts if any
go run ./contrib/backport --continue

@sryze
Copy link
Contributor Author

sryze commented Oct 6, 2023

I think the bot is trying to backport the same commit twice as it appears to be already in those release branches.

zjjhot added a commit to zjjhot/gitea that referenced this pull request Oct 8, 2023
* giteaoffical/main: (79 commits)
  Pre-register OAuth application for tea (go-gitea#27509)
  Fix mermaid flowchart margin issue (go-gitea#27503)
  add a shortcut to user's profile page to admin user details (go-gitea#27299)
  Fix actionlint (go-gitea#27513)
  [skip ci] Updated translations via Crowdin
  Update JS and PY dependencies (go-gitea#27501)
  Improve feed icons and feed merge text color (go-gitea#27498)
  Downgrade `go-co-op/gocron` to v1.31.1 (go-gitea#27511)
  Enable markdownlint `no-duplicate-header` (go-gitea#27500)
  bump go-deps (go-gitea#27489)
  Apply to became a maintainer (go-gitea#27491)
  change runner for binary
  [skip ci] Updated translations via Crowdin
  Remove .exe suffix when cross-compiling on Windows (go-gitea#27448)
  move re-useable workflow
  add checkout to disk-clean
  use hosted runners for nightly actions (go-gitea#27485)
  Avoid run change title process when the title is same (go-gitea#27467)
  Fix panic in storageHandler (go-gitea#27446)
  Rename the default themes to gitea-light, gitea-dark, gitea-auto (go-gitea#27419)
  ...
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Jan 5, 2024
@lunny lunny added the backport/done All backports for this PR have been created label Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created backport/manual No power to the bots! Create your backport yourself! backport/v1.20 This PR should be backported to Gitea 1.20 backport/v1.21 This PR should be backported to Gitea 1.21 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/bug type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

panic when loading home page and 404 error
4 participants