Files
magpie/internal/gateway/user_agent.go
T
yetone abe7b00631 gateway: a key+URL provider is sent the agent's own User-Agent by default, through a remote magpie too
nb5p runs a gateway of their own behind magpie and saw nearly every
request arrive as "magpie/<version>". The agent's User-Agent now goes
on as the agent sent it, wherever magpie isn't itself the client.

What each path sends now:
- Key+URL providers (official vendor keys and custom OpenAI/Anthropic
  compatible ones), on chat, Anthropic, Responses, Gemini,
  count_tokens, embeddings, images and videos: the agent's User-Agent,
  as sent. Before, this was the opt-in passUserAgent (#1851) and the
  default was magpie's. The opt-out is magpieUserAgent:
  `magpie provider set <id> user-agent=magpie`, or untick "Send the
  agent's own User-Agent" in the editor. passUserAgent is no longer a
  field, so a file that set it true reads the same as the new default.
  A provider someone turned back off after opting in can't be told
  apart from one never touched, and now sends the agent's too.
- When the agent sent no User-Agent, and on magpie's own requests
  (classifier magpie-router/1, vision, search, the image tool, model
  lists): magpie's, unchanged.
- A header.User-Agent the user set: still wins over both.
- Subscription accounts (Claude, Codex, Copilot, Google, ZCode…),
  plugins, and OpenCode/Cline/Kilo: unchanged. Their signing sets the
  official client's UA (provider.PassesUserAgent still refuses an
  Account or OwnClient).
- Claude Code and Codex header copies: unchanged.
- Remote magpie: the near magpie still asks as magpie/<v>, because the
  far end trusts X-Magpie-Agent/Via only from that. It now adds
  X-Magpie-User-Agent (passOnUA, on forwardOnce, embeddings, draw and
  video; the classifier's decision request names none). The far end
  takes it only from a magpie/ request (callerFrom) and strips it on
  arrival (withCaller). agentUA/callerUA then hand it to userAgentFor,
  so the far vendor sees the agent's. A client that writes the header
  itself, straight to a magpie, is ignored. magpie's own UAs are never
  relayed (magpiesOwn).

Vendor check: invalid-key requests with magpie/, claude-cli, a Chrome
UA and pi's went to Anthropic, OpenAI, Gemini, DeepSeek, Moonshot, Kimi
For Coding, Zhipu, OpenRouter, xAI, DashScope, MiniMax, Volcengine Ark,
Groq and Mistral. Every vendor gave the same auth error for every UA,
so no edge-level UA gating was seen. Kimi For Coding's agent allow-list
sits behind a valid key, which wasn't tried.

Run (mote):
- go test -run 'UserAgent|RemoteMagpie|ZCode|Blocked' ./internal/gateway: ok
- go test ./internal/provider ./internal/cli ./internal/gui: ok
- node --test provider-user-agent, provider-native-compaction and
  i18n-keys (chromium and webkit; en/zh/zh-TW/ja/de; 900 and 440 wide):
  123 of 124 pass. The red is i18n-keys "a plugin's words are its own"
  (plugins' words said in files this change doesn't touch), already
  filed as #5327/#5332, #5426 and #5482.
- gofmt -l clean; go vet; GOOS=linux -tags nogui and GOOS=windows vet:
  ok at the rebased head (on f13d5f16f). Targeted gateway, provider,
  cli and gui packages (UserAgent|RemoteMagpie|ZCode|Blocked|Budget)
  ok there too; whole packages ran at the pre-rebase base 4461957b9.

Broken by hand:
- PassesUserAgent back to opt-in: TestUserAgentPassedOn/default,
  TestUserAgentFor, TestRemoteMagpieRelaysUserAgent, TestPassesUserAgent
  and TestProviderUserAgentPair go red.
- agentUA's relay off, or passOnUA a no-op: TestRemoteMagpieRelaysUserAgent
  goes red; the far vendor is asked as magpie/dev.
- withCaller's Del(X-Magpie-User-Agent) removed: stays green. Nothing
  copies incoming headers to a vendor today, so the Del only guards
  against that.

Not tried: a real remote magpie on two machines, a real valid-key vendor
request, and nb5p's own gateway.

Reported-by: nb5p on Discord
2026-11-07 17:44:31 +08:00

80 lines
3.0 KiB
Go

package gateway
import (
"context"
"net/http"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// magpieUA is the User-Agent magpie's requests go to a vendor with.
func magpieUA() string { return "magpie/" + Version }
// userAgentFor is the User-Agent a request to p goes with, ua being the
// one the request came to magpie with: the agent's own, as it sent it,
// where p passes it on (provider.PassesUserAgent: a key+URL provider not
// set to go as magpie, #1851), and magpie's where it doesn't, where the
// agent sent none, and on magpie's own
// requests — its classifier's, vision's, search's, the image tool's
// (magpiesOwn) — which no agent sent. A User-Agent the user set among the
// provider's headers is put on after, when the request is signed, and
// stands over either.
func userAgentFor(p provider.Provider, ua string) string {
if ua != "" && p.PassesUserAgent() && !magpiesOwn(ua) {
return ua
}
return magpieUA()
}
// magpiesOwn is a User-Agent magpie's own requests carry: another magpie's
// (magpie/…), the classifier's, vision's and search's (magpie-router/1,
// magpie-vision/1, magpie-search/1), and the image tool's, which names the
// agent it draws for before its own (claude-code/2.1.0 magpie-image/1)
// from what the agent told it, not from a User-Agent the agent sent.
func magpiesOwn(ua string) bool {
return strings.HasPrefix(ua, "magpie/") || strings.HasPrefix(ua, "magpie-") || strings.HasSuffix(ua, " "+DrawAgent)
}
// callerUA is the User-Agent the request ctx is for came with (withCaller),
// or the agent's another magpie passed it on for; "" when there is none.
func callerUA(ctx context.Context) string {
c, _ := ctx.Value(callerCtx{}).(caller)
return c.ua
}
// stampUA puts on req, a request to p that isn't magpie's own forwarded
// one (an embedding, a drawing, a video), the User-Agent forwardOnce's
// carry: magpie's, or the agent's where p passes it on. A signed-in
// account's is left to its signing, as before, and another magpie's is
// set where it is asked.
func stampUA(ctx context.Context, p provider.Provider, h http.Header) {
if p.Account != nil || p.IsRemoteMagpie() {
return
}
h.Set("User-Agent", userAgentFor(p, callerUA(ctx)))
}
// agentUA is the User-Agent of the agent a request with headers in is
// for: the one it came with, or, from another magpie (magpie/…), the
// agent's that magpie named (ClientUAHeader). magpie's own requests made
// here (magpie-router/1, …) keep theirs.
func agentUA(ctx context.Context, in http.Header) string {
ua := in.Get("User-Agent")
if c, ok := ctx.Value(callerCtx{}).(caller); ok && c.relayed && strings.HasPrefix(ua, "magpie/") {
return c.ua
}
return ua
}
// passOnUA names, on a request to a remote magpie, the agent's User-Agent
// ua, for that magpie to send on as this one would; magpie's own is
// never named, and a vendor is never sent the header (withCaller takes
// it off what comes in).
func passOnUA(req *http.Request, ua string) {
req.Header.Del(ClientUAHeader)
if ua != "" && !magpiesOwn(ua) {
req.Header.Set(ClientUAHeader, ua)
}
}