Skip to content

Commit 14078f6

Browse files
authored
allow 2 redirects for http client (#1993)
* allow 2 redirects for http client * update test and reponse
1 parent 86de509 commit 14078f6

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

transport/httptransport/http_transport.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,12 @@ func (h *httpTransport) Execute(ctx context.Context, transportInfo []byte, dealI
210210
} else {
211211
// do not follow http redirects for security reasons
212212
t.client = &http.Client{
213+
// Custom CheckRedirect function to limit redirects
213214
CheckRedirect: func(req *http.Request, via []*http.Request) error {
214-
return http.ErrUseLastResponse
215+
if len(via) >= 2 { // Limit to 2 redirects
216+
return http.ErrUseLastResponse
217+
}
218+
return nil
215219
},
216220
}
217221

transport/httptransport/http_transport_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ func TestDownloadFromPrivateIPs(t *testing.T) {
387387
}
388388

389389
func TestDontFollowHttpRedirects(t *testing.T) {
390-
// we should not follow http redirects for security reasons. If the target URL tries to redirect, the client should return 303 response instead.
391-
// This test sets up two servers, with one redirecting to the other. Without the redirect check the download would have been completed successfully.
390+
// we should not follow more than 2 http redirects for security reasons. If the target URL tries to redirect, the client should return 303 response instead.
391+
// This test sets up 3 servers, with one redirecting to the other. Without the redirect check the download would have been completed successfully.
392392
rawSize := (100 * readBufferSize) + 30
393393
ctx := context.Background()
394394
st := newServerTest(t, rawSize)
@@ -422,8 +422,14 @@ func TestDontFollowHttpRedirects(t *testing.T) {
422422
redirectSvr := httptest.NewServer(redirectHandler)
423423
defer redirectSvr.Close()
424424

425+
var redirectHandler1 http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
426+
http.Redirect(w, r, redirectSvr.URL, http.StatusSeeOther)
427+
}
428+
redirectSvr1 := httptest.NewServer(redirectHandler1)
429+
defer redirectSvr1.Close()
430+
425431
of := getTempFilePath(t)
426-
th := executeTransfer(t, ctx, New(nil, newDealLogger(t, ctx), NChunksOpt(numChunks)), carSize, types.HttpRequest{URL: redirectSvr.URL}, of)
432+
th := executeTransfer(t, ctx, New(nil, newDealLogger(t, ctx), NChunksOpt(numChunks)), carSize, types.HttpRequest{URL: redirectSvr1.URL}, of)
427433
require.NotNil(t, th)
428434

429435
evts := waitForTransferComplete(th)

0 commit comments

Comments
 (0)