Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Quote in username errors dapr init #972 #1322

Merged
merged 9 commits into from
Jun 26, 2023
1 change: 1 addition & 0 deletions pkg/standalone/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,8 @@
}
return filePath, nil
}

// SanitizeDir corrects any syntactical errors in the passed directory.
shubham1172 marked this conversation as resolved.
Show resolved Hide resolved
func SanitizeDir(destDir string) string {
return strings.ReplaceAll(destDir, "'", "''")

Check failure on line 409 in utils/utils.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

File is not `gofmt`-ed with `-s` (gofmt)
}
48 changes: 48 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Loading