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 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
13 changes: 8 additions & 5 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,28 +841,31 @@
}

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.
select {
case <-ctx.Done():
return nil, ctx.Err()
return response, ctx.Err()

Check warning on line 854 in github/github.go

View check run for this annotation

Codecov / codecov/patch

github/github.go#L854

Added line #L854 was not covered by tests
default:
}

// If the error type is *url.Error, sanitize its URL before returning.
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
45 changes: 45 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,51 @@ func TestDo_redirectLoop(t *testing.T) {
}
}

func TestDo_preservesResponseInHTTPError(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, `{
"message": "Resource not found",
"documentation_url": "https://docs.github.com/rest/reference/repos#get-a-repository"
}`)
})

req, _ := client.NewRequest("GET", ".", nil)
var resp *Response
var data interface{}
resp, err := client.Do(context.Background(), req, &data)

if err == nil {
t.Fatal("Expected error response")
}

// Verify error type and access to status code
errResp, ok := err.(*ErrorResponse)
if !ok {
t.Fatalf("Expected *ErrorResponse error, got %T", err)
}

// Verify status code is accessible from both Response and ErrorResponse
if resp == nil {
t.Fatal("Expected response to be returned even with error")
}
if got, want := resp.StatusCode, http.StatusNotFound; got != want {
t.Errorf("Response status = %d, want %d", got, want)
}
if got, want := errResp.Response.StatusCode, http.StatusNotFound; got != want {
t.Errorf("Error response status = %d, want %d", got, want)
}

// Verify error contains proper message
if !strings.Contains(errResp.Message, "Resource not found") {
t.Errorf("Error message = %q, want to contain 'Resource not found'", errResp.Message)
}
}

// 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