Files
magpie/internal/access/model_pattern_test.go
T
yetone 08cca73114 gateway keys: an issuer's re: pattern never covers a glob it can't hold
bf4d8f3e5 let a key's model be a re: pattern, and covered() tried that
pattern on an issued key's glob as text. A regex's '.' or a class like
[^-] takes the glob's '*' too, so an issuer holding "re:^openai/[^-]*$"
(openai/o3, not openai/gpt-4o) could issue "openai/*", and at the
gateway foldModels kept "openai/*": the issued key used openai/gpt-4o,
which its issuer may not. "re:^gr" covered "gr*", which names group/…
ids a pattern never does. The other way, a child's glob "r*" matched
the text "re:^openai/" and so took the issuer's whole pattern.

Now a pattern covers a glob only when it matches the glob's text before
the first '*' and asserts nothing about what follows ($, \z, \b, \B),
which makes it a match in every id the glob names, and when the glob
can't name a group. A pattern is covered only by itself or by "*". The
same check runs at issue time (IssueKey) and at each request
(foldModels). It is stricter than needed in one way: "re:gpt" no longer
covers "*gpt*" (only the text before the first '*' is judged).

Run on mote at this tree:
- TestPatternCoversAGlobOnlyForAll: red on origin/main ce5e5964d (7 of
  its checks, the openai/gpt-4o fold among them), green here.
- go test -tags nogui ./internal/access: ok.
- go test -tags nogui -run "Key|Issue|Pattern|Alias" ./internal/gateway
  ./internal/gui . : ok.
- GOOS=linux and GOOS=windows go vet ./internal/access, gofmt -l: clean.
Not run: the full suite, and the GUI (no GUI change).
2026-11-11 06:40:08 +08:00

100 lines
3.6 KiB
Go

package access
import (
"slices"
"testing"
)
func TestModelPatternsAreRegularExpressions(t *testing.T) {
for _, c := range []struct {
pat, id string
want bool
}{
{"re:^gpt-", "openai/gpt-5", true},
{"re:^openai/gpt-", "openai/gpt-5", true},
{"re:^GPT-5$", "openai/gpt-5", true},
{"re:^gpt-", "openai/o3", false},
{"re:gpt", "group/gpt-fans", false}, // never a routing group
{"RE:^gpt-", "openai/gpt-5", true},
{"re:(", "openai/gpt-5", false}, // a bad one names nothing, never all
} {
if got := ModelAllowed([]string{c.pat}, c.id); got != c.want {
t.Errorf("%s on %s: %v", c.pat, c.id, got)
}
}
}
func TestCleanModelsRefusesABadPattern(t *testing.T) {
for _, bad := range []string{"re:(", "re:", "re:[a-", "re: "} {
if _, err := CleanModels([]string{"openai/gpt-5", bad}); err == nil {
t.Errorf("kept %q", bad)
}
}
got, err := CleanModels([]string{" re:^gpt-.* ", "openai/*", "RE:^GPT-.*"})
if err != nil || !slices.Equal(got, []string{"re:^gpt-.*", "openai/*"}) {
t.Fatalf("%v %v", got, err)
}
}
func TestCleanAliases(t *testing.T) {
got, err := CleanAliases(map[string]string{" fast ": " openai/gpt-5 ", "": ""})
if err != nil || len(got) != 1 || got["fast"] != "openai/gpt-5" {
t.Fatalf("%v %v", got, err)
}
for _, bad := range []map[string]string{
{"a b": "openai/gpt-5"}, {"re:x": "openai/gpt-5"}, {"x*": "openai/gpt-5"},
{"fast": "gpt-5"}, {"fast": "openai/*"}, {"fast": "re:^gpt"}, {"openai/gpt-5": "openai/GPT-5"},
{"fast": ""}, {"": "openai/gpt-5"}, {"Fast": "openai/gpt-5", "fast": "openai/gpt-4"},
} {
if _, err := CleanAliases(bad); err == nil {
t.Errorf("kept %v", bad)
}
}
}
func TestIssuedKeyTakesNoPattern(t *testing.T) {
_, err := IssueKey(Issue{Name: "x", TTL: 3600e9, Models: []string{"re:^gpt-"}})
if err == nil {
t.Fatal("issued a key with a pattern")
}
}
// An issuer's pattern is never tried on a glob's text: [^-] or '.' takes
// its '*', so "re:^openai/[^-]*$" covered "openai/*" and an issued key
// got openai/gpt-4o, which its issuer may not use.
func TestPatternCoversAGlobOnlyForAll(t *testing.T) {
for _, c := range []struct {
issuer, child string
want bool
}{
{"re:^openai/[^-]*$", "openai/*", false},
{"re:^openai/gpt-.$", "openai/gpt-*", false},
{"re:^openai/gpt-5$", "openai/gpt-5*", false},
{"re:^openai/gpt\\b", "openai/gpt*", false},
{"re:^gr", "gr*", false}, // the glob names group/… too
{"re:^openai/", "openai/gpt-*", true},
{"re:^gpt-", "openai/gpt-*", true},
{"re:gpt", "*gpt*", false}, // only the text before the '*' is judged
{"re:^openai/[^-]*$", "openai/o3", true},
{"re:^openai/[^-]*$", "openai/gpt-4o", false},
} {
if got := covered([]string{c.issuer}, c.child); got != c.want {
t.Errorf("covered(%q, %q) = %v, want %v", c.issuer, c.child, got, c.want)
}
}
// at the gateway, the issued key is held to what both allow (an empty
// fold is refused there)
may := func(who []string, id string) bool { return len(who) > 0 && ModelAllowed(who, id) }
who := foldModels([]string{"openai/*"}, []string{"re:^openai/[^-]*$"})
if may(who, "openai/gpt-4o") {
t.Errorf("folded %q: an issued key may use openai/gpt-4o, which its issuer may not", who)
}
// a glob in the child never stands in for the issuer's pattern
if who := foldModels([]string{"r*"}, []string{"re:^openai/"}); may(who, "openai/gpt-4o") {
t.Errorf("folded %q: the child's r* took the issuer's whole pattern", who)
}
if who := foldModels([]string{"*"}, []string{"re:^openai/"}); !may(who, "openai/gpt-4o") || may(who, "anthropic/x") {
t.Errorf("folded %q: * with the issuer's pattern is the pattern", who)
}
}