mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
136 lines
4.5 KiB
Go
136 lines
4.5 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const configuredSecret = "PLACEHOLDERSECRET"
|
|
|
|
// captureLargeStderr drains the pipe while fn runs; captureStderr reads
|
|
// afterwards and would block on output above the pipe buffer.
|
|
func captureLargeStderr(t *testing.T, fn func()) string {
|
|
t.Helper()
|
|
orig := os.Stderr
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Stderr = w
|
|
done := make(chan string)
|
|
go func() {
|
|
b, _ := io.ReadAll(r)
|
|
done <- string(b)
|
|
}()
|
|
fn()
|
|
os.Stderr = orig
|
|
_ = w.Close()
|
|
return <-done
|
|
}
|
|
|
|
func TestSanitizeProviderErr_EveryContextRedacted(t *testing.T) {
|
|
passwordURL := "http://" + "user:" + configuredSecret + "@h.invalid:1/v1"
|
|
queryURL := "http://h.invalid:1/v1?key=" + configuredSecret
|
|
contexts := map[string]string{
|
|
"double quote": `"` + configuredSecret + `"`,
|
|
"single quote": `'` + configuredSecret + `'`,
|
|
"backtick": "`" + configuredSecret + "`",
|
|
"space": "token " + configuredSecret + " end",
|
|
"percent 3D": "a%3D" + configuredSecret,
|
|
"percent 2F": "http%3A%2F%2F" + configuredSecret,
|
|
"bracket": "[" + configuredSecret + "]",
|
|
"comma": "a," + configuredSecret,
|
|
"hash": "a#" + configuredSecret,
|
|
"line start": configuredSecret + " failed",
|
|
"after newline": "x\n" + configuredSecret,
|
|
}
|
|
for _, cfg := range []string{passwordURL, queryURL} {
|
|
for name, msg := range contexts {
|
|
t.Run(name, func(t *testing.T) {
|
|
if got := sanitizeProviderErr(msg, cfg); strings.Contains(got, configuredSecret) {
|
|
t.Errorf("credential leaked: %q", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSanitizeProviderErr_ShortAndCommonComponentsKept(t *testing.T) {
|
|
cfg := "http://h.invalid:1/v1?a=true&b=api&c=v1&d=" + configuredSecret
|
|
const msg = "api v1 true http failed"
|
|
if got := sanitizeProviderErr(msg, cfg); got != msg {
|
|
t.Errorf("ordinary text redacted: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestTargetProxy_ClientQueryContributesNoComponents(t *testing.T) {
|
|
addr := closedLoopbackAddr(t)
|
|
h := newTargetRedirectGateway(t, "http://"+addr+"/mcp?api-key="+configuredSecret, "http://"+addr+"/a2a")
|
|
pad := strings.Repeat("a", 950<<10)
|
|
q := "x=127.0.0.1&y=" + url.QueryEscape("context canceled") + "&pad=" + pad
|
|
req := httptest.NewRequest(http.MethodGet, "/mcp/alpha?"+q, nil)
|
|
req.Header.Set("Authorization", "Bearer sk-alice")
|
|
start := time.Now()
|
|
out := captureLargeStderr(t, func() { h.ServeHTTP(httptest.NewRecorder(), req) })
|
|
t.Logf("elapsed %v", time.Since(start))
|
|
if !strings.Contains(out, "upstream connection error") {
|
|
t.Fatalf("precondition: no connection error logged (len %d)", len(out))
|
|
}
|
|
if strings.Contains(out, configuredSecret) {
|
|
t.Error("log carries the configured credential")
|
|
}
|
|
if !strings.Contains(out, "x=127.0.0.1") {
|
|
t.Error("client query text was redacted")
|
|
}
|
|
}
|
|
|
|
func TestUnparseableConfiguredURL_NoSecretInAnyErrorText(t *testing.T) {
|
|
// Joined at run time: staticcheck rejects a constant invalid URL, and the invalid URL is the subject.
|
|
raw := strings.Join([]string{"http://user:", configuredSecret, "/x@h"}, "")
|
|
_, perr := url.Parse(raw)
|
|
if perr == nil || !strings.Contains(perr.Error(), configuredSecret) {
|
|
t.Fatalf("precondition: parse error must carry the secret: %v", perr)
|
|
}
|
|
t.Run("parse error", func(t *testing.T) {
|
|
if got := sanitizeProviderErr(perr.Error(), raw); strings.Contains(got, configuredSecret) {
|
|
t.Errorf("leaked: %q", got)
|
|
}
|
|
})
|
|
t.Run("transport error", func(t *testing.T) {
|
|
resp, err := http.Get(raw + "/v1/models") //nolint:gosec,noctx // unparseable URL, the error text is the subject
|
|
if err == nil {
|
|
_ = resp.Body.Close()
|
|
t.Fatal("expected an error")
|
|
}
|
|
if got := sanitizeProviderErr(err.Error(), raw); strings.Contains(got, configuredSecret) {
|
|
t.Errorf("leaked: %q", got)
|
|
}
|
|
})
|
|
t.Run("overview and log", func(t *testing.T) {
|
|
h, log := renderingsGateway(t, raw)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminOverviewPath, "sk-admin1"))
|
|
var got adminOverviewResponse
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil || len(got.Providers) != 1 {
|
|
t.Fatalf("overview decode: %v", err)
|
|
}
|
|
if strings.Contains(got.Providers[0].LastErr, configuredSecret) || strings.Contains(log, configuredSecret) {
|
|
t.Errorf("leaked: lastErr=%q log=%q", got.Providers[0].LastErr, log)
|
|
}
|
|
})
|
|
t.Run("unified route", func(t *testing.T) {
|
|
out := unifiedChatLog(t, raw, context.Background())
|
|
if strings.Contains(out, configuredSecret) {
|
|
t.Errorf("log leaked: %q", out)
|
|
}
|
|
})
|
|
}
|