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:
2026-11-03 00:23:01 +00:00
parent f9418cc860
commit 1a2cea3417
2 changed files with 46 additions and 1 deletions
+31
View File
@@ -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)
}
})
}
}