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

Fix: Preserve HTTP Response in URL Errors #3369

Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 7 additions & 4 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,11 @@
}

resp, err := c.client.Do(req)
var response *Response
if resp != nil {
response = newResponse(resp)
}

if err != nil {
// If we got an error, and the context has been canceled,
// the context's error is probably more useful.
Expand All @@ -854,15 +859,13 @@
if e, ok := err.(*url.Error); ok {
if url, err := url.Parse(e.URL); err == nil {
e.URL = sanitizeURL(url).String()
return nil, e
return response, e
}
}

return nil, err
return response, err

Check warning on line 866 in github/github.go

View check run for this annotation

Codecov / codecov/patch

github/github.go#L866

Added line #L866 was not covered by tests
}

response := newResponse(resp)

// Don't update the rate limits if this was a cached response.
// X-From-Cache is set by https://github.com/gregjones/httpcache
if response.Header.Get("X-From-Cache") == "" {
Expand Down
21 changes: 21 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,27 @@ func TestDo_redirectLoop(t *testing.T) {
}
}

func TestDo_preservesResponseInURLError(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced that this test actually creates a url.Error.
For examples, please see: https://cs.opensource.google/go/go/+/refs/tags/go1.23.4:src/net/url/url_test.go;l=1763-1821

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I guess the original issue was about a 404 error. Renamed the test to HTTPError and made the mock more in line with the other mocks in the file. Also, now it's returning response on line 854 too. Does that look better?

t.Parallel()
client, mux, _ := setup(t)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", http.StatusNotFound)
})

req, _ := client.NewRequest("GET", ".", nil)
resp, err := client.Do(context.Background(), req, nil)

if err == nil {
t.Fatal("Expected error to be returned")
}
if resp == nil {
t.Fatal("Expected response to be returned even with error")
}
if resp.StatusCode != http.StatusNotFound {
t.Errorf("Expected status code 404, got %d", resp.StatusCode)
}
}

// Test that an error caused by the internal http client's Do() function
// does not leak the client secret.
func TestDo_sanitizeURL(t *testing.T) {
Expand Down
Loading