-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
net/http: ServeMux not validating methods for redirected paths #69690
Comments
This was referenced Dec 13, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Go version
go version go1.23.0 linux/amd64
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/_3usVyrcn1V
In the above Go Playground example, I'm registering a single
http.HandleFunc
with ahttp.ServeMux
and leveragingthe new pattern format to require both the path,
/hello
, and method,HTTP GET
to match for the request to be handled.Once registered, I create an
http.Server
and run it in a goroutine. Afterward, I begin sending 2 requests to the server.The first request contains
//
at the beginning of it's path and the second request does not. Note, both requests are sentwith the
HTTP POST
method.What did you see happen?
The first request actually executes the
HTTP GET
pattern handler which results in aHTTP 500
status code being returned.The second request did not execute the
HTTP GET
pattern handler and received aHTTP 405
status code.What did you expect to see?
For the both requests, I expected to receive a
HTTP 405 Method Not Allowed
to be returned since each request method isHTTP POST
.After investigating, I came across the following:
which along with the logs seen in the output of the Go Playground example all lead to the first request being matched to
http.RedirectHandler
.Now, I understand that
http.RedirectHandler
is expected behaviour but I believe this scenario to be a particular edge casethat leads to 2 expectations to collide. That being the request method matching of
http.ServeMux
and thehttp.RedirectHandler
behviour. As a user, I was geniunely surprised to not receive a
405
forPOST //hello
since cleaning//hello
makes it/hello
.I think in this situation the HTTP method should be validated before
http.RedirectHandler
is returned but I'd love to hear othersthoughts on this behaviour.
NOTE: this is an edge case that only happens for patterns whose method for matching happens to be
HTTP GET
sincehttp.RedirectHandler
redirects viaHTTP GET
i.e. incomingHTTP POST //hello
-> redirect toHTTP GET /hello
The text was updated successfully, but these errors were encountered: