321sssrt-bit shared magpie on the LAN with a gateway key held to a budget, and that key still read every account's quota and balance. A held key (heldKey: a budget, models or accounts) is someone else's to use, so it now gets 403 with code gateway_key_held (refuseHeld) on /v1/magpie/quotas, quotas/history, quotas/cards, POST quotas/refresh and /v1/magpie/concurrency. That holds from loopback as well as the LAN, the same as usageSummary already did. /v1/magpie/route tells it only the routes of its own requests. Route.callerKey is set by trace.beginFor at all five begin sites, so a route begun without it fails closed. It still reads its own limit at /v1/magpie/limit. Unrestricted keys and the local owner are unchanged. Client side (remote magpie): a 403 gateway_key_held drops the cards kept from before and shows only the key's own card, instead of "isn't shared". The cache is now keyed by provider and key (remoteCardsID), so a changed key never shows what the old one was told, and keepRemoteCards keeps a read only over one asked before it, so a slow pre-held answer can't bring cards back. Decided against letting an account-held key see its own accounts' cards: that would still show the host's account email and balance to the key's holder. Run: - New TestHeldKeyToldNoAccounts covers keys held by budget, models and accounts plus one held to nothing, from 127.0.0.1 and 192.168.1.9. Every route goes red with the refuseHeld calls or the route filter removed by hand. - New TestRemoteMagpieHeldKeyShowsOnlyItsOwnCard goes red with the errKeyHeld mapping removed. TestRemoteMagpieCardsKeptByKey goes red with the cache keyed by p.ID alone. - gofmt -l and go vet (darwin, linux -tags nogui, windows) are clean. - go test -tags nogui ./internal/gateway ./internal/provider -count=1 passes in a sandbox HOME. Not tried: two real machines over the LAN; the tests drive the handler with LAN remote addresses. An older client shows "isn't shared" (or its cached cards until they expire) against an updated host until it updates too.
116 lines
4.1 KiB
Go
116 lines
4.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// #1981 (321sssrt-bit): magpie shared on the local network with a gateway
|
|
// key held to a budget, models or accounts, and that key still read every
|
|
// account's quota and balance from /v1/magpie/quotas. A held key is
|
|
// someone else's to use: it is told none of this magpie's accounts, from
|
|
// this computer or another, only its own limit and the routes of its own
|
|
// requests. A key held to nothing is told what it was told before.
|
|
func TestHeldKeyToldNoAccounts(t *testing.T) {
|
|
fresh(t)
|
|
t.Setenv("MAGPIE_ADDR", "")
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":30,"completion_tokens":5}}`)
|
|
}))
|
|
defer up.Close()
|
|
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, secrets := newCaller(t, "Owner", "Budget", "Models", "Accounts")
|
|
if _, err := access.Update("limit-key", access.Change{Key: keys[1].ID, Limit: &access.Limit{Period: "day", Tokens: 1_000_000}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := access.Update("models-key", access.Change{Key: keys[2].ID, Models: []string{"plan/*"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := provider.Find("plan")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
as := p.AccountIDs()
|
|
if len(as) == 0 {
|
|
t.Fatal("no account to hold a key to")
|
|
}
|
|
if _, err := access.Update("accounts-key", access.Change{Key: keys[3].ID, Accounts: []string{"plan/" + as[0].ID}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := lanGuard(New().Handler())
|
|
do := func(method, path, from, secret string, hdr ...string) (int, string) {
|
|
var body io.Reader
|
|
if method == "POST" && strings.HasPrefix(path, "/v1/chat") {
|
|
body = strings.NewReader(chatReq)
|
|
}
|
|
r := httptest.NewRequest(method, path, body)
|
|
r.RemoteAddr = from
|
|
r.Header.Set("Authorization", "Bearer "+secret)
|
|
for i := 0; i+1 < len(hdr); i += 2 {
|
|
r.Header.Set(hdr[i], hdr[i+1])
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w.Code, w.Body.String()
|
|
}
|
|
// each key makes a request in a session of its own
|
|
for i, s := range secrets {
|
|
if c, b := do("POST", "/v1/chat/completions", "192.168.1.9:5000", s, SessionHeader, "sess-"+keys[i].Name); c != 200 {
|
|
t.Fatal(keys[i].Name, c, b)
|
|
}
|
|
}
|
|
told := []struct{ method, path string }{
|
|
{"GET", "/v1/magpie/quotas"},
|
|
{"GET", "/v1/magpie/quotas/history"},
|
|
{"GET", provider.RemoteCardsPath},
|
|
{"POST", provider.RemoteRefreshPath},
|
|
{"GET", "/v1/magpie/concurrency"},
|
|
}
|
|
route := func(from, secret, session string) any {
|
|
t.Helper()
|
|
c, b := do("GET", "/v1/magpie/route?session="+session, from, secret)
|
|
var out map[string]any
|
|
if c != 200 || json.Unmarshal([]byte(b), &out) != nil {
|
|
t.Fatal("route:", c, b)
|
|
}
|
|
return out["route"]
|
|
}
|
|
for _, from := range []string{"127.0.0.1:5000", "192.168.1.9:5000"} {
|
|
for i, s := range secrets {
|
|
name := keys[i].Name
|
|
for _, x := range told {
|
|
c, b := do(x.method, x.path, from, s)
|
|
if i == 0 {
|
|
if c != 200 {
|
|
t.Errorf("%s from %s, a key held to nothing: %s %s: %d %s", name, from, x.method, x.path, c, b)
|
|
}
|
|
continue
|
|
}
|
|
if c != http.StatusForbidden || !strings.Contains(b, provider.KeyHeldCode) || strings.Contains(b, "upstream") {
|
|
t.Errorf("%s from %s: %s %s: %d %s", name, from, x.method, x.path, c, b)
|
|
}
|
|
}
|
|
if c, b := do("GET", "/v1/magpie/limit", from, s); c != 200 {
|
|
t.Errorf("%s from %s: its own limit: %d %s", name, from, c, b)
|
|
}
|
|
if route(from, s, "sess-"+name) == nil {
|
|
t.Errorf("%s from %s: no route for its own session", name, from)
|
|
}
|
|
other := "sess-" + keys[(i+1)%len(keys)].Name
|
|
if r := route(from, s, other); (i == 0) != (r != nil) {
|
|
t.Errorf("%s from %s: another key's session %s: %v", name, from, other, r)
|
|
}
|
|
}
|
|
}
|
|
}
|