From a961e75c59b360f901020c612deef1e1da8ecfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Libor=20Ondru=C5=A1ek?= <1335068+librucha@users.noreply.github.com> Date: Sun, 10 Sep 2023 17:06:44 +0200 Subject: [PATCH] fixed escaping for postgresql user info (#344) (cherry picked from commit e15fd44f3e2c3a2b95f7d15bbafa1dce5696234a) Co-authored-by: Cyril Gaudin --- postgresql/config.go | 8 ++++---- postgresql/config_test.go | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/postgresql/config.go b/postgresql/config.go index 257c97bf..c4ed30e8 100644 --- a/postgresql/config.go +++ b/postgresql/config.go @@ -11,7 +11,7 @@ import ( "unicode" "github.com/blang/semver" - _ "github.com/lib/pq" //PostgreSQL db + _ "github.com/lib/pq" // PostgreSQL db "gocloud.dev/postgres" _ "gocloud.dev/postgres/awspostgres" _ "gocloud.dev/postgres/gcppostgres" @@ -247,8 +247,8 @@ func (c *Config) connStr(database string) string { connStr := fmt.Sprintf( "%s://%s:%s@%s:%d/%s?%s", c.Scheme, - url.QueryEscape(c.Username), - url.QueryEscape(c.Password), + url.PathEscape(c.Username), + url.PathEscape(c.Password), host, c.Port, database, @@ -304,7 +304,7 @@ func (c *Client) Connect() (*DBConnection, error) { // Version hint not set by user, need to fingerprint version, err = fingerprintCapabilities(db) if err != nil { - db.Close() + _ = db.Close() return nil, fmt.Errorf("error detecting capabilities: %w", err) } } diff --git a/postgresql/config_test.go b/postgresql/config_test.go index 8ba50280..6c9c2a70 100644 --- a/postgresql/config_test.go +++ b/postgresql/config_test.go @@ -45,6 +45,7 @@ func TestConfigConnStr(t *testing.T) { wantDbParams []string }{ {&Config{Scheme: "postgres", Host: "localhost", Port: 5432, Username: "postgres_user", Password: "postgres_password", SSLMode: "disable"}, "postgres://postgres_user:postgres_password@localhost:5432/postgres", []string{"connect_timeout=0", "sslmode=disable"}}, + {&Config{Scheme: "postgres", Host: "localhost", Port: 5432, Username: "spaced user", Password: "spaced password", SSLMode: "disable"}, "postgres://spaced%20user:spaced%20password@localhost:5432/postgres", []string{"connect_timeout=0", "sslmode=disable"}}, } for _, test := range tests {