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 the password parsing issue #38

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,20 @@ func printUsageAndExit() {

func dbMakeConnectionString(driver, user, password, address, name, ssl string) string {
return fmt.Sprintf("%s://%s:%s@%s/%s?sslmode=%s",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using URL.String() to assemble this string. It will apply the required escaping. url.UserPassword() will encapsulate the username and password.

driver, url.QueryEscape(user), url.QueryEscape(password), address, name, ssl,
driver, EscapeIfNeeded(user), EscapeIfNeeded(password), address, name, ssl,
)
}

func EscapeIfNeeded(str string) string {
unescapedStr, err := url.QueryUnescape(str)
if err != nil || unescapedStr == str {
// If the str is already unescaped or an error occurred, escape it
return url.QueryEscape(str)
}
// If the str was successfully unescaped and is different from the original, return the original
return str
}

// Main function of a cli application. It is public for backwards compatibility with `cli` package
func Main(version string) {
help := viper.GetBool("help")
Expand Down
48 changes: 48 additions & 0 deletions internal/cli/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"testing"
)

func TestEscapeIfNeeded(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "AlreadyEscaped",
input: "hello%20world",
expected: "hello%20world",
Comment on lines +15 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flaw with this approach is a set of characters in a password like %20 would not be interpreted correctly.

},
{
name: "Unescaped",
input: "hello world",
expected: "hello+world",
},
{
name: "PartiallyEscaped",
input: "hello%20world!",
expected: "hello%20world!",
},
{
name: "EmptyString",
input: "",
expected: "",
},
{
name: "SpecialCharacters",
input: "[email protected]",
expected: "hello%40world.com",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := EscapeIfNeeded(tt.input)
if result != tt.expected {
t.Errorf("EscapeIfNeeded(%q) actual = %q; want = %q", tt.input, result, tt.expected)
}
})
}
}
Loading