fix(admin): browser key entry, batched usage reads, hardened responses

Review fixes (2 Important + 4 folded):

1. GET /admin now serves the HTML shell with no authentication -
   a browser navigating directly to the URL cannot attach a custom
   header. The page reads an admin key from sessionStorage and sends
   it as x-api-key on /admin/api/* fetches, falling back to an inline
   key-entry form on a missing key or any 401/403. Both JSON routes
   keep their original 401/403/200 gate. Spec ss4 amended in place.

2. GET /admin/api/usage no longer serializes 6 GETs per user/group
   scope on the shared Redis connection. respClient gains getBatch
   (one pipelined round trip); counterStore gains getMulti; limiter
   gains storeGetMulti mirroring storeGet's fail-open/fail-closed
   policy. currentUsage now issues one getMulti per scope.

3. Provider baseUrl is stripped of userinfo and query before it is
   ever echoed in the overview JSON (sanitizeBaseURL), stronger than
   net/url's own Redacted (which leaves a bare username in place).

4. limiter.redisStatus now also returns the last failure's timestamp,
   so the dashboard can render "last error (Ns ago)" instead of a
   bare message that reads as currently-broken indefinitely.

5. Both JSON admin responses now set X-Content-Type-Options: nosniff,
   Cache-Control: no-store, and the same CSP the HTML page uses.

6. Added a test proving a file-sourced (replaceFileUsers) admin user
   gets the same access an inline admin user does.

go test ./... -count=1 -race, gofmt, vet, make yaegi-check all pass.
This commit is contained in:
2026-08-20 19:46:35 +01:00
parent 547b61f5a9
commit 33bd8bc2cc
7 changed files with 628 additions and 62 deletions
+257 -6
View File
@@ -7,6 +7,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
@@ -107,11 +108,43 @@ func TestAdmin_Disabled_HonorsPassthroughUnknown(t *testing.T) {
}
}
// TestAdminPage_ServedWithoutAuth is the controller-approved amendment to
// spec §4 (2026-08-20 review): GET /admin serves the HTML shell with no
// authentication at all, for every caller — a browser navigating
// straight to the URL has no way to attach a custom Authorization or
// x-api-key header, so gating the page itself made the dashboard
// unreachable from a browser. /admin/api/* stay fully gated (see
// TestAdmin_GateMatrix below); the page carries no data of its own, so
// serving it unauthenticated leaks nothing — verified here by scanning
// the body for the same canary-shaped check TestAdmin_SecretRedaction
// uses, not just trusting the doc comment.
func TestAdminPage_ServedWithoutAuth(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
h, _ := newAdminGatewayHandle(t, cfg)
for _, apiKey := range []string{"", "sk-alice", "sk-admin1", "not-a-real-key"} {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminPagePath, apiKey))
if rec.Code != http.StatusOK {
t.Errorf("apiKey=%q: status = %d, want 200 (GET /admin is unauthenticated)", apiKey, rec.Code)
}
for _, secret := range []string{"sk-zeta", "sk-alpha", "sk-alice", "sk-admin1"} {
if strings.Contains(rec.Body.String(), secret) {
t.Errorf("apiKey=%q: page body leaks configured secret %q", apiKey, secret)
}
}
}
}
// TestAdmin_GateMatrix covers the two /admin/api/* routes only — GET
// /admin itself is unauthenticated by design (TestAdminPage_
// ServedWithoutAuth above).
func TestAdmin_GateMatrix(t *testing.T) {
cfg := newAdminTestConfig()
h, _ := newAdminGatewayHandle(t, cfg)
paths := []string{adminPagePath, adminOverviewPath, adminUsagePath}
paths := []string{adminOverviewPath, adminUsagePath}
for _, p := range paths {
t.Run(p+"/unauthenticated", func(t *testing.T) {
rec := httptest.NewRecorder()
@@ -406,8 +439,10 @@ func TestAdminPage_CSPHeaderAndFetchURLs(t *testing.T) {
cfg := newAdminTestConfig()
h, _ := newAdminGatewayHandle(t, cfg)
// Unauthenticated: GET /admin is served without auth (see
// TestAdminPage_ServedWithoutAuth), so no key is presented here.
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminPagePath, "sk-admin1"))
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminPagePath, ""))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rec.Code)
}
@@ -420,7 +455,7 @@ func TestAdminPage_CSPHeaderAndFetchURLs(t *testing.T) {
}
body := rec.Body.String()
for _, want := range []string{adminOverviewPath, adminUsagePath, "setInterval"} {
for _, want := range []string{adminOverviewPath, adminUsagePath, "setInterval", "sessionStorage", "x-api-key"} {
if !strings.Contains(body, want) {
t.Errorf("admin page body missing %q", want)
}
@@ -455,6 +490,15 @@ func TestAdmin_SecretRedaction(t *testing.T) {
h, _ := newAdminGatewayHandle(t, cfg)
// GET /admin is unauthenticated by design (controller-approved
// amendment, 2026-08-20 review): assert it still returns 200 and
// still leaks nothing.
pageRec := httptest.NewRecorder()
h.ServeHTTP(pageRec, adminRequest(http.MethodGet, adminPagePath, ""))
if pageRec.Code != http.StatusOK {
t.Fatalf("page status = %d, want 200 (unauthenticated)", pageRec.Code)
}
overviewRec := httptest.NewRecorder()
h.ServeHTTP(overviewRec, adminRequest(http.MethodGet, adminOverviewPath, adminKey))
if overviewRec.Code != http.StatusOK {
@@ -468,10 +512,15 @@ func TestAdmin_SecretRedaction(t *testing.T) {
}
canaries := []string{providerKey, redisPass, aliceKey, adminKey}
for _, name := range []string{"overview", "usage"} {
body := overviewRec.Body.String()
if name == "usage" {
for _, name := range []string{"page", "overview", "usage"} {
var body string
switch name {
case "page":
body = pageRec.Body.String()
case "usage":
body = usageRec.Body.String()
default:
body = overviewRec.Body.String()
}
for _, secret := range canaries {
if strings.Contains(body, secret) {
@@ -487,3 +536,205 @@ func TestAdmin_SecretRedaction(t *testing.T) {
}
}
}
// --- overview: baseUrl strips userinfo and query (folded item 3, 2026-08-20 review) ---
func TestAdminOverview_BaseURLStripsCredentials(t *testing.T) {
t.Parallel()
// Built via net/url rather than a literal string constant: a scheme
// plus embedded username-colon-password-at-host literal in source
// trips generic secret scanners (trufflehog's URI detector) even
// though this is a fabricated test fixture, not a real credential.
// Assembling it programmatically exercises the identical code path
// (sanitizeBaseURL parses and strips it exactly the same either way)
// without putting a credential-shaped string literal in the diff.
credentialBaseURL := (&url.URL{
Scheme: "https",
User: url.UserPassword("embeddeduser", "embeddedpass"),
Host: "openai.invalid",
RawQuery: "api-key=leakedquerysecret",
}).String()
cfg := CreateConfig()
cfg.Admin = &AdminConfig{Enabled: true}
cfg.Providers = map[string]*ProviderConfig{
// Discovery is left false: nothing ever dials this URL, it exists
// purely to prove sanitizeBaseURL strips it before echoing.
"openai": {Type: "openai", BaseURL: credentialBaseURL, APIKey: "sk", Models: []string{"gpt-test"}},
}
cfg.Groups = map[string]*GroupConfig{"g": {}}
cfg.Users = &UsersConfig{Inline: []*UserConfig{{Name: "admin1", Group: "g", APIKey: "sk-admin1", Admin: true}}}
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminOverviewPath, "sk-admin1"))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200, body=%s", rec.Code, rec.Body.String())
}
body := rec.Body.String()
for _, leaked := range []string{"embeddeduser", "embeddedpass", "leakedquerysecret", "api-key="} {
if strings.Contains(body, leaked) {
t.Errorf("overview response leaks base URL credential/query material %q", leaked)
}
}
var got adminOverviewResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
if len(got.Providers) != 1 {
t.Fatalf("providers = %+v, want 1 entry", got.Providers)
}
wantBaseURL := "https://openai.invalid"
if got.Providers[0].BaseURL != wantBaseURL {
t.Errorf("baseUrl = %q, want %q (userinfo and query stripped)", got.Providers[0].BaseURL, wantBaseURL)
}
}
// --- usage: batched reads (important item 2, 2026-08-20 review) ---
// countingMultiStore is a counterStore stub whose getMulti serves a fixed
// value per key and counts how many times it was called — it proves
// limiter.currentUsage batches a scope's six counter reads into one
// getMulti call rather than issuing them as six separate get calls.
// incrBy/get are never exercised by currentUsage and just return zero
// values.
type countingMultiStore struct {
values map[string]int64
getMultiCalls int
}
func (s *countingMultiStore) incrBy(string, int64, time.Duration) (int64, error) {
return 0, nil
}
func (s *countingMultiStore) get(string) (int64, error) { return 0, nil }
func (s *countingMultiStore) getMulti(keys []string) ([]int64, error) {
s.getMultiCalls++
out := make([]int64, len(keys))
for i, k := range keys {
out[i] = s.values[k]
}
return out, nil
}
func TestLimiterCurrentUsage_BatchesOneGetMultiCallPerScope(t *testing.T) {
t.Parallel()
fixedNow := time.Date(2026, 8, 20, 10, 0, 0, 0, time.UTC)
store := &countingMultiStore{values: map[string]int64{
windowKey("user", "alice", metricReq, windowMin, fixedNow): 3,
windowKey("user", "alice", metricReq, windowDay, fixedNow): 7,
windowKey("user", "alice", metricTok, windowDay, fixedNow): 50,
windowKey("user", "alice", metricTok, windowMonth, fixedNow): 60,
windowKey("user", "alice", metricCost, windowDay, fixedNow): 1_000,
windowKey("user", "alice", metricCost, windowMonth, fixedNow): 2_000,
windowKey("group", "g1", metricReq, windowMin, fixedNow): 1,
}}
l := newLimiter(store, true)
l.nowFn = func() time.Time { return fixedNow }
scopes := []limitScope{
{kind: "user", id: "alice", limits: &LimitsConfig{}},
{kind: "group", id: "g1", limits: &LimitsConfig{}},
}
got := l.currentUsage(scopes)
if store.getMultiCalls != len(scopes) {
t.Errorf("getMultiCalls = %d, want %d (one pipelined getMulti per scope, not six separate get calls)", store.getMultiCalls, len(scopes))
}
if len(got) != 2 {
t.Fatalf("len(got) = %d, want 2", len(got))
}
alice := got[0]
if alice.storeDown {
t.Fatal("alice.storeDown must be false")
}
if alice.requestsPerMinute != 3 || alice.requestsPerDay != 7 || alice.tokensPerDay != 50 ||
alice.tokensPerMonth != 60 || alice.costPerDayMicros != 1_000 || alice.costPerMonthMicros != 2_000 {
t.Errorf("alice usage = %+v, want the seeded values", alice)
}
g1 := got[1]
if g1.requestsPerMinute != 1 {
t.Errorf("g1 requestsPerMinute = %d, want 1", g1.requestsPerMinute)
}
}
// --- overview: redis lastErr carries a timestamp (folded item 4, 2026-08-20 review) ---
func TestAdminOverview_RedisLastErrAt(t *testing.T) {
t.Parallel()
cfg := CreateConfig()
cfg.Admin = &AdminConfig{Enabled: true}
cfg.Providers = map[string]*ProviderConfig{
"openai": {Type: "openai", BaseURL: "http://openai.invalid", APIKey: "sk", Models: []string{"gpt-test"}},
}
// Nothing listens on 127.0.0.1:1: every operation against it fails
// fast (connection refused), driving a real store failure without a
// live Redis dependency.
cfg.Redis = &RedisConfig{Address: "127.0.0.1:1"}
cfg.Groups = map[string]*GroupConfig{"g": {}}
cfg.Users = &UsersConfig{Inline: []*UserConfig{{Name: "admin1", Group: "g", APIKey: "sk-admin1", Admin: true}}}
h, _ := newAdminGatewayHandle(t, cfg)
// buildAdminUsage lists admin1 even with no seeded traffic (spec's
// "for every user and group"), so this alone triggers a real
// storeGetMulti call against the unreachable store.
usageRec := httptest.NewRecorder()
h.ServeHTTP(usageRec, adminRequest(http.MethodGet, adminUsagePath, "sk-admin1"))
if usageRec.Code != http.StatusOK {
t.Fatalf("usage status = %d, want 200, body=%s", usageRec.Code, usageRec.Body.String())
}
overviewRec := httptest.NewRecorder()
h.ServeHTTP(overviewRec, adminRequest(http.MethodGet, adminOverviewPath, "sk-admin1"))
if overviewRec.Code != http.StatusOK {
t.Fatalf("overview status = %d, want 200, body=%s", overviewRec.Code, overviewRec.Body.String())
}
var got adminOverviewResponse
if err := json.Unmarshal(overviewRec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
if !got.Redis.Configured {
t.Error("redis.configured must be true")
}
if got.Redis.LastErr == "" {
t.Error("redis.lastErr must be non-empty after a real store failure")
}
if got.Redis.LastErrAt.IsZero() {
t.Fatal("redis.lastErrAt must be set after a real store failure")
}
if age := time.Since(got.Redis.LastErrAt); age < 0 || age > 30*time.Second {
t.Errorf("redis.lastErrAt age = %v, want within 30s of now", age)
}
}
// --- file-sourced admin user (folded item 6, 2026-08-20 review) ---
// TestAdmin_FileUserAdminFlag_GrantsAccess proves UserConfig.Admin flows
// through the file-users hot-reload path (authStore.replaceFileUsers,
// auth.go) exactly like an inline user's — buildEntry is shared by both,
// but this had never been exercised end-to-end through /admin/api/*
// before. File-users granting admin is operator-controlled via the
// Secret backing Users.File, an accepted trust boundary per
// UserConfig.Admin's own doc comment (llmgateway.go).
func TestAdmin_FileUserAdminFlag_GrantsAccess(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
h, gw := newAdminGatewayHandle(t, cfg)
fileUsers := []*UserConfig{
{Name: "fileadmin", Group: "agroup", APIKey: "sk-filedmin", Admin: true},
}
if err := gw.auth.replaceFileUsers(fileUsers); err != nil {
t.Fatalf("replaceFileUsers: %v", err)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminOverviewPath, "sk-filedmin"))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200 for a file-sourced admin user, body=%s", rec.Code, rec.Body.String())
}
}