diff --git a/admin.go b/admin.go index 481f737..530de8b 100644 --- a/admin.go +++ b/admin.go @@ -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. diff --git a/llmgateway.go b/llmgateway.go index 1781220..e1208e6 100644 --- a/llmgateway.go +++ b/llmgateway.go @@ -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) } } diff --git a/sanitize_provider_err_reserialised_test.go b/sanitize_provider_err_reserialised_test.go new file mode 100644 index 0000000..1fc122d --- /dev/null +++ b/sanitize_provider_err_reserialised_test.go @@ -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) + } +}