diff --git a/internal/gateway/lan.go b/internal/gateway/lan.go index 7989657ac..255884f65 100644 --- a/internal/gateway/lan.go +++ b/internal/gateway/lan.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path/filepath" + "slices" "strings" "sync" @@ -182,7 +183,7 @@ func lanGuard(next http.Handler) http.Handler { http.Error(w, "magpie isn't shared on the local network", http.StatusForbidden) return } - if (remote && shared) || (!remote && access.Managed(callerKey(r))) { + if (remote && shared) || (!remote && managedKey(r)) { var ok bool r, ok = identifyCaller(w, r) if !ok { @@ -199,7 +200,7 @@ func lanGuard(next http.Handler) http.Handler { // callerGuard also covers embedded handlers used by the web app and tests. func callerGuard(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if access.Caller(r.Context()).KeyID == "" && access.Managed(callerKey(r)) && (local(r) || settings.Load().LAN) { + if access.Caller(r.Context()).KeyID == "" && managedKey(r) && (local(r) || settings.Load().LAN) { var ok bool r, ok = identifyCaller(w, r) if !ok { @@ -211,10 +212,15 @@ func callerGuard(next http.Handler) http.Handler { } func identifyCaller(w http.ResponseWriter, r *http.Request) (*http.Request, bool) { - key := callerKey(r) - who, ok := access.Authenticate(key) + var who access.Identity + ok := false + for _, k := range callerKeys(r) { + if who, ok = access.Authenticate(k); ok { + break + } + } if !ok && !local(r) { - msg := refusedKey(key) + msg := refusedKey(callerKey(r)) log.Printf("refused %s %s from %s: %s", r.Method, r.URL.Path, r.RemoteAddr, msg) writeError(w, provider.Chat, http.StatusUnauthorized, msg) return r, false @@ -264,17 +270,50 @@ func sharedWith(r *http.Request) bool { return ok } -// callerKey is the API key a request carries, however its client sends one. +// callerKey is the API key a request carries, however its client sends one: +// the first of callerKeys, "" for none. func callerKey(r *http.Request) string { - if a := r.Header.Get("Authorization"); a != "" { - return strings.TrimSpace(strings.TrimPrefix(a, "Bearer ")) + if ks := callerKeys(r); len(ks) > 0 { + return ks[0] } - for _, h := range []string{"x-api-key", "x-goog-api-key"} { - if v := r.Header.Get(h); v != "" { - return v + return "" +} + +// callerKeys are the API keys a request carries, in the order they are +// read: Authorization's bearer token, x-api-key, x-goog-api-key, ?key=. A +// header that carries none ("Authorization: Bearer ", sent beside an +// x-api-key by a client with no token set) is passed over, and a client +// that sends two different values — a stale or placeholder token in +// Authorization (ANTHROPIC_AUTH_TOKEN left at magpie), the key in x-api-key +// — is let in by whichever is an enabled gateway key. Loopback takes any +// token, so this mattered only from another computer, which was refused +// what this one was answered (悠悠哥 on Discord: a client's model list from +// magpie shared on a NAS failed, from magpie on 127.0.0.1 it came). +func callerKeys(r *http.Request) []string { + var out []string + add := func(v string) { + if v = strings.TrimSpace(v); v != "" && !slices.Contains(out, v) { + out = append(out, v) } } - return r.URL.Query().Get("key") + a := strings.TrimSpace(r.Header.Get("Authorization")) + if len(a) >= 7 && strings.EqualFold(a[:7], "Bearer ") { + a = a[7:] + } else if strings.EqualFold(a, "Bearer") { + a = "" + } + add(a) + for _, h := range []string{"x-api-key", "x-goog-api-key"} { + add(r.Header.Get(h)) + } + add(r.URL.Query().Get("key")) + return out +} + +// managedKey: the request carries a named gateway key's form (sk-magpie-…) +// in any of the places a key is read. +func managedKey(r *http.Request) bool { + return slices.ContainsFunc(callerKeys(r), access.Managed) } // refusedKey says why a caller's key was turned away: none came, or the diff --git a/internal/gateway/lan_test.go b/internal/gateway/lan_test.go index dbdcf0a30..b75d9f2b0 100644 --- a/internal/gateway/lan_test.go +++ b/internal/gateway/lan_test.go @@ -5,9 +5,11 @@ import ( "net/http/httptest" "os" "path/filepath" + "strings" "testing" "github.com/yetone/magpie/internal/access" + "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/settings" ) @@ -175,3 +177,67 @@ func TestInContainer(t *testing.T) { } } } + +// Another computer lists the models with the key it was given whatever +// else its client sends beside it: an empty or placeholder Authorization +// with the key in x-api-key, a stale token there, "bearer" in lower case +// (悠悠哥 on Discord: a client's model list from magpie shared on a NAS +// failed, while from magpie on 127.0.0.1, which takes any token, it came). +// The request comes as it would from the network: another address, the +// NAS's address as its Host. +func TestLANModelsWithKeyBesideAnotherToken(t *testing.T) { + setup(t, provider.Chat, &fake{t: t}) + t.Setenv("MAGPIE_ADDR", "") + keys, secrets := newCaller(t, "PC") + key := secrets[0] + var who string + h := lanGuard(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + who = access.Caller(r.Context()).KeyID + New().Handler().ServeHTTP(w, r) + })) + list := func(from, path string, hdr ...string) (int, string) { + who = "" + r := httptest.NewRequest("GET", "http://192.168.0.200:3425"+path, nil) + r.RemoteAddr = from + 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() + } + for _, hdr := range [][]string{ + {"x-api-key", key, "anthropic-version", "2023-06-01"}, + {"x-api-key", key, "Authorization", "Bearer " + key, "anthropic-version", "2023-06-01"}, + {"x-api-key", key, "Authorization", "Bearer "}, + {"x-api-key", key, "Authorization", "Bearer"}, + {"x-api-key", key, "Authorization", "Bearer undefined"}, + {"x-api-key", key, "Authorization", "Bearer magpie"}, + {"Authorization", "bearer " + key}, + {"Authorization", "Bearer magpie", "x-goog-api-key", key}, + } { + for _, path := range []string{"/v1/models", "/models", "/v1/models?limit=1000"} { + c, b := list("192.168.0.9:50123", path, hdr...) + if c != 200 || !strings.Contains(b, `"id":"fake/m1"`) { + t.Fatalf("%s %q: %d %s", path, hdr, c, b) + } + if who != keys[0].ID { + t.Fatalf("%s %q: counted as %q, not the key", path, hdr, who) + } + } + } + // a key the gateway doesn't have is refused wherever it is sent + for _, hdr := range [][]string{ + {"x-api-key", "sk-magpie-wrong", "Authorization", "Bearer magpie"}, + {"Authorization", "Bearer "}, + {"x-api-key", key + "x"}, + } { + if c, _ := list("192.168.0.9:50123", "/v1/models", hdr...); c != http.StatusUnauthorized { + t.Fatalf("%q got %d", hdr, c) + } + } + // this computer is counted by the key beside a token too + if c, _ := list("127.0.0.1:50123", "/v1/models", "Authorization", "Bearer anything", "x-api-key", key); c != 200 || who != keys[0].ID { + t.Fatalf("loopback: %d, counted as %q", c, who) + } +}