From 1a2cea341704ee21c83bef49d380b51dfc0f2ba8 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 3 Nov 2026 00:23:01 +0000 Subject: [PATCH] fix(users): cap the users file read at 16 MiB The loader read the whole users file into memory on the request path, whatever its size. The loader now reads at most the cap plus one byte and refuses a larger file. The last good user set stays active. --- users_file.go | 16 +++++++++++++++- users_file_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/users_file.go b/users_file.go index f34c509..d55163d 100644 --- a/users_file.go +++ b/users_file.go @@ -3,6 +3,7 @@ package traefikllmgateway import ( "encoding/json" "fmt" + "io" "os" "time" ) @@ -12,6 +13,11 @@ import ( // under heavy concurrent traffic. const reloadEvery = 5 * time.Second +// maxUsersFileBytes caps how much of the users file load reads. A real file +// is a few KiB to a few MiB; the cap only stops an oversized file from being +// read into memory on the request path. +const maxUsersFileBytes = 16 << 20 + // usersFile is a JSON-encoded file of API-key users at path, in the shape // {"users":[{"name":...,"group":...,"apiKey":...,"limits":{...}}]}. type usersFile struct { @@ -32,10 +38,18 @@ type usersFileDoc struct { // load reads and parses uf's file. path is operator-supplied middleware // configuration, not untrusted request input. func (uf *usersFile) load() ([]*UserConfig, error) { - b, err := os.ReadFile(uf.path) // #nosec G304 -- operator-supplied users file path from middleware config + f, err := os.Open(uf.path) // #nosec G304 -- operator-supplied users file path from middleware config if err != nil { return nil, fmt.Errorf("llmgateway: cannot read users file %q: %w", uf.path, err) } + defer f.Close() //nolint:errcheck // read-only close; nothing actionable on failure + b, err := io.ReadAll(io.LimitReader(f, maxUsersFileBytes+1)) + if err != nil { + return nil, fmt.Errorf("llmgateway: cannot read users file %q: %w", uf.path, err) + } + if len(b) > maxUsersFileBytes { + return nil, fmt.Errorf("llmgateway: users file %q exceeds %d bytes", uf.path, maxUsersFileBytes) + } var doc usersFileDoc if err := json.Unmarshal(b, &doc); err != nil { return nil, fmt.Errorf("llmgateway: cannot parse users file %q: %w", uf.path, err) diff --git a/users_file_test.go b/users_file_test.go index dfe6212..d6c5497 100644 --- a/users_file_test.go +++ b/users_file_test.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "slices" + "strings" "sync" "testing" "time" @@ -514,3 +515,33 @@ func TestAuthStore_MaybeReload_MultiGroupUser_RebuiltPrincipalStillAuthorizesCor t.Error("want gamma still denied after reload") } } + +// TestUsersFile_Load_SizeCap pins that a users file larger than +// maxUsersFileBytes is refused instead of read into memory. +func TestUsersFile_Load_SizeCap(t *testing.T) { + t.Parallel() + const doc = `{"users":[]}` + tests := []struct { + name string + size int + wantErr bool + }{ + {name: "small valid file", size: len(doc), wantErr: false}, + {name: "exactly at the cap", size: maxUsersFileBytes, wantErr: false}, + {name: "one byte over the cap", size: maxUsersFileBytes + 1, wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + path := filepath.Join(t.TempDir(), "users.json") + content := doc + strings.Repeat(" ", tt.size-len(doc)) + if err := os.WriteFile(path, []byte(content), 0o600); err != nil { + t.Fatalf("WriteFile: %v", err) + } + _, err := newUsersFile(path).load() + if (err != nil) != tt.wantErr { + t.Errorf("load err = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}