Files
magpie/internal/gateway/middleware.go
T
yetone 9c54221814 plugins: origin magpie-key only for the sharing key used from this computer; from elsewhere it stays gateway-key
387e8572a gave a gateway hook origin "magpie-key" for the sharing key
magpie made itself, wherever it came from. But that key isn't only the
owner's agents': Gateway keys lists it (as "Magpie") with a Copy button
(copyCallerKeyBtn in plugins/gateway/keys.js), the connect section
offers it for another computer (selectConnectKey). A friend on the LAN handed it would
have read as "magpie-key", and plugins#526's switch would still have
served them the owner's memories.

Now it is "magpie-key" only from this computer (ownComputer), where
magpie writes it into agents (agent.keyAt): loopback (a WSL distro on
the WSL network is mapped to it by lanGuard), one of this computer's
own addresses (an agent calling MAGPIE_ADDR beyond loopback, ownAddr),
or a WSL distro's NAT network calling this computer's network address
(WSLNets). Never with forwarding headers, a trusted proxy's included,
nor through magpie web's page port. From anywhere else the key reads
"gateway-key", as every other key does.

docs/subsystems/gateway-request-hooks.md says so; plugins#530 and the
#526 comment are corrected to match.

Tests: TestAsyncGatewayOrigin (real Bun host) adds the sharing key from
this computer's own address and a WSL NAT address (magpie-key), and
from 10.0.0.1, through a proxy and through a trusted proxy
(gateway-key); 387e8572a's "magpie-key from 10.0.0.1" case now wants
gateway-key.

Run (on mote, at this tree):
- go test -timeout 45m ./... at 387e8572a plus this change: 80
  packages ok (gateway 662s; access and plugin cached, unchanged since
  387e8572a's full run); internal/gui red only in the same 10 real-window
  AppKit tests as 387e8572a's run, which fail the same way on origin/main
  48edbcb67 on mote: #4125, #2645.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet ./internal/gateway.

Broken by hand (each subtest red, the rest green): ownComputer dropped
from gatewayOrigin (387e8572a's rule): elsewhere, proxied and
trusted-proxy red; ownAddr dropped: own-address red; the forwarded check
dropped: trusted-proxy red; the WSL-net check dropped: wsl-nat red.

Not tried: a real WSL distro under NAT or a MAGPIE_ADDR beyond loopback
sending the sharing key to a real plugin (the peer addresses are stubbed
through ownAddr and WSLNets); a magpie in a container, whose host's
agents reach it from the container's gateway address and read as
gateway-key.
2026-10-30 20:24:54 +08:00

210 lines
7.9 KiB
Go

package gateway
import (
"context"
"net"
"net/http"
"slices"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/gwmw"
"github.com/yetone/magpie/internal/middleware"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/provider"
)
// serveAgent is serve for a request an agent sent: the agent middleware
// of the gateway's middleware point (gwmw.Agents: the JS gateway middleware
// plugins, internal/middleware, while plugins/middleware is on) see its
// body first, and its reply on the way back, in the agent's own API.
// magpie's own requests through serve (titles, the router's classifier,
// search and vision stand-ins) go without them, as their replies are
// magpie's to read.
func (s *Server) serveAgent(w http.ResponseWriter, r *http.Request, from provider.Protocol, body []byte) {
withGatewaySession(w, r)
// the agent's own turn, which the user's model for requests masking
// would change is for (maskRouteFor)
r = agentTurn(r)
// a Responses request naming the response it follows is given the
// conversation whole before anything reads it (prev_response.go)
w, r, body, kept := previousResponse(w, r, from, body)
defer kept()
w, recorded := recordConversation(w, r, from, body)
defer recorded()
// a conversation going round one tool call is answered here, before
// anything of it is asked of a hook, a middleware or a provider
if s.stoppedToolLoop(w, r, from, body) {
return
}
w, body, hooked := gatewayHooks(w, r, from, body)
defer hooked()
agents := gwmw.AgentsWanted(middlewareWanted)
if len(agents) == 0 {
s.serve(w, r, from, body)
return
}
info := gwmw.Info{Protocol: string(from), Model: modelOf(body), Stream: streamOf(body), Path: r.URL.Path, Agent: agentOf(r)}
var tried []gwmw.Tried
for _, a := range agents {
// the user's middleware runs before a route is begun: a wait
// magpie knows of (unrouted.go)
waited := noteWait(r.Context(), "JS middleware")
run := a.Begin(info)
if run == nil {
waited()
continue
}
defer run.End()
if t, ok := run.(gwmw.Tried); ok {
tried = append(tried, t)
}
var turned *gwmw.Reject
body, turned = run.Request(body)
waited()
if turned != nil {
writeError(w, from, turned.Status, turned.Message)
return
}
var done func()
w, done = run.Wrap(w)
defer done()
}
if len(tried) > 0 {
r = r.WithContext(context.WithValue(r.Context(), triedKey{}, tried))
}
s.serve(w, r, from, body)
}
type triedKey struct{}
// triedOn is body as it goes to model on p, in the agent's API: what each
// middleware of the request that is also gwmw.Tried (the JS chain's
// onProvider) made of it for that provider. Each try starts from the body
// it is given, so a change for one provider never reaches another's.
func triedOn(ctx context.Context, p provider.Provider, model string, body []byte) []byte {
tried, _ := ctx.Value(triedKey{}).([]gwmw.Tried)
if len(tried) == 0 {
return body
}
to := gwmw.To{ID: p.ID, Name: p.Name, Account: accountAgent(p), Model: model}
for _, t := range tried {
body = t.Tried(body, to)
}
return body
}
// ownAsk is ctx for a request magpie asks of its own gateway while it
// serves an agent's (a web search, an image's description): the agent's
// middleware isn't run on it, as on magpie's other own requests. Its
// runtimes are the agent request's, which one goroutine may use at a time
// (moejs), and Run has no lock to hold callers to it, while these asks go
// beside the request and each other: images described at once ran
// onProvider on one runtime, and the gateway died of "concurrent map read
// and map write" (#3695). Nor does the hooks' ctx describe them: a
// description's body is Chat and a search's Anthropic, whatever
// ctx.protocol names, and ctx.model and ctx.stream are the agent's. Nor
// is it the agent's turn, which a model set for requests masking would
// change takes (maskRouteFor): it is masked as magpie's own asks are.
func ownAsk(ctx context.Context) context.Context {
return context.WithValue(context.WithValue(ctx, triedKey{}, []gwmw.Tried(nil)), agentTurnKey{}, false)
}
// middlewareWanted: plugins.json has a JS middleware on, so plugins/middleware
// is in use.
func middlewareWanted() bool { return middleware.On() > 0 }
// gatewayHooks runs the asynchronous request hooks (OpenCode plugins'
// gateway entries, in the plugin host) on an agent's request: the body they
// leave is the one served, and they see the reply the agent got once it is
// whole. Its writer wraps the middleware's, so what they see is what the
// agent was given. With no hook it is the request as it came.
func gatewayHooks(w http.ResponseWriter, r *http.Request, from provider.Protocol, body []byte) (http.ResponseWriter, []byte, func()) {
if !plugin.HasGateway() {
return w, body, func() {}
}
info := plugin.GatewayContext{Origin: gatewayOrigin(r), Protocol: string(from), Model: modelOf(body), Stream: streamOf(body), Path: r.URL.Path, Agent: agentOf(r), Session: sessionOf(r.Header)}
if req, err := parse(from, body); err == nil {
info.Cwd = workDir(req)
info.Query = gatewayQuery(req)
}
// the plugin host, started for it if need be: a wait magpie knows of
// before a route is begun (unrouted.go)
waited := noteWait(r.Context(), "plugin hooks")
run := plugin.GatewayBegin(r.Context(), body, info)
waited()
if run == nil {
return w, body, func() {}
}
w, done := gatewayResponse(w, r, from, run.Body, run)
return w, run.Body, func() { done(); run.Close() }
}
// gatewayQuery uses the same user-authored words as routing, excluding
// agent context, tool results and images. Tool-only increments have none.
func gatewayQuery(req *Request) string {
for i := len(req.Messages) - 1; i >= 0; i-- {
m := req.Messages[i]
if m.Role != "user" {
continue
}
if query := userSaid(m); query != "" {
return query
}
}
return ""
}
// gatewayOrigin exposes provenance, never credentials or peer addresses.
// Forwarded traffic remains remote even when the administrator trusts its
// proxy for authentication: that trust does not establish conversation ownership.
//
// The sharing key magpie made itself is "magpie-key" only when it comes
// from this computer (ownComputer): that is where magpie writes it into
// the owner's own agents. The same key is listed under Gateway keys with
// a Copy button and offered to connect other computers, so it can be in a
// friend's hands too, and from anywhere else it stays "gateway-key", as
// every other gateway key does — one the owner made for someone else, a
// value a user brought, or one that didn't authenticate. A plugin can then
// serve the owner's own agents without serving a friend's key
// (plugins#526). Plugins that trust only "local" see both as not local.
func gatewayOrigin(r *http.Request) string {
if who := access.Caller(r.Context()); who.KeyID != "" && who.Default && ownComputer(r) {
return "magpie-key"
} else if who.KeyID != "" || managedKey(r) {
return "gateway-key"
}
if local(r) && !forwarded(r) {
return "local"
}
return "remote"
}
func forwarded(r *http.Request) bool {
return slices.ContainsFunc(forwardHeaders, func(h string) bool { return r.Header.Get(h) != "" })
}
// ownComputer: r's connection was made on this computer, where magpie
// writes the sharing key into agents (agent.keyAt): loopback (a WSL
// distro on the WSL network is mapped to it by lanGuard), one of this
// computer's own addresses (an agent calling MAGPIE_ADDR beyond loopback),
// or a WSL distro's address under NAT calling this computer's network
// address. Never a request with forwarding headers, a trusted proxy's
// included, nor one through magpie web's page port.
func ownComputer(r *http.Request) bool {
if forwarded(r) || viaPagePort(r) {
return false
}
if local(r) {
return true
}
a, ok := peerAddr(r)
if !ok {
return false
}
if ownAddr(a) {
return true
}
ip := net.IP(a.AsSlice())
return slices.ContainsFunc(WSLNets(), func(n *net.IPNet) bool { return n.Contains(ip) })
}