mcpKeyGuard (d55881730) held a key given some tools of a relayed server
(/mcp/<name>, /mcp/run/<name>) by decoding the body with Go's
encoding/json, which matches keys in any case and keeps a key's last
value, then sent the server the original bytes, which it reads keys
as written. So {"method":"tools/call","params":{"name":"write",
"NAME":"read"}} was checked as read and ran write on the server; a
METHOD, Params or paramſ key, or a key given twice, did the same.
strictMCPBody now reads each message (and a batch) into maps: a key
that is one of the envelope's (jsonrpc, id, method, params, result,
error) or params' (name, arguments) in another case is a 400, a
tools/call whose params aren't an object or whose name isn't a string
is a 400, and the server is sent the body written again from what was
checked, so a key given twice reaches it once. magpie's own servers
and the hub act on what they parsed, so they were not open to this.
Run on mote at this tree (base 416305451):
- go vet ./internal/gateway, also GOOS=windows and GOOS=linux: ok
- go test -count=1 -timeout 45m ./internal/gateway ./internal/access:
ok (gateway 730.8s)
- gofmt -l internal/gateway: clean
Broken by hand: checking with the old case-insensitive decode and
forwarding the original body turns TestKeyMCPRelayReadsOneWay red
(the server got the NAME body and ran write).
TestKeyMCPRun's check of the forwarded body now expects the re-encoded
form (keys sorted).
Not tried: a real remote MCP server signed in through /mcp/<name>;
the relays are covered with mcpauthtest's fake and a RunMCP stub.