🐛 bug: fasthttp errors cause panic when Params is used (#3055)

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
pull/3062/head
M. Efe Çetin 2024-07-05 10:18:34 +03:00 committed by GitHub
parent d17eb99377
commit 4e5a501a47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

6
ctx.go
View File

@ -1069,11 +1069,13 @@ func (c *DefaultCtx) Params(key string, defaultValue ...string) string {
if key == "*" || key == "+" {
key += "1"
}
for i := range c.route.Params {
route := c.Route()
for i := range route.Params {
if len(key) != len(c.route.Params[i]) {
continue
}
if c.route.Params[i] == key || (!c.app.config.CaseSensitive && utils.EqualFold(c.route.Params[i], key)) {
if route.Params[i] == key || (!c.app.config.CaseSensitive && utils.EqualFold(route.Params[i], key)) {
// in case values are not here
if len(c.values) <= i || len(c.values[i]) == 0 {
break

View File

@ -2372,6 +2372,25 @@ func Test_Ctx_Params(t *testing.T) {
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
func Test_Ctx_Params_ErrorHandler_Panic_Issue_2832(t *testing.T) {
t.Parallel()
app := New(Config{
ErrorHandler: func(c Ctx, _ error) error {
return c.SendString(c.Params("user"))
},
BodyLimit: 1 * 1024,
})
app.Get("/test/:user", func(_ Ctx) error {
return NewError(StatusInternalServerError, "error")
})
largeBody := make([]byte, 2*1024)
_, err := app.Test(httptest.NewRequest(MethodGet, "/test/john", bytes.NewReader(largeBody)))
require.ErrorIs(t, err, fasthttp.ErrBodyTooLarge, "app.Test(req)")
}
func Test_Ctx_Params_Case_Sensitive(t *testing.T) {
t.Parallel()
app := New(Config{CaseSensitive: true})