Files
traefik-llmgateway/sanitize_provider_err_passthrough_test.go
T
lukaszraczyloandGitHub f1b198bce4 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.
2026-11-29 10:50:36 +00:00

62 lines
2.2 KiB
Go

package traefikllmgateway
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestPassthrough_ClientPathAndQueryStayIntact(t *testing.T) {
addr := closedLoopbackAddr(t)
env := newGateEnv(t, func(c *Config) {
c.Providers["openai"].BaseURL = "http://" + addr + "/v1?key=" + configuredSecret
})
req := httptest.NewRequest(http.MethodGet, "/openai/v1/x&connection&refused&dial?note=client-text", nil)
req.Header.Set("Authorization", "Bearer sk-alice")
out := captureStderr(t, func() { env.h.ServeHTTP(httptest.NewRecorder(), req) })
if !strings.Contains(out, "upstream connection error") {
t.Fatalf("precondition: no connection error logged: %q", out)
}
if strings.Contains(out, configuredSecret) {
t.Errorf("log carries the configured credential: %q", out)
}
for _, want := range []string{"connection refused", "note=client-text"} {
if !strings.Contains(out, want) {
t.Errorf("client text %q was redacted: %q", want, out)
}
}
}
func TestSanitizeProviderErr_QueryNamesAndMinimumLength(t *testing.T) {
t.Run("labelled query name kept", func(t *testing.T) {
const msg = "read tcp: i/o timeout"
if got := sanitizeProviderErr(msg, "http://h.invalid:1/v1?timeout=30"); got != msg {
t.Errorf("label redacted: %q", got)
}
})
t.Run("valueless query name redacted", func(t *testing.T) {
if got := sanitizeProviderErr("failed ?"+configuredSecret, "http://h.invalid:1/v1?"+configuredSecret); strings.Contains(got, configuredSecret) {
t.Errorf("leaked: %q", got)
}
})
t.Run("minimum length", func(t *testing.T) {
if got := sanitizeProviderErr("x abcd y", "http://h.invalid:1/v1?k=abcd"); got != "x *** y" {
t.Errorf("4-character secret not redacted: %q", got)
}
if got := sanitizeProviderErr("x abc y", "http://h.invalid:1/v1?k=abc"); got != "x abc y" {
t.Errorf("3-character value redacted: %q", got)
}
})
}
func TestConfiguredURLOf_NoGuessing(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/p?q=client", nil)
if got := configuredURLOf(r); got != "" {
t.Errorf("configuredURLOf without a recorded URL = %q, want empty", got)
}
if got := configuredURLOf(withConfiguredURL(r, "http://h.invalid/v1")); got != "http://h.invalid/v1" {
t.Errorf("configuredURLOf = %q", got)
}
}