Files
magpie/internal/access/held_models_test.go
T
yetone 05a6087e1f access: a key with accounts and no model may use only its accounts' providers' models
Linka on Discord (#feedback): in a gateway key's "Models this key can
use", ticking only an account and no model let the key list and use
every model of every provider.

Cause: Key.Models empty meant "every model", and Key.Accounts (#905)
held only the providers it names, so every other provider stayed open.

Change: access.HeldModels gives a key with accounts and no models the
models "<provider>/*" of each account's provider. Authenticate builds
the Identity with it, so every route's existing model gate narrows:
/v1/models, /v1beta/models, chat, responses, messages, Gemini,
count_tokens, embeddings, images, the Codex backend path, System One,
groups and fallbacks. A key that lists models is unchanged (#905: its
accounts still hold only the providers they name); a key with neither
is unchanged. The CLI's MODELS column and `gateway-key models` say so,
and the GUI editor's hint, the "not used" notes on providers no model
or account reaches, and the save status follow (en, zh, zh-TW, ja, de).

Tests:
- TestGatewayKeyAccountsOnlyHoldTheModels: each route above is 403 for
  the accounts-only key and no upstream call reaches the other
  provider, the decider or the image provider; its own provider, a
  group of it and a key that also lists models work. Red on the old
  code on every route (list showed spare/m2, chat/count_tokens/
  embeddings/systemone 200).
- TestHeldModels (access).
- gateway-key-accounts.test.cjs: hint, notes and status; 4/4 red on
  the old assets, green on the new.
- TestGatewayKeyAccountsListModels / SystemOne / OfANamedProvider and
  TestHeldKeyToldItsOwnQuotas now encode the new rule (the held key is
  told only its own provider's quotas).

Run on mote: gofmt -l clean; go vet darwin, linux -tags nogui, windows
on access, cli, gateway, gui ok; go test -timeout 45m ./internal/access
./internal/cli ./internal/gui ./internal/gateway: ok except gui's
TestMainWindowCorners/macOS_26_SDK (#3949, not this change) and
gateway's TestHeldKeyToldItsOwnQuotas, updated and rerun ok. After the
rebase on 1f03d7546 the key tests ran again ok. GUI: gateway-key*,
i18n-keys, gui-de, gui-ja, gui-zh-tw in Chromium and WebKit: 131 pass.

Not tried: a plugin-served provider (same model gate), a real
account, Windows and Linux test runs (vet only).
2026-10-30 01:25:57 +08:00

28 lines
980 B
Go

package access
import (
"slices"
"testing"
)
// A key that names accounts and no model may use only its accounts'
// providers' models (Linka on Discord); one that lists models keeps
// them, and one that lists neither uses every model.
func TestHeldModels(t *testing.T) {
for _, c := range []struct {
models, accounts, want []string
}{
{nil, nil, nil},
{[]string{"openai/gpt-5"}, []string{"codex/acct-1"}, []string{"openai/gpt-5"}},
{nil, []string{"codex/acct-1", "CODEX/acct-2", "relay/abc"}, []string{"codex/*", "relay/*"}},
} {
if got := HeldModels(c.models, c.accounts); !slices.Equal(got, c.want) {
t.Errorf("HeldModels(%v, %v) = %v, want %v", c.models, c.accounts, got, c.want)
}
}
who := Identity{Models: HeldModels(nil, []string{"codex/acct-1"}), Accounts: []string{"codex/acct-1"}}
if !who.Allows("codex/gpt-5.5") || who.Allows("anthropic/claude-opus-4-8") || who.Allows("group/any") {
t.Fatal("an account-only key's models", who.Models)
}
}