Fix postgresql host-v6 literals (#46876)

Ipv6 literals in postgresql hosts previously worked and were subsequently
broken. This change fixes the parsing and adds additional test cases.
This commit is contained in:
felixdoerre
2022-03-31 12:45:49 +02:00
committed by GitHub
parent 0d87de153a
commit f5a790e291
2 changed files with 53 additions and 8 deletions

View File

@@ -122,18 +122,36 @@ func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string
host = dsInfo.URL
logger.Debug("Generating connection string with Unix socket specifier", "socket", host)
} else {
index := strings.LastIndex(dsInfo.URL, ":")
v6Index := strings.Index(dsInfo.URL, "]")
sp := strings.SplitN(dsInfo.URL, ":", 2)
host = sp[0]
if len(sp) > 1 {
var err error
port, err = strconv.Atoi(sp[1])
if err != nil {
return "", errutil.Wrapf(err, "invalid port in host specifier %q", sp[1])
}
if v6Index == -1 {
if len(sp) > 1 {
var err error
port, err = strconv.Atoi(sp[1])
if err != nil {
return "", errutil.Wrapf(err, "invalid port in host specifier %q", sp[1])
}
logger.Debug("Generating connection string with network host/port pair", "host", host, "port", port)
logger.Debug("Generating connection string with network host/port pair", "host", host, "port", port)
} else {
logger.Debug("Generating connection string with network host", "host", host)
}
} else {
logger.Debug("Generating connection string with network host", "host", host)
if index == v6Index+1 {
host = dsInfo.URL[0:index]
var err error
port, err = strconv.Atoi(dsInfo.URL[index+1:])
if err != nil {
return "", errutil.Wrapf(err, "invalid port in host specifier %q", dsInfo.URL[index+1:])
}
logger.Debug("Generating ipv6 connection string with network host/port pair", "host", host, "port", port)
} else {
host = dsInfo.URL
logger.Debug("Generating ipv6 connection string with network host", "host", host)
}
}
}

View File

@@ -67,6 +67,24 @@ func TestGenerateConnectionString(t *testing.T) {
tlsSettings: tlsSettings{Mode: "verify-full"},
expConnStr: "user='user' password='password' host='host' dbname='database' port=1234 sslmode='verify-full'",
},
{
desc: "Ipv6 host",
host: "[::1]",
user: "user",
password: "password",
database: "database",
tlsSettings: tlsSettings{Mode: "verify-full"},
expConnStr: "user='user' password='password' host='[::1]' dbname='database' sslmode='verify-full'",
},
{
desc: "Ipv6/port host",
host: "[::1]:1234",
user: "user",
password: "password",
database: "database",
tlsSettings: tlsSettings{Mode: "verify-full"},
expConnStr: "user='user' password='password' host='[::1]' dbname='database' port=1234 sslmode='verify-full'",
},
{
desc: "Invalid port",
host: "host:invalid",
@@ -84,6 +102,15 @@ func TestGenerateConnectionString(t *testing.T) {
tlsSettings: tlsSettings{Mode: "verify-full"},
expConnStr: `user='user' password='p\'\\assword' host='host' dbname='database' sslmode='verify-full'`,
},
{
desc: "User/DB with single quote and backslash",
host: "host",
user: `u'\ser`,
password: `password`,
database: `d'\atabase`,
tlsSettings: tlsSettings{Mode: "verify-full"},
expConnStr: `user='u\'\\ser' password='password' host='host' dbname='d\'\\atabase' sslmode='verify-full'`,
},
{
desc: "Custom TLS mode disabled",
host: "host",