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(server/v2): support for url encoded path parameters #23386

Closed
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions server/v2/api/grpcgateway/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ type uriMatch struct {
// matchURL attempts to find a match for the given URL.
// NOTE: if no match is found, nil is returned.
func (m uriMatcher) matchURL(u *url.URL) *uriMatch {
uriPath := strings.TrimRight(u.Path, "/")
// the url.RawPath is non-empty when URL encoded values are detected in the path params.
// this requires different handling when getting path parameter values.
isURLEncoded := false
var uriPath string
if u.RawPath != "" {
isURLEncoded = true
uriPath = u.RawPath
} else {
uriPath = u.Path
}
uriPath = strings.TrimRight(uriPath, "/")
params := make(map[string]string)

// see if we can get a simple match first.
Expand All @@ -51,7 +61,19 @@ func (m uriMatcher) matchURL(u *url.URL) *uriMatch {
case len(matches) > 1:
// first match is the URI, subsequent matches are the wild card values.
for i, name := range qmd.wildcardKeyNames {
params[name] = matches[i+1]
var matchValue string
if isURLEncoded {
// we'll try to unescape the URL encoded values,
// but if that doesn't work, we can just try the raw value.
if decodedMatch, err := url.QueryUnescape(matches[i+1]); err == nil {
matchValue = matches[i+1]
} else {
matchValue = decodedMatch
}
} else {
matchValue = matches[i+1]
}
params[name] = matchValue
Comment on lines +64 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix inverted logic in URL unescaping condition.

There's a critical logic error in the URL unescaping condition. When url.QueryUnescape succeeds, the code uses the encoded value (matches[i+1]) instead of the decoded value (decodedMatch), and vice versa.

Apply this fix:

 if isURLEncoded {
   // we'll try to unescape the URL encoded values,
   // but if that doesn't work, we can just try the raw value.
   if decodedMatch, err := url.QueryUnescape(matches[i+1]); err == nil {
-    matchValue = matches[i+1]
-  } else {
     matchValue = decodedMatch
+  } else {
+    matchValue = matches[i+1]
   }
 } else {
   matchValue = matches[i+1]
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var matchValue string
if isURLEncoded {
// we'll try to unescape the URL encoded values,
// but if that doesn't work, we can just try the raw value.
if decodedMatch, err := url.QueryUnescape(matches[i+1]); err == nil {
matchValue = matches[i+1]
} else {
matchValue = decodedMatch
}
} else {
matchValue = matches[i+1]
}
params[name] = matchValue
var matchValue string
if isURLEncoded {
// we'll try to unescape the URL encoded values,
// but if that doesn't work, we can just try the raw value.
if decodedMatch, err := url.QueryUnescape(matches[i+1]); err == nil {
matchValue = decodedMatch
} else {
matchValue = matches[i+1]
}
} else {
matchValue = matches[i+1]
}
params[name] = matchValue

}

return &uriMatch{
Expand Down
6 changes: 6 additions & 0 deletions server/v2/api/grpcgateway/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func TestMatchURI(t *testing.T) {
Params: map[string]string{"ibc_token": "ibc/token/stuff"},
},
},
{
name: "match with URL-encoded values",
uri: "https://localhost:8080/foo/bar/BrGC8eag4ptLGTPGg2c0B%2Fh9OOM%3D+hello",
mapping: map[string]string{"/foo/bar/{bytes}": "queryAddrBytes"},
expected: &uriMatch{QueryInputName: "queryAddrBytes", Params: map[string]string{"bytes": "BrGC8eag4ptLGTPGg2c0B/h9OOM= hello"}},
},
{
name: "no match should return nil",
uri: "https://localhost:8080/foo/bar",
Expand Down
Loading