From ba5158d4638138d01e6c2c9a8d79c86bc7472335 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Sun, 4 Oct 2026 10:49:54 +0200 Subject: [PATCH] List in /v1/models only the models the key may use Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 2 ++ internal/router/handlers.go | 9 ++++++++- internal/router/router_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0a36f13..96c41a23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -183,6 +183,8 @@ APIs change between minors without a deprecation period. handler**, and **thread ids are bounded** to 128 letters, digits and `- _ . :`. A thread is readable by every key of its project that knows the id; the docs now say to use unguessable ids. +- **`/v1/models` lists only what the key may use**, rather than every + model the gateway serves to anyone. ### Everything that can be changed on a running cluster, from the console diff --git a/internal/router/handlers.go b/internal/router/handlers.go index 20926486..91f87f13 100644 --- a/internal/router/handlers.go +++ b/internal/router/handlers.go @@ -796,7 +796,10 @@ func (r *Router) servedModels() []string { } // handleModels lists the models that at least one endpoint currently serves. -func (r *Router) handleModels(w http.ResponseWriter, _ *http.Request) { +// +// Only those the caller's key may use: listing the rest tells a key holder +// what the gateway serves to others, and invites requests that are refused. +func (r *Router) handleModels(w http.ResponseWriter, req *http.Request) { seen := map[string]struct{}{} for _, s := range r.reg.Snapshots("") { for _, m := range s.Models { @@ -810,8 +813,12 @@ func (r *Router) handleModels(w http.ResponseWriter, _ *http.Request) { } } + principal, authed := auth.FromContext(req.Context()) names := make([]string, 0, len(seen)) for n := range seen { + if authed && !r.permits(principal, n) { + continue + } names = append(names, n) } slices.Sort(names) diff --git a/internal/router/router_test.go b/internal/router/router_test.go index f6e4f6df..bad3f047 100644 --- a/internal/router/router_test.go +++ b/internal/router/router_test.go @@ -8,6 +8,7 @@ import ( "log/slog" "net/http" "net/http/httptest" + "slices" "strings" "sync/atomic" "testing" @@ -718,3 +719,32 @@ func TestAFallbackTheKeyMayNotUseIsNotServed(t *testing.T) { t.Fatalf("a key allowed both was not given the fallback: %d", rec.Code) } } + +func TestModelsEndpointListsOnlyWhatTheKeyMayUse(t *testing.T) { + e := newFakeEngine(t, "real") + cfg := baseConfig(config.Endpoint{ID: "a", URL: e.srv.URL, Models: []string{"real", "other"}}) + cfg.Models = []catalog.Model{catModel("real", []string{"real-alias"}), catModel("other", nil)} + rt := newTestRouter(t, cfg) + + p := auth.Principal{Limits: store.Effective{AllowedModels: []string{"real"}}} + req := httptest.NewRequest(http.MethodGet, "/v1/models", nil) + req = req.WithContext(auth.WithPrincipal(req.Context(), p)) + rec := httptest.NewRecorder() + rt.handleModels(rec, req) + + var out struct { + Data []struct { + ID string `json:"id"` + } `json:"data"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatal(err) + } + var ids []string + for _, d := range out.Data { + ids = append(ids, d.ID) + } + if !slices.Equal(ids, []string{"real", "real-alias"}) { + t.Errorf("listed %v; the key may use real and its alias only", ids) + } +}