diff --git a/docs/subsystems/gateway-request-hooks.md b/docs/subsystems/gateway-request-hooks.md index d3f73e834..359e30071 100644 --- a/docs/subsystems/gateway-request-hooks.md +++ b/docs/subsystems/gateway-request-hooks.md @@ -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 diff --git a/internal/access/access.go b/internal/access/access.go index 3921f0437..40a559d77 100644 --- a/internal/access/access.go +++ b/internal/access/access.go @@ -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 } diff --git a/internal/access/access_test.go b/internal/access/access_test.go index 1170841d7..0e941e04f 100644 --- a/internal/access/access_test.go +++ b/internal/access/access_test.go @@ -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) + } + }) +} diff --git a/internal/gateway/gateway_hook_test.go b/internal/gateway/gateway_hook_test.go index d1fc4122e..f91e48b8b 100644 --- a/internal/gateway/gateway_hook_test.go +++ b/internal/gateway/gateway_hook_test.go @@ -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"}, diff --git a/internal/gateway/middleware.go b/internal/gateway/middleware.go index 76714ab7b..110c1d722 100644 --- a/internal/gateway/middleware.go +++ b/internal/gateway/middleware.go @@ -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) != "" }) { diff --git a/internal/plugin/gateway.go b/internal/plugin/gateway.go index f02e45ef7..4bdc0826e 100644 --- a/internal/plugin/gateway.go +++ b/internal/plugin/gateway.go @@ -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"`