mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
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.
This commit is contained in:
+15
-1
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user