Skip to content

Commit 730bf25

Browse files
committed
Don't allow single hop headers on redirect
Signed-off-by: Levi Gross <[email protected]>
1 parent 42058d7 commit 730bf25

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

base_get_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"encoding/xml"
7+
"errors"
78
"io"
89
"net/http"
910
"net/http/cookiejar"
@@ -1164,29 +1165,28 @@ func TestAuthStripOnRedirect(t *testing.T) {
11641165
srv.Close()
11651166
}
11661167

1167-
func TestNoAuthStripOnRedirect(t *testing.T) {
1168+
func TestNoRedirect(t *testing.T) {
11681169
srv := httptest.NewServer(http.DefaultServeMux)
1169-
http.HandleFunc("/tester/", func(w http.ResponseWriter, req *http.Request) {
1170-
if req.Header.Get("Authorization") == "" {
1171-
http.Error(w, "Didn't find Auth: "+req.Header.Get("Authorization"), http.StatusInternalServerError)
1172-
}
1173-
})
1174-
1175-
resp, err := Get(srv.URL+"/tester", &RequestOptions{
1176-
Auth: []string{"one ", "two"},
1177-
Headers: map[string]string{"WWW-Authenticate": "foo"},
1178-
RedirectLocationTrusted: true,
1170+
http.HandleFunc("/3tester/", func(w http.ResponseWriter, req *http.Request) {
1171+
http.Redirect(w, req, "/", http.StatusMovedPermanently)
11791172
})
11801173

1181-
if err != nil {
1182-
t.Error("Request didn't creds inside", err)
1174+
client := &http.Client{
1175+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
1176+
return errors.New("cancel redirection")
1177+
},
11831178
}
11841179

1185-
if resp.Ok != true {
1186-
t.Error("Request didn't creds inside", resp.StatusCode, resp.String())
1180+
_, err := Get(srv.URL+"/3tester/", &RequestOptions{
1181+
HTTPClient: client,
1182+
})
1183+
1184+
if err == nil {
1185+
t.Error("Request passed when it was supposed to fail on redirect", err)
11871186
}
11881187

11891188
srv.Close()
1189+
11901190
}
11911191

11921192
func verifyOkArgsResponse(resp *Response, t *testing.T) *BasicGetResponseArgs {

request.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,6 @@ type RequestOptions struct {
107107
// this is useful if you want to use an OAUTH client with your request.
108108
HTTPClient *http.Client
109109

110-
// RedirectLocationTrusted is a flag that will enable all headers to be
111-
// forwarded to the redirect location. Otherwise, the headers specified in
112-
// `SensitiveHTTPHeaders` will be removed from the request.
113-
RedirectLocationTrusted bool
114-
115110
// SensitiveHTTPHeaders is a map of sensitive HTTP headers that a user
116111
// doesn't want passed on a redirect.
117112
SensitiveHTTPHeaders map[string]struct{}
@@ -176,6 +171,7 @@ func buildRequest(httpMethod, url string, ro *RequestOptions, httpClient *http.C
176171
// Do we need to add any HTTP headers or Basic Auth?
177172
addHTTPHeaders(ro, req)
178173
addCookies(ro, req)
174+
179175
addRedirectFunctionality(httpClient, ro)
180176

181177
return httpClient.Do(req)

utils.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
const (
12-
localUserAgent = "GRequests/0.7"
12+
localUserAgent = "GRequests/0.10"
1313

1414
// Default value for net.Dialer Timeout
1515
dialTimeout = 30 * time.Second
@@ -52,6 +52,9 @@ var (
5252
type XMLCharDecoder func(charset string, input io.Reader) (io.Reader, error)
5353

5454
func addRedirectFunctionality(client *http.Client, ro *RequestOptions) {
55+
if client.CheckRedirect != nil {
56+
return
57+
}
5558
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
5659
if ro.RedirectLimit == 0 {
5760
ro.RedirectLimit = RedirectLimit
@@ -67,7 +70,7 @@ func addRedirectFunctionality(client *http.Client, ro *RequestOptions) {
6770

6871
for k, vv := range via[0].Header {
6972
// Is this a sensitive header?
70-
if _, found := ro.SensitiveHTTPHeaders[k]; found && ro.RedirectLocationTrusted == false {
73+
if _, found := ro.SensitiveHTTPHeaders[k]; found {
7174
continue
7275
}
7376

0 commit comments

Comments
 (0)