diff --git a/client/client.go b/client/client.go index 77c552b83..ee0be9326 100644 --- a/client/client.go +++ b/client/client.go @@ -28,8 +28,9 @@ import ( ) var ( - ErrNoEndpoints = errors.New("client: no endpoints available") - ErrTooManyRedirects = errors.New("client: too many redirects") + ErrNoEndpoints = errors.New("client: no endpoints available") + ErrTooManyRedirects = errors.New("client: too many redirects") + errTooManyRedirectChecks = errors.New("client: too many redirect checks") ) var DefaultRequestTimeout = 5 * time.Second @@ -297,7 +298,7 @@ type redirectFollowingHTTPClient struct { } func (r *redirectFollowingHTTPClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) { - for i := 0; ; i++ { + for i := 0; i < 100; i++ { if i > 0 { if err := r.checkRedirect(i); err != nil { return nil, nil, err @@ -324,6 +325,8 @@ func (r *redirectFollowingHTTPClient) Do(ctx context.Context, act httpAction) (* } return resp, body, nil } + + return nil, nil, errTooManyRedirectChecks } type redirectedHTTPAction struct { diff --git a/client/client_test.go b/client/client_test.go index 224bc874a..4befb34a3 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -522,6 +522,18 @@ func TestRedirectFollowingHTTPClient(t *testing.T) { }, wantErr: errors.New("Location header not valid URL: :"), }, + + // fail if redirects checked way too many times + { + checkRedirect: func(int) error { return nil }, + client: &staticHTTPClient{ + resp: http.Response{ + StatusCode: http.StatusTemporaryRedirect, + Header: http.Header{"Location": []string{"http://example.com"}}, + }, + }, + wantErr: errTooManyRedirectChecks, + }, } for i, tt := range tests {