Skip to content

Commit 17b628d

Browse files
committed
pgconn: ignore explicit empty user in connection string
1 parent c0725c8 commit 17b628d

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

pgconn/config.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"net"
1414
"net/url"
1515
"os"
16-
"os/user"
1716
"path/filepath"
1817
"strconv"
1918
"strings"
@@ -305,13 +304,6 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con
305304
},
306305
}
307306

308-
if _, present := settings["user"]; present && config.User == "" {
309-
currentUser, err := user.Current()
310-
if err == nil {
311-
config.User = currentUser.Username
312-
}
313-
}
314-
315307
if connectTimeoutSetting, present := settings["connect_timeout"]; present {
316308
connectTimeout, err := parseConnectTimeoutSetting(connectTimeoutSetting)
317309
if err != nil {
@@ -499,7 +491,9 @@ func parseURLSettings(connString string) (map[string]string, error) {
499491
}
500492

501493
if parsedURL.User != nil {
502-
settings["user"] = parsedURL.User.Username()
494+
if u := parsedURL.User.Username(); u != "" {
495+
settings["user"] = u
496+
}
503497
if password, present := parsedURL.User.Password(); present {
504498
settings["password"] = password
505499
}
@@ -626,6 +620,9 @@ func parseKeywordValueSettings(s string) (map[string]string, error) {
626620
return nil, errors.New("invalid keyword/value")
627621
}
628622

623+
if key == "user" && val == "" {
624+
continue
625+
}
629626
settings[key] = val
630627
}
631628

pgconn/config_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,10 +1161,25 @@ func TestParseConfigExplicitEmptyUserDefaultsToOSUser(t *testing.T) {
11611161
expected string
11621162
}{
11631163
{
1164-
name: "explicit empty user parameter",
1164+
name: "keyword value explicit empty user",
11651165
connString: "host=localhost dbname=test user=",
11661166
expected: currentUser.Username,
11671167
},
1168+
{
1169+
name: "keyword value quoted empty user",
1170+
connString: "host=localhost dbname=test user=''",
1171+
expected: currentUser.Username,
1172+
},
1173+
{
1174+
name: "url explicit empty user without password",
1175+
connString: "postgres://@localhost/test",
1176+
expected: currentUser.Username,
1177+
},
1178+
{
1179+
name: "url explicit empty user with password",
1180+
connString: "postgres://:secret@localhost/test",
1181+
expected: currentUser.Username,
1182+
},
11681183
}
11691184

11701185
for i, tt := range tests {

0 commit comments

Comments
 (0)