The gateway takes gateway.Token, and the keys that name an agent (gateway.TokenFor), only from loopback. When MAGPIE_ADDR pins the gateway to an address beyond loopback and sharing is on, keyAt gives the LAN sharing key, and most writers use it. T3 Code's instance still wrote gateway.Token, so every call it made got 401. Air's and OpenChamber's Checks compared against gateway.Token, so they said the wiring Sync had just written was off. The same mistake was in every writer that pairs gateway.URL() or gatewayV1() with a fixed token: - agy, Alma, Cindy, Claude Desktop, Copilot for JetBrains, Cursor's local agent, OpenHanako, Pencil, VS Code / Insiders / VSCodium, WorkBuddy-family models.json, ZCode (both files), Zed's credential, Aside's provider, and the legacy Claude token rename. - Fix: these now take agentKey(agent) or hereKey(), which are keyAt and agentKeyAt for gateway.URL(). On loopback they are unchanged. - Alma re-keys a provider that holds any of magpie's keys other than the one it should have now. - WorkBuddy and Aside also count the new key as theirs. - ZCode treats a saved address equal to the pinned gateway as this machine's, not another magpie's. What was run: - New tests: TestT3CodeKeyBeyondLoopback and TestAirAndOpenChamberChecksTakeTheKeyForTheGateway (from the reporter's patch), and TestPinnedGatewayKeyForEveryWriter, which covers the other writers. - With the product files reverted, all three are red. The T3 token reads "magpie", and each of the 11 writers shows its old key. They are green with the fix. - go test -tags nogui ./internal/agent/ is green. - gofmt -l and go vet are clean on darwin, linux and windows. Not tried with a real T3 Code, Air, OpenChamber or a real pinned MAGPIE_ADDR on a second machine.
66 lines
2.3 KiB
Go
66 lines
2.3 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)
|
|
}
|
|
|
|
// hereKey is keyAt for the gateway's own address, gateway.URL(): an agent
|
|
// on this machine given that address sends the gateway's own token, unless
|
|
// MAGPIE_ADDR pins it to an address beyond loopback, which the gateway
|
|
// takes only a named key from (#1647, ggbdpq).
|
|
func hereKey() string { return keyAt(gateway.URL()) }
|
|
|
|
// agentKey is agentKeyAt for the gateway's own address, as hereKey.
|
|
func agentKey(agent string) string { return agentKeyAt(agent, gateway.URL()) }
|
|
|
|
// 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
|
|
}
|