Skip to content

Commit a961e75

Browse files
libruchacyrilgdn
andauthored
fixed escaping for postgresql user info (#344)
(cherry picked from commit e15fd44) Co-authored-by: Cyril Gaudin <[email protected]>
1 parent e7499ce commit a961e75

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

postgresql/config.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"unicode"
1212

1313
"github.com/blang/semver"
14-
_ "github.com/lib/pq" //PostgreSQL db
14+
_ "github.com/lib/pq" // PostgreSQL db
1515
"gocloud.dev/postgres"
1616
_ "gocloud.dev/postgres/awspostgres"
1717
_ "gocloud.dev/postgres/gcppostgres"
@@ -247,8 +247,8 @@ func (c *Config) connStr(database string) string {
247247
connStr := fmt.Sprintf(
248248
"%s://%s:%s@%s:%d/%s?%s",
249249
c.Scheme,
250-
url.QueryEscape(c.Username),
251-
url.QueryEscape(c.Password),
250+
url.PathEscape(c.Username),
251+
url.PathEscape(c.Password),
252252
host,
253253
c.Port,
254254
database,
@@ -304,7 +304,7 @@ func (c *Client) Connect() (*DBConnection, error) {
304304
// Version hint not set by user, need to fingerprint
305305
version, err = fingerprintCapabilities(db)
306306
if err != nil {
307-
db.Close()
307+
_ = db.Close()
308308
return nil, fmt.Errorf("error detecting capabilities: %w", err)
309309
}
310310
}

postgresql/config_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestConfigConnStr(t *testing.T) {
4545
wantDbParams []string
4646
}{
4747
{&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"}},
48+
{&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"}},
4849
}
4950

5051
for _, test := range tests {

0 commit comments

Comments
 (0)