From 2827b2fe8fe1283b0f0240dfdfdb194527115a46 Mon Sep 17 00:00:00 2001 From: gofastasf Date: Tue, 1 Apr 2025 22:29:43 +0530 Subject: [PATCH] fix: Use path and filepath operation appropriately Using path package methods can cause errors on windows machines. path methods are used for url operations and unix specific operation. filepath methods are used for file system paths and its cross platform. Remove strings.HasSuffix and use filepath.Ext and path.Ext for file and url extenstions respectively. --- cmd/cloudflared/service_template.go | 4 ++-- cmd/cloudflared/tunnel/subcommand_context.go | 2 +- cmd/cloudflared/updater/workers_update.go | 6 +++--- credentials/credentials_test.go | 4 ++-- credentials/origin_cert_test.go | 6 +++--- logger/create.go | 3 +-- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cmd/cloudflared/service_template.go b/cmd/cloudflared/service_template.go index c2f0d3cf..725d1f86 100644 --- a/cmd/cloudflared/service_template.go +++ b/cmd/cloudflared/service_template.go @@ -7,7 +7,7 @@ import ( "io" "os" "os/exec" - "path" + "path/filepath" "text/template" homedir "github.com/mitchellh/go-homedir" @@ -57,7 +57,7 @@ func (st *ServiceTemplate) Generate(args *ServiceTemplateArgs) error { fileMode = st.FileMode } - plistFolder := path.Dir(resolvedPath) + plistFolder := filepath.Dir(resolvedPath) err = os.MkdirAll(plistFolder, 0o755) if err != nil { return fmt.Errorf("error creating %s: %v", plistFolder, err) diff --git a/cmd/cloudflared/tunnel/subcommand_context.go b/cmd/cloudflared/tunnel/subcommand_context.go index 99403141..205fe51c 100644 --- a/cmd/cloudflared/tunnel/subcommand_context.go +++ b/cmd/cloudflared/tunnel/subcommand_context.go @@ -112,7 +112,7 @@ func (sc *subcommandContext) readTunnelCredentials(credFinder CredFinder) (conne var credentials connection.Credentials if err = json.Unmarshal(body, &credentials); err != nil { - if strings.HasSuffix(filePath, ".pem") { + if filepath.Ext(filePath) == ".pem" { return connection.Credentials{}, fmt.Errorf("The tunnel credentials file should be .json but you gave a .pem. " + "The tunnel credentials file was originally created by `cloudflared tunnel create`. " + "You may have accidentally used the filepath to cert.pem, which is generated by `cloudflared tunnel " + diff --git a/cmd/cloudflared/updater/workers_update.go b/cmd/cloudflared/updater/workers_update.go index b7a86ff1..1ab5062f 100644 --- a/cmd/cloudflared/updater/workers_update.go +++ b/cmd/cloudflared/updater/workers_update.go @@ -10,9 +10,9 @@ import ( "net/url" "os" "os/exec" + "path" "path/filepath" "runtime" - "strings" "text/template" "time" @@ -198,7 +198,7 @@ func download(url, filepath string, isCompressed bool) error { // isCompressedFile is a really simple file extension check to see if this is a macos tar and gzipped func isCompressedFile(urlstring string) bool { - if strings.HasSuffix(urlstring, ".tgz") { + if path.Ext(urlstring) == ".tgz" { return true } @@ -206,7 +206,7 @@ func isCompressedFile(urlstring string) bool { if err != nil { return false } - return strings.HasSuffix(u.Path, ".tgz") + return path.Ext(u.Path) == ".tgz" } // writeBatchFile writes a batch file out to disk diff --git a/credentials/credentials_test.go b/credentials/credentials_test.go index d9b2d7b7..8132b196 100644 --- a/credentials/credentials_test.go +++ b/credentials/credentials_test.go @@ -3,7 +3,7 @@ package credentials import ( "io/fs" "os" - "path" + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -13,7 +13,7 @@ func TestCredentialsRead(t *testing.T) { file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem") require.NoError(t, err) dir := t.TempDir() - certPath := path.Join(dir, originCertFile) + certPath := filepath.Join(dir, originCertFile) os.WriteFile(certPath, file, fs.ModePerm) user, err := Read(certPath, &nopLog) require.NoError(t, err) diff --git a/credentials/origin_cert_test.go b/credentials/origin_cert_test.go index 7e2a90a0..4f3a3b02 100644 --- a/credentials/origin_cert_test.go +++ b/credentials/origin_cert_test.go @@ -4,7 +4,7 @@ import ( "fmt" "io/fs" "os" - "path" + "path/filepath" "testing" "github.com/rs/zerolog" @@ -63,7 +63,7 @@ func TestFindOriginCert_Valid(t *testing.T) { file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem") require.NoError(t, err) dir := t.TempDir() - certPath := path.Join(dir, originCertFile) + certPath := filepath.Join(dir, originCertFile) _ = os.WriteFile(certPath, file, fs.ModePerm) path, err := FindOriginCert(certPath, &nopLog) require.NoError(t, err) @@ -72,7 +72,7 @@ func TestFindOriginCert_Valid(t *testing.T) { func TestFindOriginCert_Missing(t *testing.T) { dir := t.TempDir() - certPath := path.Join(dir, originCertFile) + certPath := filepath.Join(dir, originCertFile) _, err := FindOriginCert(certPath, &nopLog) require.Error(t, err) } diff --git a/logger/create.go b/logger/create.go index 99bcca3c..fdb4495d 100644 --- a/logger/create.go +++ b/logger/create.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "os" - "path" "path/filepath" "sync" "time" @@ -249,7 +248,7 @@ func createRollingLogger(config RollingConfig) (io.Writer, error) { } rotatingFileInit.writer = &lumberjack.Logger{ - Filename: path.Join(config.Dirname, config.Filename), + Filename: filepath.Join(config.Dirname, config.Filename), MaxBackups: config.maxBackups, MaxSize: config.maxSize, MaxAge: config.maxAge,