A person leaves licences out, and no key of theirs reaches a model under one
CI / test (push) Successful in 11m28s

A game sold in the EU cannot ship what a licence excluding the EU made. On
the profile page a person leaves out licences one by one, or by rule:
every licence forbidding commercial use, every licence granting nothing in
a region. The rules cover models added later. The choice reaches every
principal through the resolved limits, so permits() leaves such models out
of /v1/models, requests, jobs, fallbacks and the portal's pickers, and a
request for one is refused 403 licence_excluded saying which choice did.

Kept in user_excluded_licences (migration 0046), copied with the store and
gone with the person.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-11 16:10:22 +02:00
1 parent a313b3e0da
commit 4bbac0833d
20 files changed
+658 -7

No files matched your search

+3
View File
@@ -58,6 +58,9 @@ func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) {
}))
r.registerImagesPage(mux, pages)
r.registerMeshesPage(mux, pages)
if r.store != nil {
pages.SetLicences(r.licenceChoices, r.store)
}
}
// chatSend dispatches one turn on behalf of a signed-in person.
+6 -1
View File
@@ -546,7 +546,7 @@ func (r *Router) handleInference(path string) http.HandlerFunc {
}
if p, ok := auth.FromContext(req.Context()); ok {
if !r.permits(p, parsed.Model) {
auth.WriteForbiddenModel(w, parsed.Model)
r.writeForbidden(w, p, parsed.Model)
r.m.observeRequest(r.metricModel(parsed.Model), "", http.StatusForbidden, time.Since(start), nil, 0)
return
}
@@ -702,6 +702,11 @@ func (r *Router) permits(p auth.Principal, requested string) bool {
return true
}
canonical, m := r.resolve(requested)
// A licence the person has said not to use leaves the model out
// whatever the allowlist says: it is theirs to narrow, not to widen.
if licenceExcluded(p.Limits.ExcludedLicences, m) != "" {
return false
}
if p.Allows(canonical) {
return true
}
+1 -2
View File
@@ -11,7 +11,6 @@ import (
"strings"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
@@ -107,7 +106,7 @@ func (r *Router) createJob(w http.ResponseWriter, req *http.Request, path string
return
}
if !r.permits(p, in.Model) {
auth.WriteForbiddenModel(w, in.Model)
r.writeForbidden(w, p, in.Model)
return
}
if why := r.imageRouteRefusal(path, in.Model, body); why != "" {
+128
View File
@@ -0,0 +1,128 @@
package router
import (
"fmt"
"net/http"
"slices"
"sort"
"strings"
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
"gitea.mynodes.xyz/ilker/llmgw/internal/web"
)
// What a person may say not to use, besides a licence by its id: every
// licence that forbids commercial use, and every licence that grants nothing
// in a region. Rules rather than a list, so a model added later under such
// a licence is left out without anybody remembering to tick it.
const (
licenceUnspecified = "unspecified"
ruleNonCommercial = "rule:non-commercial"
ruleRegionPrefix = "rule:region:"
)
// licenceKey is how a model's licence is matched: its id, else its name,
// lowercased; licenceUnspecified for a model whose entry names none.
func licenceKey(m catalog.Model) string {
if m.License == nil {
return licenceUnspecified
}
if m.License.ID != "" {
return strings.ToLower(m.License.ID)
}
return strings.ToLower(strings.TrimSpace(m.License.Name))
}
// licenceExcluded says which of the person's choices leaves a model out,
// empty when none does.
func licenceExcluded(excluded []string, m catalog.Model) string {
if len(excluded) == 0 {
return ""
}
key := licenceKey(m)
for _, e := range excluded {
switch {
case strings.EqualFold(e, key):
return e
case e == ruleNonCommercial:
if m.License != nil && m.License.Commercial != nil && !*m.License.Commercial {
return e
}
case strings.HasPrefix(e, ruleRegionPrefix):
if m.License != nil && slices.Contains(m.License.ExcludedRegions, strings.TrimPrefix(e, ruleRegionPrefix)) {
return e
}
}
}
return ""
}
// excludedByLicence says why the principal's person has left this model
// out, empty when they have not.
func (r *Router) excludedByLicence(p auth.Principal, requested string) string {
if len(p.Limits.ExcludedLicences) == 0 {
return ""
}
_, m := r.resolve(requested)
return licenceExcluded(p.Limits.ExcludedLicences, m)
}
// licenceChoices are what the profile page offers: each licence the
// catalog's models are under, with the models, and the regions any of them
// excludes.
func (r *Router) licenceChoices() web.LicenceChoices {
by := map[string]*web.LicenceChoice{}
regions := map[string]bool{}
nonCommercial := false
for name, m := range r.modelMap() {
if name != m.Name || r.hiddenModel(name) {
continue // an alias, or a guardrail's judge nobody asks for
}
key := licenceKey(m)
c, ok := by[key]
if !ok {
c = &web.LicenceChoice{Key: key}
if m.License != nil {
c.Name, c.URL = m.License.Name, m.License.URL
if cm := m.License.Commercial; cm != nil {
c.Commercial = map[bool]string{true: "yes", false: "no"}[*cm]
}
c.ExcludedRegions = m.License.ExcludedRegions
}
by[key] = c
}
c.Models = append(c.Models, m.Name)
if m.License != nil {
for _, reg := range m.License.ExcludedRegions {
regions[reg] = true
}
if m.License.Commercial != nil && !*m.License.Commercial {
nonCommercial = true
}
}
}
var out web.LicenceChoices
for _, c := range by {
sort.Strings(c.Models)
out.Licences = append(out.Licences, *c)
}
sort.Slice(out.Licences, func(i, j int) bool { return out.Licences[i].Key < out.Licences[j].Key })
for reg := range regions {
out.Regions = append(out.Regions, reg)
}
sort.Strings(out.Regions)
out.AnyNonCommercial = nonCommercial
return out
}
// writeForbidden refuses a model the principal may not use, saying why when
// it is the person's own choice of licences rather than the key's allowlist.
func (r *Router) writeForbidden(w http.ResponseWriter, p auth.Principal, model string) {
if why := r.excludedByLicence(p, model); why != "" {
writeErrorCode(w, http.StatusForbidden, "invalid_request_error", "licence_excluded",
fmt.Sprintf("the model %q is under a licence its account has chosen not to use (%s); change it on the profile page", model, why))
return
}
auth.WriteForbiddenModel(w, model)
}
+86
View File
@@ -0,0 +1,86 @@
package router
import (
"crypto/sha256"
"net/http"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
func TestAPersonsLicenceChoiceLeavesModelsOutOfTheirKeys(t *testing.T) {
// A game sold in the EU cannot ship what a licence excluding the EU
// made: its maker says so once, and no key of theirs reaches such a model.
yes, no := true, false
names := []string{"mit", "nc", "eu-out", "plain"}
e := newFakeEngine(t, "m")
cfg := baseConfig(config.Endpoint{ID: "a", URL: e.srv.URL, Models: names})
mk := func(name string, l *catalog.License) catalog.Model {
m := catModel(name, nil)
m.License = l
return m
}
cfg.Models = []catalog.Model{
mk("mit", &catalog.License{ID: "MIT", Name: "MIT", Commercial: &yes}),
mk("nc", &catalog.License{ID: "CC-BY-NC-4.0", Name: "CC BY-NC 4.0", Commercial: &no}),
mk("eu-out", &catalog.License{ID: "tencent-hunyuan-community", Name: "Tencent Hunyuan Community",
ExcludedRegions: []string{"EU", "UK", "KR"}}),
mk("plain", nil),
}
cfg.Auth.Enabled = true
cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db")
rt := newTestRouter(t, cfg)
ctx := t.Context()
if err := rt.store.CreateUser(ctx, store.User{ID: "usr-g", Email: "g@game.test", State: store.UserActive,
CreatedAt: time.Now()}); err != nil {
t.Fatal(err)
}
secret := store.KeyPrefix + strings.Repeat("g", 43)
sum := sha256.Sum256([]byte(secret))
if err := rt.store.CreateUserKey(ctx, "usr-g", store.Key{ID: "key-g", Hash: sum[:], Prefix: "gggg",
Name: "game", CreatedAt: time.Now()}); err != nil {
t.Fatal(err)
}
if err := rt.store.SetUserExcludedLicences(ctx, "usr-g",
[]string{"rule:non-commercial", "rule:region:EU", "unspecified"}); err != nil {
t.Fatal(err)
}
h := rt.Handler()
_, list, raw := call(t, h, secret, http.MethodGet, "/v1/models", nil)
var listed []string
for _, m := range list["data"].([]any) {
listed = append(listed, m.(map[string]any)["id"].(string))
}
if !slices.Equal(listed, []string{"mit"}) {
t.Errorf("listed %v: %s", listed, raw)
}
for _, name := range []string{"nc", "eu-out", "plain"} {
code, _, raw := call(t, h, secret, http.MethodPost, "/v1/chat/completions",
map[string]any{"model": name, "messages": []any{map[string]any{"role": "user", "content": "hi"}}})
if code != http.StatusForbidden || !strings.Contains(raw, "licence_excluded") {
t.Errorf("%s: %d %s", name, code, raw)
}
}
if code, _, raw := call(t, h, secret, http.MethodPost, "/v1/chat/completions",
map[string]any{"model": "mit", "messages": []any{map[string]any{"role": "user", "content": "hi"}}}); code != 200 {
t.Errorf("mit: %d %s", code, raw)
}
// The profile page is offered every licence, and the regions in play.
c := rt.licenceChoices()
var keys []string
for _, l := range c.Licences {
keys = append(keys, l.Key)
}
if !slices.Equal(keys, []string{"cc-by-nc-4.0", "mit", "tencent-hunyuan-community", "unspecified"}) ||
!slices.Equal(c.Regions, []string{"EU", "KR", "UK"}) || !c.AnyNonCommercial {
t.Errorf("choices %+v", c)
}
}