From f2b3fd88bee96204981a8eb0a11d4ef6babebf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 7 Feb 2019 02:32:30 +0100 Subject: [PATCH] Avoid crashing on invalid GitHub hostname Fixes #2022 --- features/create.feature | 25 +++++++++++++++++++++++++ github/client.go | 6 ++++-- github/config.go | 8 ++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/features/create.feature b/features/create.feature index 83475d0a..f8268662 100644 --- a/features/create.feature +++ b/features/create.feature @@ -274,3 +274,28 @@ Feature: hub create < Location: http://disney.com {"full_name":"mislav/dotfiles"}\n """ + + Scenario: Create Enterprise repo + Given I am "nsartor" on git.my.org with OAuth token "FITOKEN" + Given the GitHub API server: + """ + post('/api/v3/user/repos', :host_name => 'git.my.org') { + assert :private => false + status 201 + json :full_name => 'nsartor/dotfiles' + } + """ + And $GITHUB_HOST is "git.my.org" + When I successfully run `hub create` + Then the url for "origin" should be "git@git.my.org:nsartor/dotfiles.git" + And the output should contain exactly "https://git.my.org/nsartor/dotfiles\n" + + Scenario: Invalid GITHUB_HOST + Given I am "nsartor" on {} with OAuth token "FITOKEN" + And $GITHUB_HOST is "{}" + When I run `hub create` + Then the exit status should be 1 + And the stderr should contain exactly: + """ + invalid hostname: "{}"\n + """ diff --git a/github/client.go b/github/client.go index 8fe03208..566612cb 100644 --- a/github/client.go +++ b/github/client.go @@ -992,8 +992,10 @@ func (client *Client) apiClient() *simpleClient { } func (client *Client) absolute(host string) *url.URL { - u, _ := url.Parse("https://" + host + "/") - if client.Host != nil && client.Host.Protocol != "" { + u, err := url.Parse("https://" + host + "/") + if err != nil { + panic(err) + } else if client.Host != nil && client.Host.Protocol != "" { u.Scheme = client.Host.Protocol } return u diff --git a/github/config.go b/github/config.go index 92e0e1e4..56a7d677 100644 --- a/github/config.go +++ b/github/config.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io/ioutil" + "net/url" "os" "os/signal" "path/filepath" @@ -40,6 +41,13 @@ func (c *Config) PromptForHost(host string) (h *Host, err error) { token := c.DetectToken() tokenFromEnv := token != "" + if host != GitHubHost { + if _, e := url.Parse("https://" + host); e != nil { + err = fmt.Errorf("invalid hostname: %q", host) + return + } + } + h = c.Find(host) if h != nil { if h.User == "" { -- GitLab