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 path parsing to use URL.EscapedPath() instead of Path and unescape individual components #660

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion protoc-gen-grpc-gateway/httprule/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

package(default_visibility = ["//:generators"])
package(default_visibility = ["//:generators", "//runtime:__subpackages__"])

go_library(
name = "go_default_library",
Expand Down
1 change: 1 addition & 0 deletions runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ go_test(
"//examples/proto/examplepb:go_default_library",
"//runtime/internal:go_default_library",
"//utilities:go_default_library",
"//protoc-gen-grpc-gateway/httprule:go_default_library",
"@com_github_golang_protobuf//jsonpb:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//ptypes:go_default_library",
Expand Down
21 changes: 19 additions & 2 deletions runtime/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"net/http"
"net/textproto"
"net/url"
"strings"

"context"

"github.com/golang/protobuf/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -148,7 +150,7 @@ func (s *ServeMux) Handle(meth string, pat Pattern, h HandlerFunc) {
func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

path := r.URL.Path
path := r.URL.EscapedPath()
if !strings.HasPrefix(path, "/") {
if s.protoErrorHandler != nil {
_, outboundMarshaler := MarshalerForRequest(s, r)
Expand All @@ -160,7 +162,22 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

components := strings.Split(path[1:], "/")
pathParts := strings.Split(path[1:], "/")
components := make([]string, len(pathParts))
for i, comp := range pathParts {
comp, err := url.PathUnescape(comp)
if err != nil {
if s.protoErrorHandler != nil {
_, outboundMarshaler := MarshalerForRequest(s, r)
sterr := status.Error(codes.InvalidArgument, err.Error())
s.protoErrorHandler(ctx, s, outboundMarshaler, w, r, sterr)
} else {
OtherErrorHandler(w, r, err.Error(), http.StatusBadRequest)
}
return
}
components[i] = comp
}
l := len(components)
var verb string
if idx := strings.LastIndex(components[l-1], ":"); idx == 0 {
Expand Down
66 changes: 66 additions & 0 deletions runtime/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/httprule"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/grpc-ecosystem/grpc-gateway/utilities"
)
Expand Down Expand Up @@ -211,3 +213,67 @@ func TestMuxServeHTTP(t *testing.T) {
}
}
}

// TestSlashEncoding ensures that slashes are supported as part of path when encoded
func TestSlashEncoding(t *testing.T) {
for _, spec := range []struct {
method string
template string
reqPath string
respStatus int
pathParams map[string]string
}{
{
method: "GET",
template: "/v1/users/{name}/profile",
reqPath: "/v1/users/some%2Fuser/profile",
respStatus: http.StatusOK,
pathParams: map[string]string{"name": "some/user"},
},
{
method: "GET",
template: "/v1/users/{name}",
reqPath: "/v1/users/some%2Fuser",
respStatus: http.StatusOK,
pathParams: map[string]string{"name": "some/user"},
},
{
method: "GET",
template: "/v1/users/{name}/profile",
reqPath: "/v1/users/some/user/profile",
respStatus: http.StatusNotFound,
},
{
method: "GET",
template: "/v1/users/{name}",
reqPath: "/v1/users/some/user",
respStatus: http.StatusNotFound,
},
} {
mux := runtime.NewServeMux()
compiled, err := httprule.Parse(spec.template)
if err != nil {
t.Fatalf("httprule.Parse(%s) failed with %v; want success", spec.template, err)
}
template := compiled.Compile()
pat, err := runtime.NewPattern(1, template.OpCodes, template.Pool, template.Verb)
if err != nil {
t.Fatalf("runtime.NewPattern(1, %#v, %#v, %q) failed with %v; want success", template.OpCodes, template.Pool, template.Verb, err)
}
mux.Handle(spec.method, pat, func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
if !reflect.DeepEqual(pathParams, spec.pathParams) {
t.Errorf("pathParams got: %v, want: %v", pathParams, spec.pathParams)
}
})
url := fmt.Sprintf("http://host.example%s", spec.reqPath)
r, err := http.NewRequest(spec.method, url, bytes.NewReader(nil))
if err != nil {
t.Fatalf("http.NewRequest(%q, %q, nil) failed with %v; want success", spec.method, url, err)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
if got, want := w.Code, spec.respStatus; got != want {
t.Errorf("w.Code = %d; want %d; req=%v", got, want, r)
}
}
}