fix(mcp): keep the target query when it does not parse

mergeQueryStrings now forwards a configured query that ParseQuery
rejects, next to the client query, instead of dropping it.
This commit is contained in:
2026-11-29 00:24:56 +00:00
parent eb3846e317
commit c3adbfe073
2 changed files with 46 additions and 5 deletions
+3 -5
View File
@@ -294,11 +294,9 @@ func mergeQueryStrings(targetQuery, requestQuery string) string {
}
merged, err := url.ParseQuery(targetQuery)
if err != nil {
// targetQuery came from an already-parsed url.URL's own RawQuery,
// so this is unreachable in practice — falls back to the
// request's own query alone rather than panicking or silently
// dropping both.
return requestQuery
// url.Parse accepts a query that ParseQuery rejects (a bare
// semicolon); the configured credential still has to go upstream.
return targetQuery + "&" + requestQuery
}
reqValues, err := url.ParseQuery(requestQuery)
if err != nil {
+43
View File
@@ -0,0 +1,43 @@
package traefikllmgateway
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestMergeQueryStrings_UnparseableTargetQueryIsKept(t *testing.T) {
if got, want := mergeQueryStrings("token=s;v=1", "a=1"), "token=s;v=1&a=1"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestMergeQueryStrings_ParseableUnchanged(t *testing.T) {
cases := []struct{ target, req, want string }{
{"token=s", "", "token=s"},
{"", "a=1", "a=1"},
{"token=s", "a=1", "a=1&token=s"},
{"a=1", "a=2", "a=1&a=2"},
{"token=s", "a=1;b=2", "token=s"},
}
for _, c := range cases {
if got := mergeQueryStrings(c.target, c.req); got != c.want {
t.Errorf("merge(%q, %q) = %q, want %q", c.target, c.req, got, c.want)
}
}
}
func TestBuildUpstreamTargetURL_SemicolonTargetQueryReachesUpstream(t *testing.T) {
var gotRaw string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotRaw = r.URL.RawQuery
}))
defer up.Close()
h := newTargetRedirectGateway(t, up.URL+"/mcp?token=s;v=1", up.URL+"/a2a")
if rec := doTarget(h, "/mcp/alpha?a=1"); rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
if gotRaw != "token=s;v=1&a=1" {
t.Errorf("upstream raw query = %q, want %q", gotRaw, "token=s;v=1&a=1")
}
}