-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Echo.Reverse doesn't encode parameters #2165
Comments
@sazzer are you sure that When I try with curl I get this x@x:~/code$ curl -v localhost:8000/example/abc%3fdef
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /example/abc%3fdef HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=UTF-8
< Date: Fri, 29 Apr 2022 18:11:58 GMT
< Content-Length: 18
<
* Connection #0 to host localhost left intact
/example/abc%3fdef Note: when you request Now issue if For example take this: func main() {
server := echo.New()
server.Add("GET", "/example/:value", func(c echo.Context) error {
value := url.PathEscape(c.Param("value"))
return c.String(200, c.Echo().Reverse("example_route", value))
}).Name = "example_route"
server.Start(":8000")
} and lets test it with x@x:~/code$ curl -v localhost:8000/example/abc%3fdef
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /example/abc%3fdef HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=UTF-8
< Date: Fri, 29 Apr 2022 18:31:43 GMT
< Content-Length: 20
<
* Connection #0 to host localhost left intact
/example/abc%253fdef Result is Anyway better test/example would be func TestEcho_Reverse(t *testing.T) {
e := New()
e.GET("/example/:value", func(Context) error { return nil }).Name = "myHandler"
assert.Equal(t, "/example/abc%3fdef", e.Reverse("myHandler", "abc%3fdef"))
assert.Equal(t, "/example/abc%3fdef", e.Reverse("myHandler", "abc?def")) // Actual: `/example/abc?def`
} |
I've just tweaked my test a bit to output the values that Go sees to the terminal, so the test now looks like:
Curiously it does act differently between http and curl, but I'm unsure why... With http I get:
And output from the test app is:
However, if I use curl then I get this:
And the output is:
And then doing it in Firefox acts the same as curl! I've also run this through Wireshark to make sure that the output from http isn't lying, and it's not. It reall Notably, the URL that both http and curl are sending is almost identical, but the way the server handles it is not! And, in fact, that "almost" is important. It turns out that if you send
That second one is more serious because it's impossible to tell if a value needs decoding or not. Take for example the input |
Issue Description
When using Echo.Reverse() to generate URLs to other handlers, the resulting URL is not correctly encoded. This can mean that the URLs generated are not valid.
For example, if I pass in a value of "abc?def" as a parameter to Echo.Reverse(), I'd expect the resulting URL to be "abc%3Fdef". Instead it's just "abc?def", which then means that the resulting URL is wrong - the "?def" becomes the start of the query string and not part of the path parameter.
Checklist
Expected behaviour
The parameters passed in to the URL should be URL encoded.
Actual behaviour
The parameters are not URL encoded.
Working code to debug
Then call with:
Version/commit
The text was updated successfully, but these errors were encountered: