Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions plugins/governance/blocklist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package governance

import (
"testing"

"github.com/maximhq/bifrost/core/schemas"
)

func TestIsModelBlockedByList(t *testing.T) {
tests := []struct {
name string
blacklist schemas.BlackList
model string
want bool
}{
{
name: "empty blacklist allows",
blacklist: schemas.BlackList{},
model: "mistral:latest",
want: false,
},
{
name: "wildcard blocks all",
blacklist: schemas.BlackList{"*"},
model: "llama3.2:latest",
want: true,
},
{
name: "bare blacklist blocks bare request",
blacklist: schemas.BlackList{"mistral:latest"},
model: "mistral:latest",
want: true,
},
{
name: "prefixed blacklist blocks bare request",
blacklist: schemas.BlackList{"ollama/mistral:latest"},
model: "mistral:latest",
want: true,
},
{
name: "bare blacklist blocks prefixed request",
blacklist: schemas.BlackList{"mistral:latest"},
model: "ollama/mistral:latest",
want: true,
},
{
name: "prefixed blacklist blocks prefixed request",
blacklist: schemas.BlackList{"ollama/mistral:latest"},
model: "ollama/mistral:latest",
want: true,
},
{
name: "different model is not blocked",
blacklist: schemas.BlackList{"mistral:latest"},
model: "llama3.2:latest",
want: false,
},
{
name: "case-insensitive match preserved",
blacklist: schemas.BlackList{"Ollama/Mistral:Latest"},
model: "ollama/mistral:latest",
want: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isModelBlockedByList(tt.blacklist, tt.model); got != tt.want {
t.Fatalf("isModelBlockedByList(%v, %q) = %v, want %v", tt.blacklist, tt.model, got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion plugins/governance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func (p *GovernancePlugin) loadBalanceProvider(ctx *schemas.BifrostContext, req
// Pre-pass: if any config for a provider blacklists the model, that provider is fully blocked.
blacklistedProviders := make(map[string]bool)
for _, config := range providerConfigs {
if config.BlacklistedModels.IsBlocked(modelStr) {
if isModelBlockedByList(config.BlacklistedModels, modelStr) {
blacklistedProviders[config.Provider] = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/governance/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (r *BudgetResolver) isModelAllowed(vk *configstoreTables.TableVirtualKey, p

// Pass 1: if any matching provider config blacklists the model, block immediately.
for _, pc := range vk.ProviderConfigs {
if pc.Provider == string(provider) && pc.BlacklistedModels.IsBlocked(model) {
if pc.Provider == string(provider) && isModelBlockedByList(pc.BlacklistedModels, model) {
return false
}
}
Expand Down
33 changes: 32 additions & 1 deletion plugins/governance/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package governance

import (
"context"
"slices"
"strings"

bifrost "github.com/maximhq/bifrost/core"
Expand Down Expand Up @@ -87,6 +88,36 @@ func getWeight(w *float64) float64 {
return *w
}

func blockedModelCandidates(model string) []string {
_, normalized := schemas.ParseModelString(model, "")

if strings.EqualFold(model, normalized) {
return []string{model}
}

return []string{model, normalized}
}

func isModelBlockedByList(blacklist schemas.BlackList, model string) bool {
if blacklist.IsBlockAll() {
return true
}

modelForms := blockedModelCandidates(model)
for _, blocked := range blacklist {
blockedForms := blockedModelCandidates(blocked)
for _, form := range modelForms {
if slices.ContainsFunc(blockedForms, func(blockedForm string) bool {
return strings.EqualFold(blockedForm, form)
}) {
return true
}
}
}

return false
}

// filterModelsForVirtualKey filters models based on virtual key's provider configs
// Returns only models that are allowed by the virtual key's ProviderConfigs
func (p *GovernancePlugin) filterModelsForVirtualKey(
Expand Down Expand Up @@ -114,7 +145,7 @@ func (p *GovernancePlugin) filterModelsForVirtualKey(
// Pre-pass: if any matching config blacklists the model, block it entirely.
isBlocked := false
for _, pc := range vk.ProviderConfigs {
if pc.Provider == string(provider) && pc.BlacklistedModels.IsBlocked(modelName) {
if pc.Provider == string(provider) && isModelBlockedByList(pc.BlacklistedModels, modelName) {
isBlocked = true
break
}
Expand Down
Loading