isaac_bai on Discord: a gateway key's value is random, and only rotate changes it, to another random one; they want to set one easy to pass on. Adding a key could take an own value since 6b6113aa; an existing key couldn't. - access: rotate-key takes Change.Secret. Given, it is checked as an added key's own value (ownSecret: 8-256 printable ASCII, no space, not sk-magpie-, no other key's value; its own current value is allowed); empty, magpie makes one as before. The key keeps its id, name, limits, models and accounts, and Named counts a request from this computer with it as that key. - ownSecret now refuses a value that is one of the user's provider keys (a provider's Key or any of its Keys, constant-time), for add-key too: a client sending it would spend that provider directly. - CLI: `magpie gateway-key rotate <id> --key <value|->`; docs/reference.md. - GUI (plugins/gateway/keys.js, outside the core budget): the Rotate key dialog has a hidden "Your own key (optional)" field; Enter rotates, Escape cancels; an own value says "Gateway key changed. Clients that send this key reach magpie now." Strings in zh, zh-TW, ja, de. Tests: TestRotateToOwnValue (bad values refused, old value still works after a refusal, trimmed own value authenticates as the same key, is Named, listed masked; empty makes a new key; add-key refuses a provider key). TestRotatedOwnValueKeepsTheKeysLimit: after rotating to an own value the old one is 401 from another computer, the own one 200 from another computer and from 127.0.0.1, and a 700-token day limit stops the third request from either (429), every record under the key. gateway-key-own-rotate.test.cjs: chromium and webkit, en/zh/zh-TW/ja/de, 1000 and 400px. Run (mote): at the pushed head: TestCoreScriptBudget ok, ./internal/access ok, the own-key/caller-key gateway tests ok. Atf37742e14with this change (no file the rebase touched): gofmt -l clean; go vet GOOS=linux and windows (./internal/access ./internal/gateway, cli with -tags nogui on linux) ok; ./internal/cli red on TestEveryRouteHasACommandOrIsNamed only, /api/context/reply (#5026,0872bc2a0, the same at main); GUI: gateway-key-own-rotate 20/20, every gateway-key-*, redact-*, masking-*, privacy-* file and i18n-keys pass. Broken by hand: the rotate own-value path off made TestRotateToOwnValue and TestRotatedOwnValueKeepsTheKeysLimit red (401 from another computer); the provider-key check off made TestRotateToOwnValue red ("accepted upstream-first-key"). Not run: the whole GUI suite (two runs lost: one to another run of this tree, one to mote rebooting mid-run); it runs after the push. Not tried: a real client on another computer with the own value, the real app.
131 lines
4.4 KiB
Go
131 lines
4.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/budget"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// A key given its own value — one clients already send, from another
|
|
// gateway (love1sbug on X) — is that key from another computer and from
|
|
// this one, as a sk-magpie- key is: counted as it, and never sent on.
|
|
func TestOwnKeyValueIsTheKey(t *testing.T) {
|
|
fresh(t)
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Bearer upstream-secret" {
|
|
t.Error("caller credential reached provider:", r.Header.Get("Authorization"))
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
|
|
}))
|
|
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)
|
|
}
|
|
s := settings.Load()
|
|
s.LAN = true
|
|
if err := settings.Save(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const own = "cpa-family-key-0042"
|
|
if _, err := access.Update("add-key", access.Change{Name: "Family", Secret: own}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := lanGuard(New().Handler()) // as the real server has it
|
|
call := func(from, key string) int {
|
|
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(chatReq))
|
|
r.RemoteAddr = from
|
|
r.Header.Set("Authorization", "Bearer "+key)
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w.Code
|
|
}
|
|
if code := call("192.0.2.7:4000", own); code != 200 {
|
|
t.Fatal("from another computer", code)
|
|
}
|
|
if code := call("127.0.0.1:4000", own); code != 200 {
|
|
t.Fatal("from this computer", code)
|
|
}
|
|
if code := call("192.0.2.7:4000", own+"x"); code != http.StatusUnauthorized {
|
|
t.Fatal("a wrong key", code)
|
|
}
|
|
recs := usage.Load(time.Time{})
|
|
if len(recs) != 2 {
|
|
t.Fatalf("records: %+v", recs)
|
|
}
|
|
for _, rec := range recs {
|
|
if rec.CallerKeyName != "Family" {
|
|
t.Fatalf("not counted as the key: %+v", rec)
|
|
}
|
|
}
|
|
keys, err := access.List()
|
|
if err != nil || len(keys) != 1 {
|
|
t.Fatal(keys, err)
|
|
}
|
|
if _, err := access.Update("off-key", access.Change{Key: keys[0].ID}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if code := call("192.0.2.7:4000", own); code != http.StatusUnauthorized {
|
|
t.Fatal("a disabled key from another computer", code)
|
|
}
|
|
}
|
|
|
|
// A key magpie made, rotated to a value of the user's own (isaac_bai on
|
|
// Discord), is still that key: from another computer and from this one
|
|
// it is let in, counted as it and held to its limit, and its old value
|
|
// is refused.
|
|
func TestRotatedOwnValueKeepsTheKeysLimit(t *testing.T) {
|
|
fresh(t)
|
|
budget.Forget()
|
|
pinLimitClock(t)
|
|
calls := limitedUpstream(t, 0)
|
|
keys, secrets := newCaller(t, "Family")
|
|
setLimit(t, keys[0].ID, &access.Limit{Period: "day", Tokens: 700})
|
|
const own = "family-easy-to-share"
|
|
if _, err := access.Update("rotate-key", access.Change{Key: keys[0].ID, Secret: own}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := lanGuard(New().Handler())
|
|
call := func(from, key string) int {
|
|
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(chatReq))
|
|
r.RemoteAddr = from
|
|
r.Header.Set("Authorization", "Bearer "+key)
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w.Code
|
|
}
|
|
if code := call("192.0.2.7:4000", secrets[0]); code != http.StatusUnauthorized {
|
|
t.Fatal("the value it had", code)
|
|
}
|
|
if code := call("192.0.2.7:4000", own); code != 200 {
|
|
t.Fatal("from another computer", code)
|
|
}
|
|
if code := call("127.0.0.1:4000", own); code != 200 {
|
|
t.Fatal("from this computer", code)
|
|
}
|
|
// 700 tokens, 350 a call: the third is refused, from either computer
|
|
if code := call("192.0.2.7:4000", own); code != http.StatusTooManyRequests {
|
|
t.Fatal("past its limit from another computer", code)
|
|
}
|
|
if code := call("127.0.0.1:4000", own); code != http.StatusTooManyRequests {
|
|
t.Fatal("past its limit from this computer", code)
|
|
}
|
|
if calls.Load() != 2 {
|
|
t.Fatal("calls reached the provider:", calls.Load())
|
|
}
|
|
for _, rec := range usage.Load(time.Time{}) {
|
|
if rec.CallerKeyID != keys[0].ID {
|
|
t.Fatalf("not counted as the key: %+v", rec)
|
|
}
|
|
}
|
|
}
|