diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 91374aa15..ce84c6a36 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)) { + 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 6a4d3aa3b..fd8aae628 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -403,3 +403,8 @@ func FindFileInDir(dirPath, fileName string) (string, error) { } return filePath, nil } + +// SanitizeDir sanitizes the input string to make it a valid directory. +func SanitizeDir(destDir string) string { + return strings.ReplaceAll(destDir, "'", "''") +} 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) + }) + } +}