mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
fix(admin): scrub the re-serialised base URL from provider errors
sanitizeProviderErr now also replaces the URL as net/url writes it. A provider baseUrl with userinfo, a query or a fragment logs one construction warning. The config still loads.
This commit is contained in:
@@ -299,20 +299,24 @@ const unparseableBaseURLPlaceholder = "[unparseable url]"
|
||||
//
|
||||
// Still best-effort for URLs OTHER than rawBaseURL: an error naming a
|
||||
// redirect target is not caught. Rejecting a credential-bearing baseUrl
|
||||
// at construction (providers.go) is the only complete fix; this is the
|
||||
// containment that changes no behavior.
|
||||
// at construction (providers.go) would be the complete fix; instead the
|
||||
// constructor only warns, so a config that loads today keeps loading.
|
||||
func sanitizeProviderErr(lastErr, rawBaseURL string) string {
|
||||
if lastErr == "" || rawBaseURL == "" {
|
||||
return lastErr
|
||||
}
|
||||
out := replaceURLForm(lastErr, rawBaseURL)
|
||||
u, parseErr := url.Parse(rawBaseURL)
|
||||
if parseErr != nil || u.User == nil {
|
||||
if parseErr != nil {
|
||||
return out
|
||||
}
|
||||
// net/http reports url.URL.String(), which lower-cases the scheme and
|
||||
// re-escapes userinfo, so it can differ from the configured text.
|
||||
out = replaceURLForm(out, u.String())
|
||||
if u.User == nil {
|
||||
return out
|
||||
}
|
||||
if _, hasPassword := u.User.Password(); !hasPassword {
|
||||
// No password set means net/http passed the URL through
|
||||
// unmasked, so the verbatim pass above already matched it.
|
||||
return out
|
||||
}
|
||||
// net/http's exact stripPassword output, mirrored call for call.
|
||||
|
||||
+6
-1
@@ -1273,8 +1273,13 @@ func newGateway(ctx context.Context, next http.Handler, config *Config, name str
|
||||
// would otherwise reopen the same leak through g.configWarnings /
|
||||
// GET /admin/api/overview's own Warnings field instead of through
|
||||
// baseUrl directly.
|
||||
if _, parseErr := url.Parse(pc.BaseURL); parseErr != nil {
|
||||
bu, parseErr := url.Parse(pc.BaseURL)
|
||||
if parseErr != nil {
|
||||
g.warnf("config: provider %q baseUrl does not parse as a URL; the admin dashboard will show it redacted as %q", name, unparseableBaseURLPlaceholder)
|
||||
continue
|
||||
}
|
||||
if bu.User != nil || bu.RawQuery != "" || bu.Fragment != "" {
|
||||
g.warnf("config: provider %q baseUrl carries userinfo, a query or a fragment; error text scrubbing is best-effort, so keep credentials in apiKey instead", name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package traefikllmgateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func closedLoopbackAddr(t *testing.T) string {
|
||||
t.Helper()
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := l.Addr().String()
|
||||
_ = l.Close()
|
||||
return addr
|
||||
}
|
||||
|
||||
func TestSanitizeProviderErr_ReserialisedForms(t *testing.T) {
|
||||
const tok = "DUMMYTOKEN123"
|
||||
addr := closedLoopbackAddr(t)
|
||||
cases := []struct {
|
||||
name string
|
||||
base string
|
||||
}{
|
||||
{"upper-case scheme with userinfo", "HTTP://" + tok + "@" + addr + "/v1"},
|
||||
{"escaped userinfo, no password", "http://us%40" + tok + "@" + addr + "/v1"},
|
||||
{"upper-case scheme with query credential", "HTTP://" + addr + "/v1?key=" + tok},
|
||||
{"lower-case userinfo", "http://" + tok + "@" + addr + "/v1"},
|
||||
{"user and password", "http://user:" + tok + "@" + addr + "/v1"},
|
||||
{"lower-case query", "http://" + addr + "/v1?key=" + tok},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
resp, err := http.Get(tc.base) //nolint:gosec,noctx // loopback closed port, error text is the subject
|
||||
if err == nil {
|
||||
_ = resp.Body.Close()
|
||||
t.Fatal("expected a dial error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tok) && !strings.Contains(err.Error(), "***") {
|
||||
t.Fatalf("precondition: error text does not carry the credential: %v", err)
|
||||
}
|
||||
got := sanitizeProviderErr(err.Error(), tc.base)
|
||||
if strings.Contains(got, tok) {
|
||||
t.Fatalf("credential leaked: %q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_WarnsOnceForCredentialBearingBaseURL(t *testing.T) {
|
||||
const tok = "DUMMYTOKEN123"
|
||||
cfg := CreateConfig()
|
||||
cfg.Providers = map[string]*ProviderConfig{
|
||||
"bad": {Type: "openai", APIKey: "k", BaseURL: "https://" + tok + "@example.invalid/v1"},
|
||||
"good": {Type: "openai", APIKey: "k", BaseURL: "https://example.invalid/v1"},
|
||||
}
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
||||
h, err := New(context.Background(), next, cfg, "llmgw")
|
||||
if err != nil {
|
||||
t.Fatalf("New must keep accepting the config: %v", err)
|
||||
}
|
||||
warnings, _ := h.(*Gateway).configWarningsSnapshot()
|
||||
var n int
|
||||
for _, w := range warnings {
|
||||
if strings.Contains(w, "userinfo") {
|
||||
n++
|
||||
if strings.Contains(w, tok) || !strings.Contains(w, `"bad"`) {
|
||||
t.Errorf("warning must name the provider and never the credential: %q", w)
|
||||
}
|
||||
}
|
||||
}
|
||||
if n != 1 {
|
||||
t.Errorf("userinfo warnings = %d, want 1 (%v)", n, warnings)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user