fix(security): audit run-4 fixes and run-3 leftovers (#26)

Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
This commit is contained in:
2026-11-29 10:50:36 +00:00
committed by GitHub
parent 009de03264
commit f1b198bce4
48 changed files with 3598 additions and 211 deletions
+52
View File
@@ -0,0 +1,52 @@
package traefikllmgateway
import (
"net/http"
"net/http/httptest"
"sync"
"testing"
)
func upstreamBetaHeaders(t *testing.T, sent ...string) [][]string {
t.Helper()
var mu sync.Mutex
var seen [][]string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
seen = append(seen, append([]string(nil), r.Header.Values("anthropic-beta")...))
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(betaCacheResp))
}))
t.Cleanup(srv.Close)
gw := newMessagesTestGateway(t, srv.URL, "")
body := map[string]any{"model": "claude-test", "max_tokens": 10, "messages": []any{map[string]any{"role": "user", "content": "hi"}}}
req := newMessagesRequest(t, body, "x-api-key", "sk-alice")
for _, v := range sent {
req.Header.Add("anthropic-beta", v)
}
rec := httptest.NewRecorder()
gw.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String())
}
mu.Lock()
defer mu.Unlock()
return seen
}
func TestMessages_EmptyTokenBetaHeaderNotForwarded(t *testing.T) {
for _, v := range []string{",", " , ,"} {
seen := upstreamBetaHeaders(t, v)
if len(seen) != 1 || len(seen[0]) != 0 {
t.Errorf("sent %q: upstream saw %q, want no anthropic-beta header", v, seen)
}
}
}
func TestMessages_BetaHeaderForwardedVerbatim(t *testing.T) {
seen := upstreamBetaHeaders(t, "b-two, b-one", "b-three")
if len(seen) != 1 || len(seen[0]) != 2 || seen[0][0] != "b-two, b-one" || seen[0][1] != "b-three" {
t.Fatalf("upstream saw %q, want both values verbatim", seen)
}
}