Skip to content

Commit 0874761

Browse files
committed
QueryUnescape the tlsConfig name during DSN parsing.
Also add unit test.
1 parent fd85cc8 commit 0874761

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

utils.go

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ func parseDSNParams(cfg *config, params string) (err error) {
267267
if boolValue {
268268
cfg.tls = &tls.Config{}
269269
}
270+
} else if value, err := url.QueryUnescape(value); err != nil {
271+
return fmt.Errorf("Invalid value for tls config name: %v", err)
270272
} else {
271273
if strings.ToLower(value) == "skip-verify" {
272274
cfg.tls = &tls.Config{InsecureSkipVerify: true}

utils_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"crypto/tls"
1414
"encoding/binary"
1515
"fmt"
16+
"net/url"
1617
"testing"
1718
"time"
1819
)
@@ -116,6 +117,23 @@ func TestDSNWithCustomTLS(t *testing.T) {
116117
DeregisterTLSConfig("utils_test")
117118
}
118119

120+
func TestDSNWithCustomTLS_queryEscape(t *testing.T) {
121+
const configKey = "&%!:"
122+
dsn := "user:password@tcp(localhost:5555)/dbname?tls=" + url.QueryEscape(configKey)
123+
name := "foohost"
124+
tlsCfg := tls.Config{ServerName: name}
125+
126+
RegisterTLSConfig(configKey, &tlsCfg)
127+
128+
cfg, err := parseDSN(dsn)
129+
130+
if err != nil {
131+
t.Error(err.Error())
132+
} else if cfg.tls.ServerName != name {
133+
t.Errorf("Did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, dsn)
134+
}
135+
}
136+
119137
func TestDSNUnsafeCollation(t *testing.T) {
120138
_, err := parseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=true")
121139
if err != errInvalidDSNUnsafeCollation {

0 commit comments

Comments
 (0)