Skip to content

Commit 44fa292

Browse files
pivotal-rebecca-chinjulienschmidt
pivotal-rebecca-chin
authored andcommitted
utils: don't mutate registered tls configs (go-sql-driver#600)
Fixes go-sql-driver#536
1 parent 382e13d commit 44fa292

7 files changed

+84
-0
lines changed

AUTHORS

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Carlos Nieto <jose.carlos at menteslibres.net>
1818
Chris Moos <chris at tech9computers.com>
1919
Daniel Nichter <nil at codenode.com>
2020
Daniël van Eeden <git at myname.nl>
21+
Dave Protasowski <dprotaso at gmail.com>
2122
DisposaBoy <disposaboy at dby.me>
2223
Egor Smolyakov <egorsmkv at gmail.com>
2324
Frederick Mayle <frederickmayle at gmail.com>
@@ -46,6 +47,7 @@ Nicola Peduzzi <thenikso at gmail.com>
4647
Olivier Mengué <dolmen at cpan.org>
4748
Paul Bonser <misterpib at gmail.com>
4849
Peter Schultz <peter.schultz at classmarkets.com>
50+
Rebecca Chin <rchin at pivotal.io>
4951
Runrioter Wung <runrioter at gmail.com>
5052
Soroush Pour <me at soroushjp.com>
5153
Stan Putrya <root.vagner at gmail.com>
@@ -59,4 +61,5 @@ Zhenye Xie <xiezhenye at gmail.com>
5961

6062
Barracuda Networks, Inc.
6163
Google Inc.
64+
Pivotal Inc.
6265
Stripe Inc.

dsn.go

+2
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ func parseDSNParams(cfg *Config, params string) (err error) {
511511
}
512512

513513
if tlsConfig, ok := tlsConfigRegister[name]; ok {
514+
tlsConfig = cloneTLSConfig(tlsConfig)
515+
514516
if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify {
515517
host, _, err := net.SplitHostPort(cfg.Addr)
516518
if err == nil {

dsn_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ func TestDSNWithCustomTLS(t *testing.T) {
159159
t.Error(err.Error())
160160
} else if cfg.tls.ServerName != name {
161161
t.Errorf("did not get the correct ServerName (%s) parsing DSN (%s).", name, tst)
162+
} else if tlsCfg.ServerName != "" {
163+
t.Errorf("tlsCfg was mutated ServerName (%s) should be empty parsing DSN (%s).", name, tst)
162164
}
163165

164166
DeregisterTLSConfig("utils_test")

utils.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var (
2626
// RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
2727
// Use the key as a value in the DSN where tls=value.
2828
//
29+
// Note: The tls.Config provided to needs to be exclusively owned by the driver after registering.
30+
//
2931
// rootCertPool := x509.NewCertPool()
3032
// pem, err := ioutil.ReadFile("/path/ca-cert.pem")
3133
// if err != nil {

utils_go17.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
// +build go1.7
10+
// +build !go1.8
11+
12+
package mysql
13+
14+
import "crypto/tls"
15+
16+
func cloneTLSConfig(c *tls.Config) *tls.Config {
17+
return &tls.Config{
18+
Rand: c.Rand,
19+
Time: c.Time,
20+
Certificates: c.Certificates,
21+
NameToCertificate: c.NameToCertificate,
22+
GetCertificate: c.GetCertificate,
23+
RootCAs: c.RootCAs,
24+
NextProtos: c.NextProtos,
25+
ServerName: c.ServerName,
26+
ClientAuth: c.ClientAuth,
27+
ClientCAs: c.ClientCAs,
28+
InsecureSkipVerify: c.InsecureSkipVerify,
29+
CipherSuites: c.CipherSuites,
30+
PreferServerCipherSuites: c.PreferServerCipherSuites,
31+
SessionTicketsDisabled: c.SessionTicketsDisabled,
32+
SessionTicketKey: c.SessionTicketKey,
33+
ClientSessionCache: c.ClientSessionCache,
34+
MinVersion: c.MinVersion,
35+
MaxVersion: c.MaxVersion,
36+
CurvePreferences: c.CurvePreferences,
37+
DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled,
38+
Renegotiation: c.Renegotiation,
39+
}
40+
}

utils_go18.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
// +build go1.8
10+
11+
package mysql
12+
13+
import "crypto/tls"
14+
15+
func cloneTLSConfig(c *tls.Config) *tls.Config {
16+
return c.Clone()
17+
}

utils_legacy.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
// +build !go1.7
10+
11+
package mysql
12+
13+
import "crypto/tls"
14+
15+
func cloneTLSConfig(c *tls.Config) *tls.Config {
16+
clone := *c
17+
return &clone
18+
}

0 commit comments

Comments
 (0)