Skip to content

Context.Bind does not recover from panic #577

@Paulius-Maruska

Description

@Paulius-Maruska

Description

Context.Bind does not always return the error, namely - if there is a panic call - Context.Bind will not recover from it.

Expected behaviour

Context.Bind return type is error - the expectation is that any and all errors that occur within will be returned.

Actual behaviour

Context.Bind does not recover from panic, so some errors are not returned - instead they crash the handler function.

Working code to debug

package main

import (
    "fmt"

    "github.com/labstack/echo"
    "github.com/labstack/echo/engine/standard"
)

func main() {
    app := echo.New()
    app.POST("/handler", func(c echo.Context) error {
        var obj = make([]struct{ Field string }, 0)
        if e := c.Bind(&obj); e != nil {
            return c.String(400, fmt.Sprintf("Error\n%v\n", e))
        }
        return c.String(200, fmt.Sprintf("Request data parsed succesfully\n"))
    })
    app.Run(standard.New(":8081"))
}

The way to make a panic condition (that I found) - you can send JSON content with wrong content-type header value, for example:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d '[{"Field": "foo"},{"Field": "foo"}]' http://localhost:8081/handler

This causes a panic within a standard library and it is not handled by Context.Bind. This means, that instead of returning error to the handler, so that the handler could return 400 status code - it simply crashes the entire handler coroutine.

Version/commit

I found and tested this on echo fbcdf70

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions