Files
magpie/internal/gateway/key_accounts_only_test.go
T
yetone 05a6087e1f access: a key with accounts and no model may use only its accounts' providers' models
Linka on Discord (#feedback): in a gateway key's "Models this key can
use", ticking only an account and no model let the key list and use
every model of every provider.

Cause: Key.Models empty meant "every model", and Key.Accounts (#905)
held only the providers it names, so every other provider stayed open.

Change: access.HeldModels gives a key with accounts and no models the
models "<provider>/*" of each account's provider. Authenticate builds
the Identity with it, so every route's existing model gate narrows:
/v1/models, /v1beta/models, chat, responses, messages, Gemini,
count_tokens, embeddings, images, the Codex backend path, System One,
groups and fallbacks. A key that lists models is unchanged (#905: its
accounts still hold only the providers they name); a key with neither
is unchanged. The CLI's MODELS column and `gateway-key models` say so,
and the GUI editor's hint, the "not used" notes on providers no model
or account reaches, and the save status follow (en, zh, zh-TW, ja, de).

Tests:
- TestGatewayKeyAccountsOnlyHoldTheModels: each route above is 403 for
  the accounts-only key and no upstream call reaches the other
  provider, the decider or the image provider; its own provider, a
  group of it and a key that also lists models work. Red on the old
  code on every route (list showed spare/m2, chat/count_tokens/
  embeddings/systemone 200).
- TestHeldModels (access).
- gateway-key-accounts.test.cjs: hint, notes and status; 4/4 red on
  the old assets, green on the new.
- TestGatewayKeyAccountsListModels / SystemOne / OfANamedProvider and
  TestHeldKeyToldItsOwnQuotas now encode the new rule (the held key is
  told only its own provider's quotas).

Run on mote: gofmt -l clean; go vet darwin, linux -tags nogui, windows
on access, cli, gateway, gui ok; go test -timeout 45m ./internal/access
./internal/cli ./internal/gui ./internal/gateway: ok except gui's
TestMainWindowCorners/macOS_26_SDK (#3949, not this change) and
gateway's TestHeldKeyToldItsOwnQuotas, updated and rerun ok. After the
rebase on 1f03d7546 the key tests ran again ok. GUI: gateway-key*,
i18n-keys, gui-de, gui-ja, gui-zh-tw in Chromium and WebKit: 131 pass.

Not tried: a plugin-served provider (same model gate), a real
account, Windows and Linux test runs (vet only).
2026-10-30 01:25:57 +08:00

155 lines
7.6 KiB
Go

package gateway
import (
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
)
// A gateway key that names an account and no model (Linka on Discord:
// "Models this key may use" with only an account ticked) may use only
// that account's provider's models, on every route a model reaches it
// by: the model lists, a chat on each API, counting tokens, embeddings,
// pictures, Codex's backend, System One, a group with another
// provider's model in it and a fallback to one. Before, the account held
// only its own provider, and the key could list and use every model of
// every other provider.
func TestGatewayKeyAccountsOnlyHoldTheModels(t *testing.T) {
fresh(t)
plan := &fake{t: t, ctype: "application/json", reply: `{"id":"from-plan","choices":[]}`}
spare := &fake{t: t, ctype: "application/json", reply: `{"id":"from-spare","choices":[]}`}
twoProviders(t, plan, spare)
var decided int
jev := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
decided++
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"answers":{"intent":{"choice":"bug"}},"usage":{"input_tokens":10,"output_tokens":2}}`)
}))
t.Cleanup(jev.Close)
if err := provider.Save(provider.Provider{ID: "jev", Name: "Jev", Key: "k", Decide: jev.URL + "/v1"}); err != nil {
t.Fatal(err)
}
var drew int
art := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
drew++
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"created":1,"data":[{"b64_json":"`+base64.StdEncoding.EncodeToString(pngBytes)+`"}]}`)
}))
t.Cleanup(art.Close)
if err := provider.Save(provider.Provider{ID: "art", Name: "Art", Chat: art.URL + "/v1", Key: "kA", Models: []string{"gpt-image-1"}}); err != nil {
t.Fatal(err)
}
for _, g := range []provider.Group{
{Name: "Both", Members: []string{"plan/m1", "spare/m2"}, Routing: provider.Ordered},
{Name: "Solo", Members: []string{"plan/m1"}, Routing: provider.Ordered},
} {
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
}
keys, secrets := newCaller(t, "Held", "Free")
if _, err := access.Update("accounts-key", access.Change{Key: keys[0].ID, Accounts: []string{"plan/" + provider.KeyID("k")}}); err != nil {
t.Fatal(err)
}
if ks, _ := access.List(); len(ks[len(ks)-2].Models) != 0 {
t.Fatalf("the key's own models were written: %+v", ks[len(ks)-2])
}
h := New().Handler()
do := func(secret, method, path, body string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, path, strings.NewReader(body))
r.Header.Set("Authorization", "Bearer "+secret)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
refused := func(what string, w *httptest.ResponseRecorder) {
t.Helper()
if w.Code != 403 || !strings.Contains(w.Body.String(), "Held") {
t.Errorf("%s: %d %s", what, w.Code, w.Body.String())
}
}
// the lists: the account's provider's models and a group of them only
var l struct{ Data []struct{ ID string } }
if w := do(secrets[0], "GET", "/v1/models", ""); json.Unmarshal(w.Body.Bytes(), &l) != nil {
t.Fatal(w.Body.String())
}
var ids []string
for _, m := range l.Data {
ids = append(ids, m.ID)
}
if !slices.Contains(ids, "plan/m1") || !slices.Contains(ids, "group/solo") || slices.Contains(ids, "spare/m2") || slices.Contains(ids, "group/both") || slices.Contains(ids, "jev/jev-latest") {
t.Errorf("/v1/models lists %v", ids)
}
if w := do(secrets[0], "GET", "/v1beta/models", ""); w.Code != 200 || !strings.Contains(w.Body.String(), "m1") || strings.Contains(w.Body.String(), "m2") {
t.Errorf("gemini list %d %s", w.Code, w.Body.String())
}
if w := do(secrets[0], "GET", "/v1/models/spare/m2", ""); w.Code == 200 {
t.Error("a model of another provider was found", w.Body.String())
}
// a request for another provider's model, on every API
for _, model := range []string{"spare/m2", "m2", "group/both"} {
refused("chat "+model, do(secrets[0], "POST", "/v1/chat/completions", `{"model":"`+model+`","messages":[{"role":"user","content":"hi"}]}`))
}
refused("responses", do(secrets[0], "POST", "/v1/responses", `{"model":"spare/m2","input":"hi"}`))
refused("messages", do(secrets[0], "POST", "/v1/messages", `{"model":"spare/m2","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`))
refused("gemini", do(secrets[0], "POST", "/v1beta/models/spare/m2:generateContent", `{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`))
refused("count_tokens", do(secrets[0], "POST", "/v1/messages/count_tokens", `{"model":"spare/m2","messages":[{"role":"user","content":"hi"}]}`))
refused("countTokens", do(secrets[0], "POST", "/v1beta/models/spare/m2:countTokens", `{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`))
refused("embeddings", do(secrets[0], "POST", "/v1/embeddings", `{"model":"spare/m2","input":"hi"}`))
refused("codex backend", do(secrets[0], "POST", CodexPath+"/responses", `{"model":"spare/m2","input":"hi","stream":true}`))
refused("system one", do(secrets[0], "POST", "/v1/systemone", `{"model":"jev/jev-latest","state":{"message":"hi"},"questions":{"intent":{"type":"choice"}}}`))
refused("images", do(secrets[0], "POST", "/v1/images/generations", `{"model":"art/gpt-image-1","prompt":"a magpie"}`))
if spare.calls != 0 || decided != 0 || drew != 0 {
t.Fatal("a held request reached another provider", spare.calls, decided, drew)
}
if w := do(secrets[1], "POST", "/v1/images/generations", `{"model":"art/gpt-image-1","prompt":"a magpie"}`); w.Code != 200 || drew != 1 {
t.Errorf("a free key's drawing: %d %s", w.Code, w.Body.String())
}
// its own provider's models are served, a group of them too
for _, model := range []string{"plan/m1", "m1", "group/solo"} {
if w := do(secrets[0], "POST", "/v1/chat/completions", `{"model":"`+model+`","messages":[{"role":"user","content":"hi"}]}`); w.Code != 200 || !strings.Contains(w.Body.String(), "from-plan") {
t.Errorf("%s: %d %s", model, w.Code, w.Body.String())
}
}
// a fallback to another provider's model is not the key's
plan.code, plan.reply = 429, `{"error":{"message":"slow down"}}`
if w := do(secrets[0], "POST", "/v1/chat/completions", chatReq); w.Code == 200 || spare.calls != 0 {
t.Errorf("a held key fell back to another provider: %d %s", w.Code, w.Body.String())
}
// a free key is served everywhere, the fallback too
if w := do(secrets[1], "POST", "/v1/chat/completions", chatReq); w.Code != 200 || !strings.Contains(w.Body.String(), "from-spare") {
t.Errorf("a free key's fallback: %d %s", w.Code, w.Body.String())
}
plan.code, plan.reply = 0, `{"id":"from-plan","choices":[]}`
// a key that lists models keeps them: its account holds only the
// provider it names, as before (#905)
if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"spare/*"}}); err != nil {
t.Fatal(err)
}
if w := do(secrets[0], "POST", "/v1/chat/completions", `{"model":"spare/m2","messages":[{"role":"user","content":"hi"}]}`); w.Code != 200 || !strings.Contains(w.Body.String(), "from-spare") {
t.Errorf("a listed model: %d %s", w.Code, w.Body.String())
}
// and one let off its accounts uses every model again
if _, err := access.Update("models-key", access.Change{Key: keys[0].ID}); err != nil {
t.Fatal(err)
}
if _, err := access.Update("accounts-key", access.Change{Key: keys[0].ID}); err != nil {
t.Fatal(err)
}
if w := do(secrets[0], "POST", "/v1/chat/completions", `{"model":"spare/m2","messages":[{"role":"user","content":"hi"}]}`); w.Code != 200 {
t.Errorf("a key let off: %d %s", w.Code, w.Body.String())
}
}