gateway: a key held to accounts is told only of the image and video models they serve (#2579)
A gateway key held to some accounts was told, in /v1/models' image and
video lists (drawerObjects, videomakerObjects), of every image and video
model its held models named, whether or not an account the key may use
serves it. Its chat list already applied accountServes; the two media
lists applied modelAllowed alone. Both now apply both, as entryAllowed
does. Reported by ggbdpq; the patch and test are ump45nose's
(fix/2579-image-video-account-serves, 592cc4db), with a t.Cleanup added
and the doc comments and failure message reworded.
Siblings: the other modelAllowed-only callers are not model lists (the
codex and desktop subagent picks, chatGPTSearchers, quotas_scope, which
handles accounts in card(), and videoModelHeld). The Drawers/Videomakers
uses in draw.go, video.go and drawing_route.go resolve a model, they do
not list them.
Run at this commit's tree (base 6f950653f):
- With the fix removed, TestGatewayKeyAccountsHoldTheImageAndVideoLists
fails: "a key whose account serves no image or video model is told of
image [grok/grok-imagine-image grok/grok-imagine-image-quality], video
[grok/grok-imagine-video grok/grok-imagine-video-1.5]". With it, it and
TestGatewayKeyModelsHoldTheImageAndVideoLists pass.
- gofmt -l: clean. go vet ./...; GOOS=linux go vet -tags nogui;
GOOS=windows go vet (with and without -tags nogui): clean. Builds for
darwin, linux (nogui) and windows: ok.
- go test -tags nogui -timeout 45m ./... with pipefail, sandbox HOME:
every package ok, EXIT=0 (internal/gateway 1315s).
Co-authored-by: ggbdpq <67929842+ggbdpq@users.noreply.github.com>
Co-authored-by: ump45nose <52391318+ump45nose@users.noreply.github.com>
This commit is contained in:
co-authored by
ggbdpq
ump45nose
parent
6f950653fc
commit
c1e95f919b
@@ -938,7 +938,7 @@ func nativeEndpoints(e provider.Entry) []string {
|
||||
// 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. A gateway key held to some models is told of those alone, as its
|
||||
// chat list is.
|
||||
// chat list is: models it names that an account it may use serves (#2579).
|
||||
func drawerObjects(r *http.Request) []map[string]any {
|
||||
who, held := keyHolds(r)
|
||||
var out []map[string]any
|
||||
@@ -947,7 +947,7 @@ func drawerObjects(r *http.Request) []map[string]any {
|
||||
continue
|
||||
}
|
||||
for _, m := range Drawers(p) {
|
||||
if held && !modelAllowed(who, p, m.ID) {
|
||||
if held && (!modelAllowed(who, p, m.ID) || !accountServes(who, p, m.ID)) {
|
||||
continue
|
||||
}
|
||||
name := cmp.Or(m.Name, m.ID)
|
||||
@@ -965,7 +965,8 @@ func drawerObjects(r *http.Request) []map[string]any {
|
||||
|
||||
// videomakerObjects are the video models magpie makes videos with, as
|
||||
// another magpie asks for them (provider.VideomakersHeader): "kind": "video".
|
||||
// A gateway key held to some models is told of those alone.
|
||||
// A gateway key held to some models or accounts is told of those alone,
|
||||
// as its chat list is (#2579).
|
||||
func videomakerObjects(r *http.Request) []map[string]any {
|
||||
who, held := keyHolds(r)
|
||||
var out []map[string]any
|
||||
@@ -974,7 +975,7 @@ func videomakerObjects(r *http.Request) []map[string]any {
|
||||
continue
|
||||
}
|
||||
for _, m := range Videomakers(p) {
|
||||
if held && !modelAllowed(who, p, m.ID) {
|
||||
if held && (!modelAllowed(who, p, m.ID) || !accountServes(who, p, m.ID)) {
|
||||
continue
|
||||
}
|
||||
name := cmp.Or(m.Name, m.ID)
|
||||
|
||||
@@ -394,3 +394,55 @@ func TestGatewayKeyModelsHoldTheImageAndVideoLists(t *testing.T) {
|
||||
t.Fatalf("a key held to one video model is told of image %v, video %v", image, video)
|
||||
}
|
||||
}
|
||||
|
||||
// A gateway key held to an account that serves only a chat model must not
|
||||
// be told of that provider's image/video models in /v1/models (#2579): the
|
||||
// chat catalog already applies accountServes; drawerObjects and
|
||||
// videomakerObjects used to list them from modelAllowed alone.
|
||||
func TestGatewayKeyAccountsHoldTheImageAndVideoLists(t *testing.T) {
|
||||
grokSignedIn(t)
|
||||
newGrokMedia(t)
|
||||
keys, secrets := newCaller(t, "Held", "Free")
|
||||
if _, err := access.Update("accounts-key", access.Change{Key: keys[0].ID, Accounts: []string{"grok/me@x.ai"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SetAccountModels("grok", "me@x.ai", []string{"grok-4"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = provider.SetAccountModels("grok", "me@x.ai", nil)
|
||||
})
|
||||
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[1]); !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 whose account serves no image or video model is told of image %v, video %v", image, video)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user