A provider's fixed Headers can't say a value that changes from request
to request or from client to client. 3jRzfF52Qx on X asked for the
agent's own header values to reach the vendor. Each provider now has a
comma-separated list of request headers, saved as `passHeaders`. When the
agent's request carries one of them, its value goes to that provider as
the agent sent it.
- Where it is: the editor's "Request headers to pass" (zh 透传请求头),
under Advanced, right after Reply headers.
- The agent's value stands over a fixed Headers value of the same name.
When the agent sends none, the fixed one goes.
- Never passed on, listed or not (provider.RequestHeaderPassable):
- magpie's key: Authorization, X-Api-Key, X-Goog-Api-Key, Api-Key;
- cookies and sign-ins: Cookie, Set-Cookie(2), WWW-Authenticate,
Authentication-Info, Proxy-*;
- the connection's own fields: Host, Connection, Keep-Alive, TE,
Trailer, Transfer-Encoding, Upgrade, Expect;
- what magpie writes for the body: Content-*, Accept,
Accept-Encoding;
- Origin, Sec-* and X-Magpie-*.
Save refuses such a name with a reason (RequestHeadersOK), and so does
the editor, before the save. The editor's list is held to Go's by
TestReplyHeadersNeverAsTheProvidersAre. A name written into
providers.json by hand is dropped on read.
- Only that provider gets them. forwardOnce, which every chat,
responses, messages, count_tokens, group-member, fallback and plugin
try goes through, puts the agent's headers in the try's context under
the try's provider id (WithClientHeaders). Provider.Sign passes only
what that id lists, before an account signs, so a signed-in account's
own signing still wins. A plugin's fetch gets them in req.Header.
- Wire headers are still masked when read (5ec237568), so a passed
token header shows masked in a call's log.
Run on mote at the tree rebased on origin/main 06944e065:
- gofmt -l: clean.
- go vet: darwin (provider, gateway, gui), GOOS=linux -tags nogui
./..., and GOOS=windows ./...: ok.
- go test -timeout 45m ./internal/provider ./internal/gateway
./internal/gui: provider and gateway ok. In gui, TestCoreScriptBudget
and the i18n tests are ok and only TestMainWindowCorners/macOS_26_SDK
is red, as on main on macOS 27 (#3949).
- The core budget is not raised. This change's words, and the comments
on extra body and the never lists, were cut first. After the rebase
onto 63dc359fb it was 438 bytes over again, so three provider-editor
comments (User-Agent, balance token, plan access keys) were cut to a
line each. The core is 1,533,554 bytes, 248 under 1,533,802.
- At this head (rebased on origin/main 63dc359fb): go test -run
Budget|I18n|ReplyHeaders ./internal/gui is ok. The new provider and
gateway tests are ok. node --test pass-headers, reply-headers,
i18n-keys, header-session-hint, balance-fix and extra-body: 162 of
162 ok.
- node --test, Chromium and WebKit: pass-headers (en/zh/zh-TW/ja/de),
reply-headers, i18n-keys, provider-detect-models, header-session-hint
and extra-body: 46 of 46 ok.
- Before the budget cut, against the same base: every GUI file that
opens the provider editor and saves (45 files plus i18n-keys) had 739
of 758 ok. These were red, and red the same on a fresh origin/main
06944e065 copy: models-works-only (#4391), provider-picks-stay
(#4397) and remote-magpie (#4398). Also red: one WebKit sub-pixel
miss in provider-detect-models (#4964), which passed on main and at
this head.
New tests:
- TestRequestHeadersList: cleaning and refusals.
- TestSignPassesClientHeaders: the agent's value over the fixed one,
never magpie's key even when listed by hand, nothing for another id,
and the fixed value when nothing was sent.
- TestPassHeadersReachTheProvider: messages relayed and translated,
chat, a chat stream, responses and count_tokens.
- TestPassHeadersOnlyToTheirProvider: a fallback that lists nothing gets
nothing, the log shows the passed headers masked, and in a group the
member that lists them gets them while the one before it, which lists
nothing, doesn't.
Broken by hand, each red, then put back:
- forwardOnce without WithClientHeaders: both gateway tests red.
- RequestHeaderPassable ignoring the never list: TestRequestHeadersList
red, and TestSignPassesClientHeaders red on X-Api-Key=magpie-key
passed on.
- Sign not skipping a fixed header the agent sent: the provider test,
and the gateway test on every route, red on X-Route=route-fixed.
Not covered: embeddings, images, video and the Codex backend relay,
which don't go through forwardOnce.
Not tried:
- a real vendor or a real plugin provider with the setting;
- Windows and Linux test runs (vet only);
- a gateway test with Authorization listed by hand, which normalize
drops on read. Sign's own check is covered by
TestSignPassesClientHeaders.
Requested by 3jRzfF52Qx on X.
152 lines
4.6 KiB
Go
152 lines
4.6 KiB
Go
package provider
|
|
|
|
// Headers of the agent's request passed on to the vendor (PassHeaders,
|
|
// 3jRzfF52Qx on X): a value that changes from request to request or from
|
|
// client to client, which a provider's fixed Headers can't say, goes as the
|
|
// agent sent it. The mirror of ReplyHeaders (replyheaders.go).
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// requestHeadersNever are never passed on, listed or not: magpie's own key
|
|
// as an agent sends it (Authorization, x-api-key, x-goog-api-key, api-key)
|
|
// and the sign-ins and cookies beside it, the connection's own fields, and
|
|
// what magpie writes for the body it sends (a translated body has another
|
|
// type, length and encoding, and the transport asks for compression
|
|
// itself). The prefixes below go with them.
|
|
var requestHeadersNever = map[string]bool{
|
|
"Authorization": true, "X-Api-Key": true, "X-Goog-Api-Key": true, "Api-Key": true,
|
|
"Cookie": true, "Set-Cookie": true, "Set-Cookie2": true, "Www-Authenticate": true, "Authentication-Info": true,
|
|
"Host": true, "Connection": true, "Keep-Alive": true, "Te": true, "Trailer": true, "Transfer-Encoding": true,
|
|
"Upgrade": true, "Expect": true, "Accept": true, "Accept-Encoding": true, "Origin": true,
|
|
}
|
|
|
|
// requestHeadersNeverPrefix: Content-Length, -Type and the rest;
|
|
// Proxy-Authorization and the rest; Sec- fetch metadata; and magpie's own,
|
|
// which carry who asked and the conversation to a plugin or another
|
|
// magpie.
|
|
var requestHeadersNeverPrefix = []string{"Content-", "Proxy-", "Sec-", "X-Magpie-"}
|
|
|
|
// RequestHeaderPassable says whether a header of the agent's request may be
|
|
// passed on to the vendor at all.
|
|
func RequestHeaderPassable(name string) bool {
|
|
k := http.CanonicalHeaderKey(strings.TrimSpace(name))
|
|
if !validHeaderName(k) || requestHeadersNever[k] {
|
|
return false
|
|
}
|
|
for _, p := range requestHeadersNeverPrefix {
|
|
if strings.HasPrefix(k, p) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// RequestHeadersOK says what is wrong with a list of request headers to
|
|
// pass on, "" when nothing is.
|
|
func RequestHeadersOK(names []string) string {
|
|
for _, n := range names {
|
|
n = strings.TrimSpace(n)
|
|
if n == "" {
|
|
continue
|
|
}
|
|
if !validHeaderName(n) {
|
|
return "\"" + n + "\" isn't a header name"
|
|
}
|
|
if !RequestHeaderPassable(n) {
|
|
return n + " is never passed on: it carries a key, or magpie or the connection writes it"
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// cleanRequestHeaders is the list as saved: names trimmed and canonical,
|
|
// each once, what can't be passed on dropped; nil when nothing is left.
|
|
func cleanRequestHeaders(names []string) []string {
|
|
var out []string
|
|
for _, n := range names {
|
|
k := http.CanonicalHeaderKey(strings.TrimSpace(n))
|
|
if RequestHeaderPassable(k) && !slices.Contains(out, k) {
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// RequestHeadersNever are the names and prefixes RequestHeaderPassable
|
|
// refuses, for the GUI's own check of a list typed.
|
|
func RequestHeadersNever() (names, prefixes []string) {
|
|
for k := range requestHeadersNever {
|
|
names = append(names, k)
|
|
}
|
|
slices.Sort(names)
|
|
return names, slices.Clone(requestHeadersNeverPrefix)
|
|
}
|
|
|
|
type agentHeadersKey struct{}
|
|
|
|
type agentHeaders struct {
|
|
id string
|
|
h http.Header
|
|
}
|
|
|
|
// WithClientHeaders is ctx carrying h, the headers of the agent's request,
|
|
// for Sign to pass on to the provider id those it lists (PassHeaders):
|
|
// that provider's request alone, never another's made under the same ctx.
|
|
func WithClientHeaders(ctx context.Context, id string, h http.Header) context.Context {
|
|
if len(h) == 0 {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, agentHeadersKey{}, agentHeaders{id: id, h: h})
|
|
}
|
|
|
|
// passClientHeaders puts into h the headers of the agent's request ctx
|
|
// carries that p lists, as sent, in place of any value h has under the
|
|
// name in any case. It gives the names it put.
|
|
func (p Provider) passClientHeaders(ctx context.Context, h http.Header) []string {
|
|
if len(p.PassHeaders) == 0 {
|
|
return nil
|
|
}
|
|
c, ok := ctx.Value(agentHeadersKey{}).(agentHeaders)
|
|
if !ok || c.id != p.ID {
|
|
return nil
|
|
}
|
|
var put []string
|
|
for _, n := range p.PassHeaders {
|
|
k := http.CanonicalHeaderKey(strings.TrimSpace(n))
|
|
if slices.Contains(put, k) || !RequestHeaderPassable(k) {
|
|
continue
|
|
}
|
|
vs := valuesFold(c.h, k)
|
|
if len(vs) == 0 {
|
|
continue
|
|
}
|
|
for hk := range h {
|
|
if strings.EqualFold(hk, k) {
|
|
delete(h, hk)
|
|
}
|
|
}
|
|
h[k] = slices.Clone(vs)
|
|
put = append(put, k)
|
|
}
|
|
return put
|
|
}
|
|
|
|
// valuesFold is h's values under k, whatever the case of h's keys (a
|
|
// header map filled by hand).
|
|
func valuesFold(h http.Header, k string) []string {
|
|
if vs := h.Values(k); len(vs) > 0 {
|
|
return vs
|
|
}
|
|
for hk, hv := range h {
|
|
if strings.EqualFold(hk, k) {
|
|
return hv
|
|
}
|
|
}
|
|
return nil
|
|
}
|