From 8b84dc902bf016ef391836e1b287d091e827f5b6 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 16 Oct 2026 14:15:36 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20a=20gateway=20key=20held=20to=20some?= =?UTF-8?q?=20models=20is=20told=20of=20those=20image=20and=20video=20mode?= =?UTF-8?q?ls=20alone=20in=20/v1/models=20(=E5=A1=94=E5=93=A5=20on=20Disco?= =?UTF-8?q?rd)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A key limited to some models (#882) saw only those in /v1/models' chat list, but the image and video entries (kind image/video) were listed for every provider. Requests with them were already refused (draw.go, video.go), so this was a listing leak only. A remote magpie signed in with such a key showed the owner's whole image and video catalog. drawerObjects and videomakerObjects now take the request and drop what the key doesn't allow, as the chat list does. Test: TestGatewayKeyModelsHoldTheImageAndVideoLists (key_models_test.go): this computer and an unrestricted key see grok-imagine-image/-video, a key held to elsewhere/* sees neither, a key held to grok/grok-imagine-video sees that alone. Red on the old code (the restricted keys saw both), green now. Run at a15e57656 + this change, sandbox HOME: gofmt -l clean; GOOS=darwin/linux/windows go vet -tags nogui ./... clean. Full suite: set -o pipefail; go test -timeout 45m ./... -> every package ok except internal/gui TestPanelJoinsFullScreenSpaces ("the panel shown doesn't take the keyboard"), red the same on a15e57656 without this change (3 of 3 alone), filed as #2450. --- internal/gateway/gateway.go | 20 +++++++--- internal/gateway/key_models_test.go | 53 +++++++++++++++++++++++++++ internal/gateway/video_custom_test.go | 2 +- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 7a309f9a0..9756fa435 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -937,14 +937,19 @@ func nativeEndpoints(e provider.Entry) []string { // drawerObjects are the image models magpie draws with, as another magpie // asks for them in its list (provider.DrawersHeader): "kind": "image", // which an agent's list never has, so it takes none for a model to chat -// with. -func drawerObjects() []map[string]any { +// with. A gateway key held to some models is told of those alone, as its +// chat list is. +func drawerObjects(r *http.Request) []map[string]any { + who, held := keyHolds(r) var out []map[string]any for _, p := range provider.All() { if !p.On() || p.DecideOnly() { continue } for _, m := range Drawers(p) { + if held && !modelAllowed(who, p, m.ID) { + continue + } name := cmp.Or(m.Name, m.ID) o := map[string]any{"id": p.ID + "/" + m.ID, "object": "model", "type": "model", "kind": "image", "created": 0, "owned_by": p.ID, "display_name": name, "magpie_label": name + " · " + p.Name, @@ -960,13 +965,18 @@ func drawerObjects() []map[string]any { // videomakerObjects are the video models magpie makes videos with, as // another magpie asks for them (provider.VideomakersHeader): "kind": "video". -func videomakerObjects() []map[string]any { +// A gateway key held to some models is told of those alone. +func videomakerObjects(r *http.Request) []map[string]any { + who, held := keyHolds(r) var out []map[string]any for _, p := range provider.All() { if !p.On() || p.DecideOnly() { continue } for _, m := range Videomakers(p) { + if held && !modelAllowed(who, p, m.ID) { + continue + } name := cmp.Or(m.Name, m.ID) out = append(out, map[string]any{"id": p.ID + "/" + m.ID, "object": "model", "type": "model", "kind": "video", "created": 0, "owned_by": p.ID, "display_name": name, "magpie_label": name + " · " + p.Name, @@ -1053,10 +1063,10 @@ func (s *Server) models(w http.ResponseWriter, r *http.Request) { } } if r.Header.Get(provider.DrawersHeader) != "" { - data = append(data, drawerObjects()...) + data = append(data, drawerObjects(r)...) } if r.Header.Get(provider.VideomakersHeader) != "" { - data = append(data, videomakerObjects()...) + data = append(data, videomakerObjects(r)...) } if r.Header.Get(provider.DecidersHeader) != "" { deciders := keyAllowed(r, provider.Deciders()) diff --git a/internal/gateway/key_models_test.go b/internal/gateway/key_models_test.go index 8baecad0a..1153b5b9d 100644 --- a/internal/gateway/key_models_test.go +++ b/internal/gateway/key_models_test.go @@ -341,3 +341,56 @@ func TestGatewayKeyModelsHoldCountTokensAndSystemOne(t *testing.T) { t.Fatalf("a key given the decider: %d %s, decided %d", w.Code, w.Body.String(), decided) } } + +// Another magpie added as a provider with a gateway key held to some +// models asks this one for its image and video models too +// (provider.DrawersHeader, VideomakersHeader). It is told of those the +// key may use alone, as it is of chat models; a key free of models, and +// this computer, are told of every one. +func TestGatewayKeyModelsHoldTheImageAndVideoLists(t *testing.T) { + grokSignedIn(t) + newGrokMedia(t) + keys, secrets := newCaller(t, "Elsewhere", "Grok", "Free") + if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"elsewhere/*"}}); err != nil { + t.Fatal(err) + } + if _, err := access.Update("models-key", access.Change{Key: keys[1].ID, Models: []string{"grok/grok-imagine-video"}}); err != nil { + t.Fatal(err) + } + h := New().Handler() + listed := func(secret string) (image, video []string) { + r := httptest.NewRequest("GET", "/v1/models", nil) + if secret != "" { + r.Header.Set("Authorization", "Bearer "+secret) + } + r.Header.Set(provider.DrawersHeader, "1") + r.Header.Set(provider.VideomakersHeader, "1") + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + var l struct{ Data []struct{ ID, Kind string } } + if err := json.Unmarshal(w.Body.Bytes(), &l); err != nil { + t.Fatal(w.Body.String()) + } + for _, m := range l.Data { + switch m.Kind { + case "image": + image = append(image, m.ID) + case "video": + video = append(video, m.ID) + } + } + return image, video + } + if image, video := listed(""); !slices.Contains(image, "grok/grok-imagine-image") || !slices.Contains(video, "grok/grok-imagine-video") { + t.Fatalf("this computer is told of image %v, video %v", image, video) + } + if image, video := listed(secrets[2]); !slices.Contains(image, "grok/grok-imagine-image") || !slices.Contains(video, "grok/grok-imagine-video") { + t.Fatalf("a free key is told of image %v, video %v", image, video) + } + if image, video := listed(secrets[0]); len(image) != 0 || len(video) != 0 { + t.Fatalf("a key held elsewhere is told of image %v, video %v", image, video) + } + if image, video := listed(secrets[1]); len(image) != 0 || !slices.Equal(video, []string{"grok/grok-imagine-video"}) { + t.Fatalf("a key held to one video model is told of image %v, video %v", image, video) + } +} diff --git a/internal/gateway/video_custom_test.go b/internal/gateway/video_custom_test.go index baddba54c..901642aec 100644 --- a/internal/gateway/video_custom_test.go +++ b/internal/gateway/video_custom_test.go @@ -164,7 +164,7 @@ func TestCustomProviderMakesVideos(t *testing.T) { t.Fatalf("no model named: %d %s", code, raw) } // another magpie asking this one for its video models is told of it - if objs := videomakerObjects(); len(objs) != 1 || objs[0]["id"] != "studio/vid-1" { + if objs := videomakerObjects(httptest.NewRequest("GET", "/v1/models", nil)); len(objs) != 1 || objs[0]["id"] != "studio/vid-1" { t.Fatalf("videomakerObjects %v", objs) } }