Repository navigation
transform DoWithAuth from recursive to loop approach to prevent endless authentication attempts #6018
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
transform DoWithAuth from recursive to loop approach to prevent endless authentication attempts #6018
Changes from all commits
b3ac11b
4332f65
e87ac8f
a9f4927
a775cea
5ec91bb
1fe9b17
7eed85e
8359673
c614460
c352a7e
9c78f96
1b9b59b
49f784c
1245cbc
3c606d2
18290a6
c323f76
24bd477
98014b5
30b3075
163dcaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,30 +24,52 @@ var ( | |
| // authentication from netrc or git's credential helpers if necessary, | ||
| // supporting basic authentication. | ||
| func (c *Client) DoWithAuth(remote string, access creds.Access, req *http.Request) (*http.Response, error) { | ||
| count := 0 | ||
| res, err := c.doWithAuth(remote, &count, access, req, nil) | ||
|
|
||
| if errors.IsAuthError(err) { | ||
| if len(req.Header.Get("Authorization")) == 0 { | ||
| // This case represents a rejected request that | ||
| // should have been authenticated but wasn't. Do | ||
| // not count this against our redirection | ||
| // maximum. | ||
| newAccess := c.Endpoints.AccessFor(access.URL()) | ||
| tracerx.Printf("api: http response indicates %q authentication. Resubmitting...", newAccess.Mode()) | ||
| return c.DoWithAuth(remote, newAccess, req) | ||
| maxAuthAttempts := defaultMaxAuthAttempts | ||
| if access.Mode() == creds.NoneAccess { | ||
| maxAuthAttempts++ | ||
| } | ||
|
|
||
| for i := range maxAuthAttempts { | ||
| res, err := c.doWithAuth(remote, access, req, nil) | ||
| if err == nil || !errors.IsAuthError(err) { | ||
| return res, err | ||
| } | ||
|
|
||
| // We expect this condition should occur only when an | ||
| // Authorization header was already set for the request | ||
| // and the request was rejected. Otherwise, after a 401 | ||
| // status code is received, doWithAuth() will remove any | ||
| // Authorization header that it added to the request. | ||
| if len(req.Header.Get("Authorization")) != 0 { | ||
| return res, err | ||
| } | ||
|
|
||
| // This case represents a rejected request that | ||
| // should have been authenticated but wasn't, possibly because | ||
| // it is part of a multi-stage authentication sequence. | ||
| if res != nil && res.Body != nil { | ||
| res.Body.Close() | ||
| } | ||
|
|
||
| // If at least one more attempt is still permitted, we update | ||
| // the access mode and retry the request. | ||
| if i < maxAuthAttempts-1 { | ||
| access = c.Endpoints.AccessFor(access.URL()) | ||
| tracerx.Printf("api: http response indicates %q authentication. Resubmitting...", access.Mode()) | ||
| } | ||
| } | ||
|
chrisd8088 marked this conversation as resolved.
Resolved
|
||
|
|
||
| return res, err | ||
| c.credContext.SetStateFields(nil) | ||
|
|
||
| tracerx.Printf("api: too many authentication attempts") | ||
| return nil, fmt.Errorf("too many authentication attempts") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I started to implement this change, but on reflection, I think the message is probably sufficient as it stands, for a few reasons. First, I would hope that the statement that "too many attempts" have been made should imply to most readers that these attempts have not all been successful. Second, while each attempt has received a 401 response, they haven't been outright denied with a 403. So in that sense they haven't failed—but they also haven't completely succeeded. As a concrete example, in a long multi-stage authentication sequence, each stage could "succeed" in the sense that a 401 is returned and so prompt the client to proceed to the next stage, but we might still report this error if the number of stages exceeds our allowed maximum. While this is probably not going to occur in practice for anyone, I think this was the spirit in which the error message was first written, back in commit 6783078 of PR #5803. In other words, the client has made multiple authentication requests, but hasn't reached a point of either being accepted or denied. Its attempts to authenticate haven't specifically failed or succeeded, just reached an internal limit on the number of attempts. |
||
| } | ||
|
|
||
| // DoWithAuthNoRetry sends an HTTP request to get an HTTP response. It works in | ||
| // the same way as DoWithAuth, but will not retry the request if it fails with | ||
| // an authorization error. | ||
| func (c *Client) DoWithAuthNoRetry(remote string, access creds.Access, req *http.Request) (*http.Response, error) { | ||
| count := 0 | ||
| return c.doWithAuth(remote, &count, access, req, nil) | ||
| return c.doWithAuth(remote, access, req, nil) | ||
| } | ||
|
|
||
| // DoAPIRequestWithAuth sends an HTTP request to get an HTTP response similarly | ||
|
|
@@ -60,11 +82,7 @@ func (c *Client) DoAPIRequestWithAuth(remote string, req *http.Request) (*http.R | |
| return c.DoWithAuth(remote, access, req) | ||
| } | ||
|
|
||
| func (c *Client) doWithAuth(remote string, count *int, access creds.Access, req *http.Request, via []*http.Request) (*http.Response, error) { | ||
| if *count == defaultMaxAuthAttempts { | ||
| return nil, fmt.Errorf("too many authentication attempts") | ||
| } | ||
|
|
||
| func (c *Client) doWithAuth(remote string, access creds.Access, req *http.Request, via []*http.Request) (*http.Response, error) { | ||
| req.Header = c.client.ExtraHeadersFor(req) | ||
|
|
||
| credWrapper, err := c.getCreds(remote, access, req) | ||
|
|
@@ -73,27 +91,23 @@ func (c *Client) doWithAuth(remote string, count *int, access creds.Access, req | |
| } | ||
| c.credContext.SetStateFields(credWrapper.Creds["state[]"]) | ||
|
|
||
| res, err := c.doWithCreds(req, count, credWrapper, access, via) | ||
| if err != nil { | ||
| if errors.IsAuthError(err) { | ||
| multistage := credWrapper.Creds.IsMultistage() | ||
| newMode, newModes, headers := getAuthAccess(res, access.Mode(), c.access, multistage) | ||
| newAccess := access.Upgrade(newMode) | ||
| if newAccess.Mode() != access.Mode() { | ||
| c.Endpoints.SetAccess(newAccess) | ||
| c.access = newModes | ||
| } | ||
| res, err := c.doWithCreds(req, credWrapper, access, via) | ||
| if err != nil && errors.IsAuthError(err) { | ||
| multistage := credWrapper.Creds.IsMultistage() | ||
| newMode, newModes, headers := getAuthAccess(res, access.Mode(), c.access, multistage) | ||
| newAccess := access.Upgrade(newMode) | ||
| if newAccess.Mode() != access.Mode() { | ||
| c.Endpoints.SetAccess(newAccess) | ||
| c.access = newModes | ||
| } | ||
|
|
||
| if credWrapper.Creds != nil { | ||
| req.Header.Del("Authorization") | ||
| if multistage && *count < defaultMaxAuthAttempts && res != nil && res.StatusCode == 401 { | ||
| *count++ | ||
| } else { | ||
| credWrapper.CredentialHelper.Reject(credWrapper.Creds) | ||
| } | ||
| if credWrapper.Creds != nil { | ||
| req.Header.Del("Authorization") | ||
| if !multistage { | ||
| credWrapper.CredentialHelper.Reject(credWrapper.Creds) | ||
| } | ||
| c.credContext.SetWWWAuthHeaders(headers) | ||
| } | ||
| c.credContext.SetWWWAuthHeaders(headers) | ||
| } | ||
|
|
||
| if res != nil && res.StatusCode < 300 && res.StatusCode > 199 { | ||
|
|
@@ -103,7 +117,7 @@ func (c *Client) doWithAuth(remote string, count *int, access creds.Access, req | |
| return res, err | ||
| } | ||
|
|
||
| func (c *Client) doWithCreds(req *http.Request, count *int, credWrapper creds.CredentialHelperWrapper, access creds.Access, via []*http.Request) (*http.Response, error) { | ||
| func (c *Client) doWithCreds(req *http.Request, credWrapper creds.CredentialHelperWrapper, access creds.Access, via []*http.Request) (*http.Response, error) { | ||
| if access.Mode() == creds.NegotiateAccess { | ||
| return c.doWithNegotiate(req, credWrapper) | ||
| } | ||
|
|
@@ -124,7 +138,7 @@ func (c *Client) doWithCreds(req *http.Request, count *int, credWrapper creds.Cr | |
| return res, errors.New(tr.Tr.Get("failed to redirect request")) | ||
| } | ||
|
|
||
| return c.doWithAuth("", count, access, redirectedReq, via) | ||
| return c.doWithAuth("", access, redirectedReq, via) | ||
| } | ||
|
|
||
| // getCreds fills the authorization header for the given request if possible, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.