A server magpie signed in to (#615) is given to the agents at the gateway's /mcp/<name>, at the address each reaches the gateway at. A WSL distro under NAT reaches it at Windows' address, so its requests come from another machine, and lanGuard refuses such a request without an enabled gateway key with a 401, which an MCP client takes for the server asking it to sign in. through() gave it no key. Its models already carry the LAN sharing key there (keyAt, whqtian on Discord); the relay didn't. The gateway's checks are unchanged: a request from another machine still needs the key, and mcpProxy still lends a sign-in beyond loopback only to a request that carried it (sharedWith). What changed: - agent.KeyAt exports keyAt: the LAN sharing key for an agent that reaches the gateway from another machine while sharing is on, else gateway.Token. - through() gives such an agent Authorization: Bearer <that key>. The gateway takes it in lanGuard, and mcpProxy drops Authorization for magpie's token, so the server never sees it. An agent on loopback, and one in WSL while nothing is shared, stay keyless. - access.LANChanged runs after sharing is turned on or off or its key is made anew; the library syncs again then when it relays any server, so the entry carries the key there is now without a visit to the Library. - docs/subsystems/library.md says so. Run: - TestSignedInServerCarriesKeyBeyondLoopback (library: a FakeWSL distro routed via 172.20.0.1, LAN on) fails on the old through() ("claude@wsl: Ubuntu under NAT is given headers map[X-Team:acme], want the LAN key as its Authorization") and passes now; with the LANChanged call commented out it fails at "after a new key ... is given" the old key. - TestMCPProxyFromWSLWithLANKey (gateway, through lanGuard as the server runs it): 401 without the key and the server not called, 200 with it, the server gets magpie's token and never the key. - go test -tags nogui ./... under a temp HOME; gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui on the touched packages. - Not tried on a real Windows + WSL NAT machine with a real OAuth MCP server; FakeWSL and httptest only. Patch by ggbdpq in #1488; the LANChanged resync added on top. Co-authored-by: ggbdpq <ggbdpq@gmail.com>
57 lines
1.9 KiB
Go
57 lines
1.9 KiB
Go
package agent
|
|
|
|
import (
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
// keyAt is the key an agent that reaches the gateway at gw sends it. On
|
|
// this machine's loopback that is gateway.Token, which the gateway takes
|
|
// from this computer only; from another machine — a WSL distro under NAT,
|
|
// which asks Windows' address — the gateway turns a request without a
|
|
// named key away, so it is the LAN sharing key, while sharing is on
|
|
// (whqtian on Discord: omp, then OpenCode, in WSL were refused).
|
|
func keyAt(gw string) string {
|
|
if onAnotherMachine(gw) {
|
|
if k := access.LANSecret(); k != "" {
|
|
return k
|
|
}
|
|
}
|
|
return gateway.Token
|
|
}
|
|
|
|
// KeyAt is keyAt, for what else magpie gives an agent that reaches the
|
|
// gateway at gw (the library's MCP servers relayed through it).
|
|
func KeyAt(gw string) string { return keyAt(gw) }
|
|
|
|
// gwKey is keyAt for the agent at p.
|
|
func (p place) gwKey() string { return keyAt(p.gw()) }
|
|
|
|
// agentKeyAt is keyAt for an agent whose key on this machine names it
|
|
// (gateway.TokenFor), for requests that carry nothing else of it.
|
|
func agentKeyAt(agent, gw string) string {
|
|
if k := keyAt(gw); k != gateway.Token {
|
|
return k
|
|
}
|
|
return gateway.TokenFor(agent)
|
|
}
|
|
|
|
// ourKey says k is a key magpie gives an agent: gateway.Token, or the LAN
|
|
// sharing key one that reaches the gateway from elsewhere is given.
|
|
func ourKey(k string) bool {
|
|
if k == gateway.Token {
|
|
return true
|
|
}
|
|
return k != "" && k == access.LANSecret()
|
|
}
|
|
|
|
// gatewayTakes says the gateway would authenticate a request carrying k: a
|
|
// caller key of this magpie's own (one issued, or a value the user brought),
|
|
// so an agent holding it in its store of its own still goes through magpie
|
|
// (#1332). A key of another magpie's, which this gateway would refuse, is
|
|
// not.
|
|
func gatewayTakes(k string) bool {
|
|
_, ok := access.Authenticate(k)
|
|
return ok
|
|
}
|