mirror of https://github.com/gofiber/fiber.git
🐛 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
parent
d17eb99377
commit
4e5a501a47
6
ctx.go
6
ctx.go
|
@ -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
|
||||
|
|
19
ctx_test.go
19
ctx_test.go
|
@ -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})
|
||||
|
|
Loading…
Reference in New Issue