From ad9559c66a5b89bae4c91d493a60d63a17494e24 Mon Sep 17 00:00:00 2001 From: Michael Borkenstein Date: Tue, 22 Oct 2019 10:41:44 -0500 Subject: [PATCH] AUTH-2173: Prepends access login url with scheme if one doesnt exist --- cmd/cloudflared/access/carrier.go | 2 +- cmd/cloudflared/access/cmd.go | 15 +++++++++++++-- cmd/cloudflared/access/cmd_test.go | 25 +++++++++++++++++++++++++ cmd/cloudflared/transfer/transfer.go | 2 +- 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 cmd/cloudflared/access/cmd_test.go diff --git a/cmd/cloudflared/access/carrier.go b/cmd/cloudflared/access/carrier.go index 108bad37..881e0c29 100644 --- a/cmd/cloudflared/access/carrier.go +++ b/cmd/cloudflared/access/carrier.go @@ -23,7 +23,7 @@ func ssh(c *cli.Context) error { if err != nil || rawHostName == "" { return cli.ShowCommandHelp(c, "ssh") } - originURL := "https://" + hostname + originURL := ensureURLScheme(hostname) // get the headers from the cmdline and add them headers := buildRequestHeaders(c.StringSlice(sshHeaderFlag)) diff --git a/cmd/cloudflared/access/cmd.go b/cmd/cloudflared/access/cmd.go index 53b140b8..f1c01fe5 100644 --- a/cmd/cloudflared/access/cmd.go +++ b/cmd/cloudflared/access/cmd.go @@ -191,7 +191,8 @@ func login(c *cli.Context) error { raven.SetDSN(sentryDSN) logger := log.CreateLogger() args := c.Args() - appURL, err := url.Parse(args.First()) + rawURL := ensureURLScheme(args.First()) + appURL, err := url.Parse(rawURL) if args.Len() < 1 || err != nil { logger.Errorf("Please provide the url of the Access application\n") return err @@ -211,6 +212,16 @@ func login(c *cli.Context) error { return nil } +// ensureURLScheme prepends a URL with https:// if it doesnt have a scheme. http:// URLs will not be converted. +func ensureURLScheme(url string) string { + url = strings.Replace(strings.ToLower(url), "http://", "https://", 1) + if !strings.HasPrefix(url, "https://") { + url = fmt.Sprintf("https://%s", url) + + } + return url +} + // curl provides a wrapper around curl, passing Access JWT along in request func curl(c *cli.Context) error { raven.SetDSN(sentryDSN) @@ -294,7 +305,7 @@ func sshGen(c *cli.Context) error { return cli.ShowCommandHelp(c, "ssh-gen") } - originURL, err := url.Parse("https://" + hostname) + originURL, err := url.Parse(ensureURLScheme(hostname)) if err != nil { return err } diff --git a/cmd/cloudflared/access/cmd_test.go b/cmd/cloudflared/access/cmd_test.go new file mode 100644 index 00000000..652be56d --- /dev/null +++ b/cmd/cloudflared/access/cmd_test.go @@ -0,0 +1,25 @@ +package access + +import "testing" + +func Test_ensureURLScheme(t *testing.T) { + type args struct { + url string + } + tests := []struct { + name string + args args + want string + }{ + {"no scheme", args{"localhost:123"}, "https://localhost:123"}, + {"http scheme", args{"http://test"}, "https://test"}, + {"https scheme", args{"https://test"}, "https://test"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ensureURLScheme(tt.args.url); got != tt.want { + t.Errorf("ensureURLScheme() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cmd/cloudflared/transfer/transfer.go b/cmd/cloudflared/transfer/transfer.go index 10be1a04..dbe5536a 100644 --- a/cmd/cloudflared/transfer/transfer.go +++ b/cmd/cloudflared/transfer/transfer.go @@ -45,7 +45,7 @@ func Run(transferURL *url.URL, resourceName, key, value, path string, shouldEncr if err != nil { fmt.Fprintf(os.Stderr, "Please open the following URL and log in with your Cloudflare account:\n\n%s\n\nLeave cloudflared running to download the %s automatically.\n", requestURL, resourceName) } else { - fmt.Fprintf(os.Stderr, "A browser window should have opened at the following URL:\n\n%s\n\nIf the browser failed to open, open it yourself and visit the URL above.\n", requestURL) + fmt.Fprintf(os.Stderr, "A browser window should have opened at the following URL:\n\n%s\n\nIf the browser failed to open, please visit the URL above directly in your browser.\n", requestURL) } var resourceData []byte