Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,23 @@ func (c *Context) QueryParam(name string) string {
// unescape are skipped. It avoids allocating the full url.Values map for single-key lookups.
func getRawQueryParam(query, name string) string {
for query != "" {
var seg string
seg, query, _ = strings.Cut(query, "&")
if seg == "" || strings.Contains(seg, ";") {
// strings.IndexByte instead of strings.Cut skips the call to strings.Index for each segment.
seg := query
if i := strings.IndexByte(query, '&'); i >= 0 {
seg, query = query[:i], query[i+1:]
} else {
query = ""
}
key, value := seg, ""
if i := strings.IndexByte(seg, '='); i >= 0 {
key, value = seg[:i], seg[i+1:]
}
// Unescaping never makes a key longer, so a shorter key cannot match.
if len(key) < len(name) {
continue
}
key, value, _ := strings.Cut(seg, "=")
k, err := url.QueryUnescape(key)
if err != nil || k != name {
if err != nil || k != name || seg == "" || strings.Contains(seg, ";") {
continue
}
v, err := url.QueryUnescape(value)
Expand Down
3 changes: 3 additions & 0 deletions query_fastpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func TestGetRawQueryParamMatchesStdlib(t *testing.T) {
"q=%E4%B8%AD%E6%96%87", // unicode value
"empty=&x=1",
"a=1&&b=2", // empty segment

"na%6De=1&name=2", // escaped key is longer than the name it matches
"names=1&nam=2&name=3", // keys that are longer or shorter than the name
}
names := []string{"a", "b", "c", "d", "e", "name", "age", "q", "empty", "x", "a=b", "missing", ""}

Expand Down