mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(providers): do not follow upstream redirects
The adapter client re-sent provider API key headers to any redirect target. The client now returns the 3xx response to the caller. A test pins this behaviour.
This commit is contained in:
+8
-1
@@ -231,7 +231,14 @@ func newAdapterHTTPClient(timeout time.Duration) *http.Client {
|
||||
}
|
||||
tr.MaxIdleConnsPerHost = adapterIdleConnsPerHost
|
||||
tr.ResponseHeaderTimeout = timeout
|
||||
return &http.Client{Transport: tr}
|
||||
// Redirects are not followed: the client re-sends provider API key
|
||||
// headers, and a hijacked redirect target would receive them.
|
||||
return &http.Client{
|
||||
Transport: tr,
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// attemptRecorder is invoked once per upstream HTTP attempt a shared
|
||||
|
||||
@@ -6,8 +6,11 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"testing/iotest"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestProviderPassthroughEnabled is the security+performance audit
|
||||
@@ -210,3 +213,49 @@ func TestReadAllLimited_PropagatesNonEOFError(t *testing.T) {
|
||||
t.Errorf("readAllLimited err = %v, want errors.Is match for %v", gotErr, injected)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterHTTPClient_RedirectHandling pins that the adapter client
|
||||
// never follows a redirect, so provider API key headers are not re-sent
|
||||
// to a redirect target.
|
||||
func TestAdapterHTTPClient_RedirectHandling(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
wantStatus int
|
||||
wantFollowed bool
|
||||
}{
|
||||
{name: "redirect returned to caller, target not contacted", wantStatus: http.StatusFound, wantFollowed: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
var followed atomic.Bool
|
||||
target := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
||||
followed.Store(true)
|
||||
}))
|
||||
t.Cleanup(target.Close)
|
||||
redirector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, target.URL, http.StatusFound)
|
||||
}))
|
||||
t.Cleanup(redirector.Close)
|
||||
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, redirector.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequestWithContext: %v", err)
|
||||
}
|
||||
req.Header.Set("x-api-key", "test-key")
|
||||
resp, err := newAdapterHTTPClient(time.Second).Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Do: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = resp.Body.Close() })
|
||||
|
||||
if resp.StatusCode != tt.wantStatus {
|
||||
t.Errorf("status = %d, want %d", resp.StatusCode, tt.wantStatus)
|
||||
}
|
||||
if got := followed.Load(); got != tt.wantFollowed {
|
||||
t.Errorf("redirect target contacted = %v, want %v", got, tt.wantFollowed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user