From 7450fa5f27dd4e20a4518ae64176f5d09d05672a Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 10 Oct 2026 17:41:09 +0800 Subject: [PATCH] provider: a request header typed in lower case is sent once and is the user's, on the model list, the tests and requests (#1487, Dudung1018) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A provider's own headers were written into the request as typed (req.Header[k] = …) to keep their case, beside magpie's own written with Header.Set under the canonical name. A "user-agent" or "authorization" typed in lower case, as devtools show headers, therefore went out next to magpie's: two User-Agents, magpie's (or Go's Go-http-client/1.1, which Go's HTTP/1 writer adds when it finds no canonical "User-Agent") first. The reporter's relay checks the client and answered every probe 401 "unauthorized client detected", so the headers looked ignored. catalog.PutUserHeader now takes every header of the same name in any case off before writing the user's, and keeps User-Agent under its canonical name so Go adds none. It is used everywhere the provider's headers are applied as typed: Sign (forwarded calls, endpoint tests, detection), the model lists (catalog FetchURL/FetchAt, Gemini pages), Kilo's list and the Gemini image path. Azure's list merge drops an api-key of another case too. The balance read already used Header.Set. Run: curl against the reporter's relay (ps.air-outer.com, HTTP/1.1): User-Agent "claude-cli/2.1.0 (external, cli)" alone gets past its client check (to "无效的令牌" for a fake key); curl/8, or Go-http-client/1.1 ahead of the claude-cli one, gets "unauthorized client detected". A scratch Go client confirmed Go sends "User-Agent: Go-http-client/1.1" beside a lower-case "user-agent". TestUserHeadersInLowerCaseAreSentOnce (HTTP/1.1 httptest server that refuses two User-Agents or two Authorizations) fails without the change on list, endpoints and detect (User-Agent ["Go-http-client/1.1" "claude-cli/…"], Authorization ["Bearer sk-test" "Bearer relay-own"]) and passes with it. Not tried through a built magpie against the relay with the reporter's key. --- internal/catalog/gemini_list.go | 2 +- internal/catalog/live.go | 2 +- internal/catalog/userheader.go | 29 +++++++++ internal/gateway/draw.go | 2 +- internal/provider/account.go | 7 ++- internal/provider/azure.go | 6 ++ internal/provider/kilo.go | 2 +- internal/provider/userheader_case_test.go | 77 +++++++++++++++++++++++ 8 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 internal/catalog/userheader.go create mode 100644 internal/provider/userheader_case_test.go diff --git a/internal/catalog/gemini_list.go b/internal/catalog/gemini_list.go index 0366faf99..18bb7f01e 100644 --- a/internal/catalog/gemini_list.go +++ b/internal/catalog/gemini_list.go @@ -70,7 +70,7 @@ func geminiPage(ctx context.Context, u, key string, headers map[string]string) ( req.Header.Set("x-goog-api-key", key) } for k, v := range headers { - req.Header[k] = []string{v} + PutUserHeader(req.Header, k, v) } res, err := http.DefaultClient.Do(req) if err != nil { diff --git a/internal/catalog/live.go b/internal/catalog/live.go index ec4a85790..c29e83f9a 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -253,7 +253,7 @@ func fetchOne(ctx context.Context, url, key string, anthropic bool, headers map[ // The user's own headers, after the defaults so a private auth scheme // wins. Written directly so the name keeps the exact case the user typed. for k, v := range headers { - req.Header[k] = []string{v} + PutUserHeader(req.Header, k, v) } res, err := http.DefaultClient.Do(req) if err != nil { diff --git a/internal/catalog/userheader.go b/internal/catalog/userheader.go new file mode 100644 index 000000000..a91181eb8 --- /dev/null +++ b/internal/catalog/userheader.go @@ -0,0 +1,29 @@ +package catalog + +import ( + "net/http" + "strings" +) + +// PutUserHeader writes a request header the user typed (a provider's own +// headers) onto h. Every header already there under the same name in any +// case is taken off first: Header.Set keys a name by its canonical form, +// while the user's goes in as typed (some gateways match names +// case-sensitively), so a "user-agent" or "authorization" typed in lower +// case used to ride beside magpie's own — two of the header, magpie's +// first (#1487, a relay that checks the client turned the request away). +// +// User-Agent itself is kept under its canonical name: Go's HTTP/1 writer +// looks only there, and adds its own Go-http-client/1.1 when it finds +// none, ahead of a "user-agent" written in any other case. +func PutUserHeader(h http.Header, name, value string) { + for k := range h { + if strings.EqualFold(k, name) { + delete(h, k) + } + } + if strings.EqualFold(name, "User-Agent") { + name = "User-Agent" + } + h[name] = []string{value} +} diff --git a/internal/gateway/draw.go b/internal/gateway/draw.go index a081e4327..03555cb31 100644 --- a/internal/gateway/draw.go +++ b/internal/gateway/draw.go @@ -720,7 +720,7 @@ func (s *Server) sendWith(ctx context.Context, p provider.Provider, method, url, // Google's API keys go in their own header req.Header.Set("x-goog-api-key", p.Key) for k, v := range p.Headers { - req.Header[k] = []string{v} + catalog.PutUserHeader(req.Header, k, v) } } for k, v := range extra { diff --git a/internal/provider/account.go b/internal/provider/account.go index 1fbce6e81..08202da6b 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -193,8 +193,9 @@ func (p Provider) Sign(ctx context.Context, req *http.Request, proto Protocol, b } // The user's own headers ride on plain key+URL providers, after auth so // they can override a default when a gateway insists on a private scheme. - // Written to the map directly, not via Set, so the name keeps the exact - // case the user typed — some gateways match header names case-sensitively. + // Written with the name as the user typed it — some gateways match + // header names case-sensitively — in place of the same header in any + // other case (PutUserHeader), so it is sent once, theirs. // A list the request has its own of (anthropic-beta) is added to, not // replaced: the user's betas go after those the agent asked. for k, v := range p.Headers { @@ -202,7 +203,7 @@ func (p Provider) Sign(ctx context.Context, req *http.Request, proto Protocol, b MergeList(req.Header, k, v) continue } - req.Header[k] = []string{v} + catalog.PutUserHeader(req.Header, k, v) } return nil } diff --git a/internal/provider/azure.go b/internal/provider/azure.go index 3ac1d2c10..ad0f118d3 100644 --- a/internal/provider/azure.go +++ b/internal/provider/azure.go @@ -138,6 +138,12 @@ func (p Provider) azureModels(ctx context.Context) ([]catalog.Model, string, err headers[k] = v } for k, v := range p.Headers { + // the user's api-key, in any case, is the one sent + for had := range headers { + if strings.EqualFold(had, k) { + delete(headers, had) + } + } headers[k] = v } var errs []string diff --git a/internal/provider/kilo.go b/internal/provider/kilo.go index 52b3d7abd..5197d3da7 100644 --- a/internal/provider/kilo.go +++ b/internal/provider/kilo.go @@ -78,7 +78,7 @@ func (p Provider) kiloModels(ctx context.Context) ([]catalog.Model, string, erro req.Header.Set("Authorization", "Bearer "+p.Key) } for k, v := range p.Headers { - req.Header[k] = []string{v} + catalog.PutUserHeader(req.Header, k, v) } res, err := http.DefaultClient.Do(req) if err != nil { diff --git a/internal/provider/userheader_case_test.go b/internal/provider/userheader_case_test.go new file mode 100644 index 000000000..0c78379f9 --- /dev/null +++ b/internal/provider/userheader_case_test.go @@ -0,0 +1,77 @@ +package provider + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) + +// A header typed in lower case, as a browser's devtools show it, is sent +// once and is the user's: a relay that checks the client (ps.air-outer.com +// answers "unauthorized client detected" to any User-Agent but Claude +// Code's) used to get Go's User-Agent first and the user's beside it, on +// the model list, the endpoint tests and the protocol detection (#1487, +// Dudung1018). The server is HTTP/1.1, as that relay is. +func TestUserHeadersInLowerCaseAreSentOnce(t *testing.T) { + const ua, auth = "claude-cli/2.1.0 (external, cli)", "Bearer relay-own" + for _, entry := range []string{"list", "endpoints", "detect"} { + t.Run(entry, func(t *testing.T) { + detectHome(t) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var uas, auths []string + for k, v := range r.Header { + switch http.CanonicalHeaderKey(k) { + case "User-Agent": + uas = append(uas, v...) + case "Authorization": + auths = append(auths, v...) + } + } + if len(uas) != 1 || uas[0] != ua || len(auths) != 1 || auths[0] != auth { + t.Logf("%s %s: User-Agent %q, Authorization %q", r.Method, r.URL.Path, uas, auths) + http.Error(w, `{"error":{"message":"unauthorized client detected"}}`, http.StatusUnauthorized) + return + } + if r.Method == http.MethodGet { + _, _ = w.Write([]byte(`{"data":[{"id":"claude-opus-5"}]}`)) + return + } + _, _ = w.Write([]byte(`{}`)) + })) + defer srv.Close() + p := Provider{ID: "relay", Key: "sk-test", Anthropic: srv.URL, + Models: []string{"claude-opus-5"}, + Headers: map[string]string{"user-agent": ua, "authorization": auth}} + ctx := context.Background() + var results []Result + switch entry { + case "list": + if _, err := p.Fetch(ctx); err != nil { + t.Fatalf("model list: %v", err) + } + return + case "endpoints": + results = p.Test(ctx) + case "detect": + detected, err := p.Detect(ctx, srv.URL, "claude-opus-5") + if err != nil { + t.Fatal(err) + } + for _, d := range detected { + if d.Protocol == Anthropic { + results = append(results, d.Result) + } + } + } + if len(results) == 0 { + t.Fatal("nothing was asked") + } + for _, r := range results { + if !r.OK { + t.Errorf("%s: %+v", r.Protocol, r) + } + } + }) + } +}