gateway: a gateway key held to some models is told of those image and video models alone in /v1/models (塔哥 on Discord)

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.
This commit is contained in:
yetone
2026-10-16 14:15:36 +08:00
parent 107fbbc36d
commit 8b84dc902b
3 changed files with 69 additions and 6 deletions
+15 -5
View File
@@ -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())
+53
View File
@@ -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)
}
}
+1 -1
View File
@@ -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)
}
}