Browse Source

QueryUnescape the tlsConfig name during DSN parsing.

Also add unit test.
Kevin Malachowski 10 năm trước cách đây
mục cha
commit
0874761875
2 tập tin đã thay đổi với 20 bổ sung0 xóa
  1. 2 0
      utils.go
  2. 18 0
      utils_test.go

+ 2 - 0
utils.go

@@ -267,6 +267,8 @@ func parseDSNParams(cfg *config, params string) (err error) {
 				if boolValue {
 					cfg.tls = &tls.Config{}
 				}
+			} else if value, err := url.QueryUnescape(value); err != nil {
+				return fmt.Errorf("Invalid value for tls config name: %v", err)
 			} else {
 				if strings.ToLower(value) == "skip-verify" {
 					cfg.tls = &tls.Config{InsecureSkipVerify: true}

+ 18 - 0
utils_test.go

@@ -13,6 +13,7 @@ import (
 	"crypto/tls"
 	"encoding/binary"
 	"fmt"
+	"net/url"
 	"testing"
 	"time"
 )
@@ -116,6 +117,23 @@ func TestDSNWithCustomTLS(t *testing.T) {
 	DeregisterTLSConfig("utils_test")
 }
 
+func TestDSNWithCustomTLS_queryEscape(t *testing.T) {
+	const configKey = "&%!:"
+	dsn := "user:password@tcp(localhost:5555)/dbname?tls=" + url.QueryEscape(configKey)
+	name := "foohost"
+	tlsCfg := tls.Config{ServerName: name}
+
+	RegisterTLSConfig(configKey, &tlsCfg)
+
+	cfg, err := parseDSN(dsn)
+
+	if err != nil {
+		t.Error(err.Error())
+	} else if cfg.tls.ServerName != name {
+		t.Errorf("Did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, dsn)
+	}
+}
+
 func TestDSNUnsafeCollation(t *testing.T) {
 	_, err := parseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=true")
 	if err != errInvalidDSNUnsafeCollation {