Files
magpie/internal/access/memory_key_test.go
T
yetone 4e24994a6e gateway: a key let use Magpie Memory reaches it from another computer
Niapyaya on X reaches their own MCP servers through the MCP hub from a
remote machine, but not magpie-memory: memory answered the agents on
this computer alone, on /mcp/magpie/memory (547585cd9) and in the hub
(HereOnly), whatever key came. That was on purpose: a friend's key
could read every memory or plant a global rule the digest writes into
every agent's instructions here.

So it stays off for every key, and the user can let one key of their
own use it:

- access.Key.Memory, set by "memory-key" (`magpie gateway-key memory
  <id> on|off`, or the key's ... > Advanced > Magpie Memory). As Admin,
  only a key the user made can have it (memoryOf): never the sharing
  key, a read-only or Browser key, nor an issued or expiring one.
  Read-only takes it away; the management API can't set it.
- memoryMCP lets a caller from elsewhere in only while magpie is
  shared, by such a key, and never from a page by hubFromPage (Origin,
  Sec-Fetch-Site, a Mozilla/ agent; Node's fetch sends Sec-Fetch-Mode,
  #5522). This computer's check is unchanged.
- The hub gives magpie-memory (HubServer.Memory, from isMemory) to an
  Away visitor only by such a key (HubVisitor.Memory), listed and run,
  in the gateway and again in the library's Call.
- Advanced in the key's menu now opens Management API and Magpie
  Memory, each its own pick; a key with it says so in its row. Strings
  in zh, zh-TW, ja, de. Docs: memory.md, mcp-router.md, and the site's
  remote page (en, zh, ja) with its index rebuilt.

Image already answers a key directly (/mcp/magpie/image, held to the
key); sessions stays this computer's. There is no skills MCP server.

Tests: TestMemoryMCPToAKeyGivenIt, TestHubGivesMemoryToAKeyLetUseIt,
TestMemoryKey, the hubServerOf Memory column, and gateway-key-admin's
GUI file now goes through Advanced > Management API and covers Memory.

Run (mote unless said; base d01e0a465, rebased onto c4bae0724, which
only touches the Settings keep guard):
- Red on the old code (mica): TestMemoryMCPToAKeyGivenIt (403 to the
  key) and TestHubGivesMemoryToAKeyLetUseIt (hub listed [docs] only).
- go test -count=1 -timeout 45m ./internal/access ./internal/gateway
  ./internal/library ./internal/cli ./internal/gui: access, library ok.
  Reds, each the same at a clean origin/main c4bae0724 and filed:
  gateway TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (#7117) and
  the doneHold panic log (#6505); cli TestHealthcheck…/TestUpdate…
  WithoutMagpieAddr, TestUpdateFindsAMagpieThatWroteNoRecord (#6471,
  #6223); gui TestSettingsSaveKeepsWhatItDoesNotSend (fixed by
  c4bae0724).
- GUI, both engines, every language, at this head: 64 files (every
  caller-keys / gateway-plugin file and i18n-*): 1008 of 1024 pass;
  api-key-theme (#6444) and connect-address-kept (#6755) fail the same
  at clean origin/main c4bae0724.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... ok; builds for all three OSes ok.
- Broken by hand: drop !hubFromPage -> the Sec-Fetch-Site case gets 200,
  red; drop the key's Memory check -> another key gets 200, red. Drop
  sharedWith -> still green: serveMCP's share guard refuses first, so
  memoryReaches' check is a second line, not the only one.

Not tried: a real remote agent against a real shared magpie (tests use
httptest with 192.0.2.7 as the remote address); the full suite runs
after the push.
2026-11-19 02:31:18 +08:00

88 lines
2.7 KiB
Go

package access
import (
"strings"
"testing"
"time"
)
// Niapyaya on X: a key can be let use Magpie Memory from another
// computer, but only a key the user made for it, and only by the user:
// never the sharing key, a read-only or Browser key, nor an issued one.
func TestMemoryKey(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("HOME", t.TempDir())
plainID, plain := addKey(t, "plain")
if who, ok := Authenticate(plain); !ok || who.Memory {
t.Fatal("a new key has Memory")
}
for _, on := range []bool{true, false, true} {
if _, err := Update("memory-key", Change{Key: plainID, Memory: on}); err != nil {
t.Fatal(on, err)
}
if who, _ := Authenticate(plain); who.Memory != on {
t.Errorf("memory %v: Memory %v", on, who.Memory)
}
}
// read-only takes it away, and is refused it
if _, err := Update("access-key", Change{Key: plainID, ReadOnly: true}); err != nil {
t.Fatal(err)
}
if who, _ := Authenticate(plain); who.Memory {
t.Error("a key made read-only kept Memory")
}
if _, err := Update("memory-key", Change{Key: plainID, Memory: true}); err == nil {
t.Error("a read-only key was given Memory")
}
// the extension's key
if _, err := Update("add-key", Change{Name: "ext", Browser: true}); err != nil {
t.Fatal(err)
}
keys, _ := List()
if _, err := Update("memory-key", Change{Key: keys[len(keys)-1].ID, Memory: true}); err == nil {
t.Error("the extension's key was given Memory")
}
// the sharing key, given to every agent that reaches magpie
if err := ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
keys, _ = List()
for _, k := range keys {
if k.LAN {
if _, err := Update("memory-key", Change{Key: k.ID, Memory: true}); err == nil || !strings.Contains(err.Error(), "sharing key") {
t.Errorf("the sharing key was given Memory: %v", err)
}
}
}
if who, _ := Authenticate(LANSecret()); who.Memory {
t.Error("the sharing key has Memory")
}
// an issued key, even when the file says otherwise
issuerID, _ := addKey(t, "issuer")
if _, err := Update("memory-key", Change{Key: issuerID, Memory: true}); err != nil {
t.Fatal(err)
}
if _, err := Update("issuer-key", Change{Key: issuerID, Issues: true}); err != nil {
t.Fatal(err)
}
k, err := IssueKey(Issue{Name: "sandbox", TTL: time.Hour, Issuer: issuerID})
if err != nil {
t.Fatal(err)
}
if who, ok := Authenticate(k.Key); !ok || who.Memory {
t.Error("an issued key has Memory")
}
keys, _ = List()
for _, key := range keys {
if key.Issuer != "" {
if _, err := Update("memory-key", Change{Key: key.ID, Memory: true}); err == nil || !strings.Contains(err.Error(), "issued") {
t.Errorf("an issued key was given Memory: %v", err)
}
}
}
}