diff --git a/github/repos_contents.go b/github/repos_contents.go index 431d4b12dc..be58fd52f6 100644 --- a/github/repos_contents.go +++ b/github/repos_contents.go @@ -317,5 +317,9 @@ func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo st } parsedURL, err := url.Parse(resp.Header.Get("Location")) - return parsedURL, newResponse(resp), err + if err != nil { + return nil, newResponse(resp), err + } + + return parsedURL, newResponse(resp), nil } diff --git a/github/repos_contents_test.go b/github/repos_contents_test.go index e0465e93d0..1cfa100df3 100644 --- a/github/repos_contents_test.go +++ b/github/repos_contents_test.go @@ -744,6 +744,23 @@ func TestRepositoriesService_GetArchiveLink_StatusMovedPermanently_followRedirec } } +func TestRepositoriesService_GetArchiveLink_invalidLocationHeader(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/tarball", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + ctlChar := 0x7f + badURL := "https://google.com" + string(byte(ctlChar)) + w.Header().Add("Location", badURL) + w.WriteHeader(http.StatusFound) + }) + + ctx := context.Background() + _, _, err := client.Repositories.GetArchiveLink(ctx, "o", "r", Tarball, &RepositoryContentGetOptions{}, false) + testURLParseError(t, err) +} + func TestRepositoriesService_GetContents_NoTrailingSlashInDirectoryApiPath(t *testing.T) { client, mux, _, teardown := setup() defer teardown()