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(Context.Bind): should unescape special char in path #2478

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion bind.go
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -35,7 +36,12 @@ func (b *DefaultBinder) BindPathParams(c Context, i interface{}) error {
values := c.ParamValues()
params := map[string][]string{}
for i, name := range names {
params[name] = []string{values[i]}
escaped, err := url.QueryUnescape(values[i])
if err != nil {
return err
}

params[name] = []string{escaped}
}
if err := b.bindData(i, params, "param"); err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
Expand Down
13 changes: 13 additions & 0 deletions bind_test.go
Expand Up @@ -468,6 +468,19 @@ func TestBindParam(t *testing.T) {
assert.Equal(t, "Jon Snow", u.Name)
}

// Bind param with escaped characters
{
c := e.NewContext(req, rec)
c.SetPath("/users/:name")
c.SetParamNames("name")
c.SetParamValues("John%2FSnow")

err := c.Bind(u)
if assert.NoError(t, err) {
assert.Equal(t, "John/Snow", u.Name)
}
}

// Second test for the absence of a param
c2 := e.NewContext(req, rec)
c2.SetPath("/users/:id")
Expand Down