From c9fc57e2b278dcd8ec5f7702e0a6d48694eb3824 Mon Sep 17 00:00:00 2001 From: mrmtheboss Date: Thu, 22 Jun 2023 23:04:33 +0530 Subject: [PATCH 1/9] Fix for Quote in username errors dapr init #972 Signed-off-by: mrmtheboss --- pkg/standalone/standalone.go | 1 + utils/utils.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 91374aa15..6a2b96ddd 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -905,6 +905,7 @@ func moveFileToPath(filepath string, installLocation string) (string, error) { p := os.Getenv("PATH") if !strings.Contains(strings.ToLower(p), strings.ToLower(destDir)) { + utils.FixCommandWithApostrophe(&destDir) pathCmd := "[System.Environment]::SetEnvironmentVariable('Path',[System.Environment]::GetEnvironmentVariable('Path','user') + '" + fmt.Sprintf(";%s", destDir) + "', 'user')" _, err := utils.RunCmdAndWait("powershell", pathCmd) if err != nil { diff --git a/utils/utils.go b/utils/utils.go index 6a4d3aa3b..a65d21728 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -403,3 +403,18 @@ func FindFileInDir(dirPath, fileName string) (string, error) { } return filePath, nil } + +// FixCommandWithApostrophe corrects the passed command which might have apostrophes in it +func FixCommandWithApostrophe(destDir *string) { + bytes := []byte(*destDir) + var result []byte + + for i := 0; i < len(bytes); i++ { + result = append(result, bytes[i]) + if bytes[i] == '\'' { + result = append(result, '\'') + } + } + + *destDir = string(result) +} From 648b1ecefc07db9c16a466abe99b18588c5fd496 Mon Sep 17 00:00:00 2001 From: mrmtheboss Date: Fri, 23 Jun 2023 02:10:08 +0530 Subject: [PATCH 2/9] addressed a GitHub Actions Check failure Signed-off-by: mrmtheboss --- utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index a65d21728..dfdd34ca4 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -404,7 +404,7 @@ func FindFileInDir(dirPath, fileName string) (string, error) { return filePath, nil } -// FixCommandWithApostrophe corrects the passed command which might have apostrophes in it +// FixCommandWithApostrophe corrects the passed command which might have apostrophes in it. func FixCommandWithApostrophe(destDir *string) { bytes := []byte(*destDir) var result []byte From 69f5b56dcfd94afe63feb4148f84123fefd77381 Mon Sep 17 00:00:00 2001 From: Mohit Pal Singh Date: Fri, 23 Jun 2023 18:44:10 +0530 Subject: [PATCH 3/9] Addressed Shubham's comments Signed-off-by: Mohit Pal Singh --- pkg/standalone/standalone.go | 2 +- utils/utils.go | 16 +++------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 6a2b96ddd..45bdecaa7 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -905,7 +905,7 @@ func moveFileToPath(filepath string, installLocation string) (string, error) { p := os.Getenv("PATH") if !strings.Contains(strings.ToLower(p), strings.ToLower(destDir)) { - utils.FixCommandWithApostrophe(&destDir) + utils.SanitizeDir(&destDir) pathCmd := "[System.Environment]::SetEnvironmentVariable('Path',[System.Environment]::GetEnvironmentVariable('Path','user') + '" + fmt.Sprintf(";%s", destDir) + "', 'user')" _, err := utils.RunCmdAndWait("powershell", pathCmd) if err != nil { diff --git a/utils/utils.go b/utils/utils.go index dfdd34ca4..054a76e2a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -404,17 +404,7 @@ func FindFileInDir(dirPath, fileName string) (string, error) { return filePath, nil } -// FixCommandWithApostrophe corrects the passed command which might have apostrophes in it. -func FixCommandWithApostrophe(destDir *string) { - bytes := []byte(*destDir) - var result []byte - - for i := 0; i < len(bytes); i++ { - result = append(result, bytes[i]) - if bytes[i] == '\'' { - result = append(result, '\'') - } - } - - *destDir = string(result) +// SanitizeDir corrects any syntactical errors in the passed directory. +func SanitizeDir(destDir *string) { + *destDir = strings.Replace(*destDir, "'", "''", -1) } From 29b9c8de56c8ab7f5e51467a370dc94a0c586a55 Mon Sep 17 00:00:00 2001 From: Mohit Pal Singh Date: Fri, 23 Jun 2023 19:52:15 +0530 Subject: [PATCH 4/9] Addressed Shubham's new comments Signed-off-by: Mohit Pal Singh --- pkg/standalone/standalone.go | 2 +- utils/utils.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 45bdecaa7..ce84c6a36 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -905,7 +905,7 @@ func moveFileToPath(filepath string, installLocation string) (string, error) { p := os.Getenv("PATH") if !strings.Contains(strings.ToLower(p), strings.ToLower(destDir)) { - utils.SanitizeDir(&destDir) + destDir = utils.SanitizeDir(destDir) pathCmd := "[System.Environment]::SetEnvironmentVariable('Path',[System.Environment]::GetEnvironmentVariable('Path','user') + '" + fmt.Sprintf(";%s", destDir) + "', 'user')" _, err := utils.RunCmdAndWait("powershell", pathCmd) if err != nil { diff --git a/utils/utils.go b/utils/utils.go index 054a76e2a..1d6ad56a2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -405,6 +405,8 @@ func FindFileInDir(dirPath, fileName string) (string, error) { } // SanitizeDir corrects any syntactical errors in the passed directory. -func SanitizeDir(destDir *string) { - *destDir = strings.Replace(*destDir, "'", "''", -1) +func SanitizeDir(destDir string) string { + correctedDestDir := destDir + correctedDestDir = strings.Replace(correctedDestDir, "'", "''", -1) + return correctedDestDir } From f506e968b747edacf8992941a957d719fc530fd1 Mon Sep 17 00:00:00 2001 From: Mohit Pal Singh Date: Fri, 23 Jun 2023 21:14:12 +0530 Subject: [PATCH 5/9] Addressed a GitHub actions failure Signed-off-by: Mohit Pal Singh --- utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 1d6ad56a2..e9473a100 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -407,6 +407,6 @@ func FindFileInDir(dirPath, fileName string) (string, error) { // SanitizeDir corrects any syntactical errors in the passed directory. func SanitizeDir(destDir string) string { correctedDestDir := destDir - correctedDestDir = strings.Replace(correctedDestDir, "'", "''", -1) + correctedDestDir = strings.ReplaceAll(correctedDestDir, "'", "''") return correctedDestDir } From d58b9e58b55f7ea2ff2fb92f690e4d1d58cca176 Mon Sep 17 00:00:00 2001 From: Mohit Pal Singh Date: Fri, 23 Jun 2023 21:52:41 +0530 Subject: [PATCH 6/9] Update utils/utils.go Co-authored-by: Shubham Sharma Signed-off-by: Mohit Pal Singh --- utils/utils.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index e9473a100..bae7c0cdd 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -406,7 +406,5 @@ func FindFileInDir(dirPath, fileName string) (string, error) { // SanitizeDir corrects any syntactical errors in the passed directory. func SanitizeDir(destDir string) string { - correctedDestDir := destDir - correctedDestDir = strings.ReplaceAll(correctedDestDir, "'", "''") - return correctedDestDir + return strings.ReplaceAll(destDir, "'", "''") } From b29f32e7d3a714ad05a1a8d2024b61bebb9e5247 Mon Sep 17 00:00:00 2001 From: Mohit Pal Singh Date: Fri, 23 Jun 2023 22:30:09 +0530 Subject: [PATCH 7/9] Added Unit test for SanitizeDir() in utils/utils_test.go Signed-off-by: Mohit Pal Singh --- utils/utils_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/utils/utils_test.go b/utils/utils_test.go index 222a8b20e..d89ea4d0a 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -512,3 +512,51 @@ func cleanupTempDir(t *testing.T, fileName string) { err := os.RemoveAll(fileName) assert.NoError(t, err) } + +func TestSanitizeDir(t *testing.T) { + testcases := []struct { + name string + input string + expected string + }{ + { + name: "directory with single quote in three places", + input: "C:\\Use'rs\\sta'rk\\Docum'ents", + expected: "C:\\Use''rs\\sta''rk\\Docum''ents", + }, + { + name: "directory with single quote in two places", + input: "C:\\Use'rs\\sta'rk", + expected: "C:\\Use''rs\\sta''rk", + }, + { + name: "directory with single quote in username", + input: "C:\\Users\\Debash'ish", + expected: "C:\\Users\\Debash''ish", + }, + { + name: "directory with no single quote", + input: "C:\\Users\\Shubham", + expected: "C:\\Users\\Shubham", + }, + { + name: "directory with single quote in one place", + input: "C:\\Use'rs\\Shubham", + expected: "C:\\Use''rs\\Shubham", + }, + { + name: "directory with single quote in many places in username", + input: "C:\\Users\\Shu'bh'am", + expected: "C:\\Users\\Shu''bh''am", + }, + } + + for _, tc := range testcases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + actual := SanitizeDir(tc.input) + assert.Equal(t, tc.expected, actual) + }) + } +} From d47c682bec30d2246dd292826d5e52a79ad83228 Mon Sep 17 00:00:00 2001 From: Mohit Pal Singh Date: Sat, 24 Jun 2023 15:48:55 +0530 Subject: [PATCH 8/9] Addressed a GitHub actions failure Signed-off-by: Mohit Pal Singh --- utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index bae7c0cdd..7f279e2ac 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -406,5 +406,5 @@ func FindFileInDir(dirPath, fileName string) (string, error) { // SanitizeDir corrects any syntactical errors in the passed directory. func SanitizeDir(destDir string) string { - return strings.ReplaceAll(destDir, "'", "''") + return strings.ReplaceAll(destDir, "'", "''") } From 0c45ce1cd78c0e5f20251b10243733818e61e0a5 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Sat, 24 Jun 2023 16:25:25 +0530 Subject: [PATCH 9/9] Update comment. Signed-off-by: Shubham Sharma --- utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 7f279e2ac..fd8aae628 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -404,7 +404,7 @@ func FindFileInDir(dirPath, fileName string) (string, error) { return filePath, nil } -// SanitizeDir corrects any syntactical errors in the passed directory. +// SanitizeDir sanitizes the input string to make it a valid directory. func SanitizeDir(destDir string) string { return strings.ReplaceAll(destDir, "'", "''") }