Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a few issues with redirects #462

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions lib/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,9 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
for {
// For all but the first request, create the next
// request hop and replace req.
loc := req.URL.String()
if len(reqs) > 0 {
loc := resp.Header.Get("Location")
loc = resp.Header.Get("Location")
if loc == "" {
return nil, uerr(fmt.Errorf("%d response missing Location header", resp.StatusCode))
}
Expand Down Expand Up @@ -571,14 +572,6 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
if ref := refererForURL(reqs[len(reqs)-1].URL, req.URL); ref != "" {
req.Header.Set("Referer", ref)
}
err = c.checkRedirect(req, resp, reqs)

// Sentinel error to let users select the
// previous response, without closing its
// body. See Issue 10069.
if err == ErrUseLastResponse {
return resp, nil
}

// Close the previous response's body. But
// read at least some of the body so if it's
Expand All @@ -590,16 +583,6 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
io.CopyN(ioutil.Discard, resp.Body, maxBodySlurpSize)
}
resp.Body.Close()

if err != nil {
// Special case for Go 1 compatibility: return both the response
// and an error if the CheckRedirect function failed.
// See https://golang.org/issue/3795
// The resp.Body has already been closed.
ue := uerr(err)
ue.(*url.Error).URL = loc
return resp, ue
}
}

reqs = append(reqs, req)
Expand All @@ -614,6 +597,22 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
}
return nil, uerr(err)
}
err = c.checkRedirect(req, resp, reqs)

// Sentinel error to let users select the
// previous response, without closing its
// body. See Issue 10069.
if err == ErrUseLastResponse {
return resp, nil
}
if err != nil {
// Special case for Go 1 compatibility: return both the response
// and an error if the CheckRedirect function failed.
// See https://golang.org/issue/3795
ue := uerr(err)
ue.(*url.Error).URL = loc
return resp, err
}

var shouldRedirect bool
redirectMethod, shouldRedirect, includeBody = redirectBehavior(req.Method, resp, reqs[0])
Expand Down
15 changes: 11 additions & 4 deletions modules/http/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var (
// ErrTooManyRedirects is returned when the number of HTTP redirects exceeds
// MaxRedirects.
ErrTooManyRedirects = errors.New("Too many redirects")

ErrDoNotRedirect = errors.New("No redirects configured")
)

// Flags holds the command-line configuration for the HTTP scan module.
Expand Down Expand Up @@ -388,6 +390,13 @@ func redirectsToLocalhost(host string) bool {
// the redirectToLocalhost and MaxRedirects config
func (scan *scan) getCheckRedirect() func(*http.Request, *http.Response, []*http.Request) error {
return func(req *http.Request, res *http.Response, via []*http.Request) error {
if scan.scanner.config.MaxRedirects == 0 {
return ErrDoNotRedirect
}
//len-1 because otherwise we'll return a failure on 1 redirect when we specify only 1 redirect. I.e. we are 0
if len(via)-1 > scan.scanner.config.MaxRedirects {
return ErrTooManyRedirects
}
if !scan.scanner.config.FollowLocalhostRedirects && redirectsToLocalhost(req.URL.Hostname()) {
return ErrRedirLocalhost
}
Expand All @@ -413,10 +422,6 @@ func (scan *scan) getCheckRedirect() func(*http.Request, *http.Response, []*http
}
}

if len(via) > scan.scanner.config.MaxRedirects {
return ErrTooManyRedirects
}

return nil
}
}
Expand Down Expand Up @@ -529,6 +534,8 @@ func (scan *scan) Grab() *zgrab2.ScanError {
}
if err != nil {
switch err {
case ErrDoNotRedirect:
break
case ErrRedirLocalhost:
break
case ErrTooManyRedirects:
Expand Down
Loading