plugins: the sharing key magpie made itself reaches a hook as origin magpie-key, any other gateway key stays gateway-key
A gateway hook's ctx.origin was "gateway-key" both for the sharing key
magpie makes and writes into this computer's own agents (a WSL distro
under NAT, MAGPIE_ADDR beyond loopback) and for a named key the owner
made for a friend. A plugin couldn't tell them apart: plugins PR #526
(nowledge-mem)'s "Agents using a Magpie key" switch would have put the
owner's memories into a friend's requests.
Now access.Authenticate marks the default sharing key (Identity.Default:
the key marked lan, the one LANKeyID names, or an older magpie's
settings-only key, as access.Default tells it), and gatewayOrigin
returns "magpie-key" for it. Every other key, on loopback too, stays
"gateway-key": one the owner made, a value a user brought, or a
sk-magpie- key that didn't authenticate. magpie has no "this named key
is mine" mark, so none of those is treated as the owner's.
Why a new value and not a new field: every consumer in
magpie-community/plugins (agent-office's track.mjs, powercontext, and
#526) compares origin to "local" only, and treats anything else, an
unknown value included, as not local. None compares to "gateway-key" in
code. So "magpie-key" lands in their not-local branch, where the
sharing key already was: no plugin gains or loses a request it serves.
host.js passes the context through untouched. A plugin that wants the
owner's agents checks one value.
docs/subsystems/gateway-request-hooks.md says what each value is. Not
changed: the plugins repo has no type or doc for origin; #526's own
README and switch are its author's to scope (asked on the PR).
Tests:
- TestAuthenticateTellsTheDefaultKey (access): the ConfigureLAN key, a
key named by LANKeyID only, and the legacy settings key read Default;
a made key and a user-brought value don't.
- TestAsyncGatewayOrigin (gateway, real Bun host): the sharing key from
10.0.0.1 and from loopback reaches onRequest and onResponse as
magpie-key; a friend's own-value key as gateway-key.
Run (on mote, at this tree):
- go test -timeout 45m ./...: 80 packages ok (access, gateway 760s,
plugin among them); internal/gui red only in its real-window AppKit
tests (TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt,
TestMainWindowCorners and 7 more, each killed at its deadline). Three
of them run at a fresh origin/main 48edbcb67 on mote fail the same
way: #4125 (mote's AppKit tests hang) and #2645.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet of access,
gateway and plugin.
Broken by hand: origin/main's middleware.go put back, the two magpie-key
subtests red ("request origin=gateway-key, want magpie-key"); Default
forced false in Authenticate, TestAuthenticateTellsTheDefaultKey red in
its first two subtests.
Not tried: a real plugin in the GUI app with a WSL agent sending the
sharing key; npm-published plugins outside magpie-community weren't
searched.
This commit is contained in:
@@ -49,8 +49,18 @@ input intact while excluding client environment/instruction notices from recall.
|
||||
Empty identity fields must not be interpreted as authenticated user identity.
|
||||
|
||||
`origin` is host-derived: `local` for a direct loopback request without
|
||||
forwarding headers or a named gateway key, `gateway-key` for a named key
|
||||
(including one used on loopback), otherwise `remote`. Forwarded traffic stays
|
||||
forwarding headers or a named gateway key; `magpie-key` for the sharing key
|
||||
magpie made itself (`access.Identity.Default`: the key marked `lan`, the one
|
||||
`LANKeyID` names, or an older magpie's settings-only key), which it writes
|
||||
into this computer's own agents that reach the gateway from beyond loopback;
|
||||
`gateway-key` for any other named key, on loopback too (one the owner made
|
||||
for someone else, a value a user brought, or a `sk-magpie-` key that didn't
|
||||
authenticate); otherwise `remote`. magpie has no "this named key is mine"
|
||||
mark, so every key the owner made reads `gateway-key`. A plugin that serves
|
||||
the owner's own agents beyond `local` checks `magpie-key`, never
|
||||
`gateway-key` (plugins#526). Hosts before `magpie-key` sent `gateway-key`
|
||||
for the sharing key too, so a plugin that trusts only `local` sees both as
|
||||
not local, as before. Forwarded traffic stays
|
||||
remote even with `MAGPIE_TRUST_PROXY=1`. No key, key name, peer address or
|
||||
client-supplied origin claim is exposed. Missing origin on an older host means
|
||||
unknown. This classification is not a user identity; plugins that export
|
||||
|
||||
@@ -152,6 +152,11 @@ type Identity struct {
|
||||
Accounts []string
|
||||
Quotas bool
|
||||
ReadOnly bool
|
||||
// Default: the key is the sharing key magpie made itself (Default),
|
||||
// the one it writes into this computer's own agents that reach the
|
||||
// gateway from beyond loopback, never one the user made for someone
|
||||
// else.
|
||||
Default bool
|
||||
}
|
||||
type contextKey struct{}
|
||||
|
||||
@@ -520,7 +525,10 @@ func Authenticate(secret string) (Identity, bool) {
|
||||
if k.Off {
|
||||
return Identity{}, false
|
||||
}
|
||||
return Identity{KeyID: k.ID, KeyName: k.Name, Limit: k.Limit, More: k.More, Models: HeldModels(k.Models, k.Accounts), Accounts: k.Accounts, Quotas: k.Quotas, ReadOnly: k.ReadOnly}, true
|
||||
// the default key as Default tells it: marked LAN, or the id
|
||||
// the settings name (settings read only for an unmarked key)
|
||||
def := k.LAN || (settings.Load().LANKeyID == k.ID && k.ID != "")
|
||||
return Identity{KeyID: k.ID, KeyName: k.Name, Limit: k.Limit, More: k.More, Models: HeldModels(k.Models, k.Accounts), Accounts: k.Accounts, Quotas: k.Quotas, ReadOnly: k.ReadOnly, Default: def}, true
|
||||
}
|
||||
}
|
||||
s := settings.Load()
|
||||
@@ -528,7 +536,7 @@ func Authenticate(secret string) (Identity, bool) {
|
||||
// fall back after either durable marker exists, or to a revoked mirror.
|
||||
if s.LAN && s.LANKeyID == "" && s.LANKey != "" && !strings.HasPrefix(s.LANKey, revokedLANPrefix) && !slices.ContainsFunc(keys, func(k Key) bool { return k.LAN }) && subtle.ConstantTimeCompare([]byte(secret), []byte(s.LANKey)) == 1 {
|
||||
k := legacyLANKey(s.LANKey)
|
||||
return Identity{KeyID: k.ID, KeyName: k.Name}, true
|
||||
return Identity{KeyID: k.ID, KeyName: k.Name, Default: true}, true
|
||||
}
|
||||
return Identity{}, false
|
||||
}
|
||||
|
||||
@@ -350,3 +350,61 @@ func TestOwnKeyValue(t *testing.T) {
|
||||
t.Fatal("a short key shows its end", keys[1].Masked, short)
|
||||
}
|
||||
}
|
||||
|
||||
// Authenticate tells the sharing key magpie made itself (the one it writes
|
||||
// into this computer's own agents) from every other gateway key, so a
|
||||
// plugin's ctx.origin can tell the owner's agents from a friend's key
|
||||
// (plugins#526): marked LAN, named by LANKeyID only, or the older
|
||||
// settings-only key the fallback takes.
|
||||
func TestAuthenticateTellsTheDefaultKey(t *testing.T) {
|
||||
t.Run("made by ConfigureLAN", func(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
friend, err := Update("add-key", Change{Name: "Friend"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
brought, err := Update("add-key", Change{Name: "Brought", Secret: "a-much-longer-key-of-my-own"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ConfigureLAN(true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if who, ok := Authenticate(LANSecret()); !ok || !who.Default {
|
||||
t.Fatal("the default sharing key isn't Default", who, ok)
|
||||
}
|
||||
for _, k := range []string{friend, brought} {
|
||||
if who, ok := Authenticate(k); !ok || who.Default {
|
||||
t.Fatal("a named key reads as the default one", who, ok)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("named by LANKeyID only", func(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
s := settings.Load()
|
||||
s.LANKeyID = "lan"
|
||||
if err := settings.Save(s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := save([]Key{{ID: "lan", Name: "Desk", Secret: Prefix + "lan"}, {ID: "other", Name: "Other", Secret: Prefix + "other"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if who, ok := Authenticate(Prefix + "lan"); !ok || !who.Default {
|
||||
t.Fatal(who, ok)
|
||||
}
|
||||
if who, ok := Authenticate(Prefix + "other"); !ok || who.Default {
|
||||
t.Fatal(who, ok)
|
||||
}
|
||||
})
|
||||
t.Run("older magpie's settings key", func(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
s := settings.Load()
|
||||
s.LAN, s.LANKey = true, "sk-magpie-legacy-lan-secret"
|
||||
if err := settings.Save(s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if who, ok := Authenticate(s.LANKey); !ok || !who.Default {
|
||||
t.Fatal(who, ok)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -288,6 +288,19 @@ func TestAsyncGatewayOrigin(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// a key of the user's own value, made for a friend, is a named key too
|
||||
friend, err := access.Update("add-key", access.Change{Name: "Friend", Secret: "a-friends-own-key-0042"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// the sharing key magpie makes and writes into this computer's agents
|
||||
if err := access.ConfigureLAN(true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mine := access.LANSecret()
|
||||
if mine == "" || mine == key {
|
||||
t.Fatalf("no default sharing key: %q", mine)
|
||||
}
|
||||
abs := filepath.Join(t.TempDir(), "origin.hooks.mjs")
|
||||
script := `export function onRequest(body,ctx){ctx.state.origin=ctx.origin;body.origin=ctx.origin;return body}
|
||||
export async function onResponse(reply,ctx){await fetch(ctx.options.url,{method:"POST",body:JSON.stringify({origin:ctx.origin,before:ctx.state.origin}),signal:ctx.signal})}`
|
||||
@@ -314,6 +327,9 @@ export async function onResponse(reply,ctx){await fetch(ctx.options.url,{method:
|
||||
{"named-loopback", "127.0.0.1:1234", "", key, "0", "gateway-key"},
|
||||
{"remote", "10.0.0.1:1234", "", key, "0", "gateway-key"},
|
||||
{"proxy", "127.0.0.1:1234", "X-Forwarded-For", key, "0", "gateway-key"},
|
||||
{"friend-own-value", "10.0.0.1:1234", "", friend, "0", "gateway-key"},
|
||||
{"magpie-key-remote", "10.0.0.1:1234", "", mine, "0", "magpie-key"},
|
||||
{"magpie-key-loopback", "127.0.0.1:1234", "", mine, "0", "magpie-key"},
|
||||
{"trusted-proxy", "127.0.0.1:1234", "Forwarded", "", "1", "remote"},
|
||||
{"trusted-tunnel", "127.0.0.1:1234", "Tailscale-User-Login", "", "1", "remote"},
|
||||
{"unknown-peer", "malformed", "", "", "0", "remote"},
|
||||
|
||||
@@ -156,8 +156,17 @@ func gatewayQuery(req *Request) string {
|
||||
// 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, which it writes into this computer's
|
||||
// own agents that reach the gateway from beyond loopback, is "magpie-key";
|
||||
// any other gateway key — one the owner made for someone else, a value a
|
||||
// user brought, or one that didn't authenticate — stays "gateway-key", so
|
||||
// a plugin can 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 access.Caller(r.Context()).KeyID != "" || managedKey(r) {
|
||||
if who := access.Caller(r.Context()); who.KeyID != "" && who.Default {
|
||||
return "magpie-key"
|
||||
} else if who.KeyID != "" || managedKey(r) {
|
||||
return "gateway-key"
|
||||
}
|
||||
if local(r) && !slices.ContainsFunc(forwardHeaders, func(h string) bool { return r.Header.Get(h) != "" }) {
|
||||
|
||||
@@ -80,7 +80,7 @@ func GatewayOnly(target string) bool {
|
||||
// GatewayContext contains only the request identity and text, never its
|
||||
// authorization headers. Empty fields mean the agent supplied no identity.
|
||||
type GatewayContext struct {
|
||||
Origin string `json:"origin"` // local, remote, or gateway-key; empty means unknown
|
||||
Origin string `json:"origin"` // local, remote, magpie-key or gateway-key; empty means unknown
|
||||
Protocol string `json:"protocol"`
|
||||
Model string `json:"model"`
|
||||
Stream bool `json:"stream"`
|
||||
|
||||
Reference in New Issue
Block a user