Skip to content

Commit 37b91d8

Browse files
elemountjulienschmidt
authored andcommitted
Mark AllowNativePassword in DSN param string be true by default. (go-sql-driver#644)
1 parent 3955978 commit 37b91d8

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Paul Bonser <misterpib at gmail.com>
5656
Peter Schultz <peter.schultz at classmarkets.com>
5757
Rebecca Chin <rchin at pivotal.io>
5858
Runrioter Wung <runrioter at gmail.com>
59+
Shuode Li <elemount at qq.com>
5960
Soroush Pour <me at soroushjp.com>
6061
Stan Putrya <root.vagner at gmail.com>
6162
Stanley Gunawan <gunawan.stanley at gmail.com>

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ Default: false
138138
```
139139
Type: bool
140140
Valid Values: true, false
141-
Default: false
141+
Default: true
142142
```
143-
`allowNativePasswords=true` allows the usage of the mysql native password method.
143+
`allowNativePasswords=false` disallows the usage of MySQL native password method.
144144

145145
##### `allowOldPasswords`
146146

dsn.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ func (cfg *Config) FormatDSN() string {
104104
}
105105
}
106106

107-
if cfg.AllowNativePasswords {
107+
if !cfg.AllowNativePasswords {
108108
if hasParam {
109-
buf.WriteString("&allowNativePasswords=true")
109+
buf.WriteString("&allowNativePasswords=false")
110110
} else {
111111
hasParam = true
112-
buf.WriteString("?allowNativePasswords=true")
112+
buf.WriteString("?allowNativePasswords=false")
113113
}
114114
}
115115

@@ -284,8 +284,9 @@ func (cfg *Config) FormatDSN() string {
284284
func ParseDSN(dsn string) (cfg *Config, err error) {
285285
// New config with some default values
286286
cfg = &Config{
287-
Loc: time.UTC,
288-
Collation: defaultCollation,
287+
Loc: time.UTC,
288+
Collation: defaultCollation,
289+
AllowNativePasswords: true,
289290
}
290291

291292
// [user[:password]@][net[(addr)]]/dbname[?param1=value1&paramN=valueN]

dsn_test.go

+20-16
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,49 @@ var testDSNs = []struct {
2222
out *Config
2323
}{{
2424
"username:password@protocol(address)/dbname?param=value",
25-
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC},
25+
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
2626
}, {
2727
"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true",
28-
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, ColumnsWithAlias: true},
28+
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true, ColumnsWithAlias: true},
2929
}, {
3030
"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true&multiStatements=true",
31-
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, ColumnsWithAlias: true, MultiStatements: true},
31+
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true, ColumnsWithAlias: true, MultiStatements: true},
3232
}, {
3333
"user@unix(/path/to/socket)/dbname?charset=utf8",
34-
&Config{User: "user", Net: "unix", Addr: "/path/to/socket", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC},
34+
&Config{User: "user", Net: "unix", Addr: "/path/to/socket", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
3535
}, {
3636
"user:password@tcp(localhost:5555)/dbname?charset=utf8&tls=true",
37-
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, TLSConfig: "true"},
37+
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true, TLSConfig: "true"},
3838
}, {
3939
"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
40-
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8mb4,utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, TLSConfig: "skip-verify"},
40+
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8mb4,utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true, TLSConfig: "skip-verify"},
4141
}, {
4242
"user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci&maxAllowedPacket=16777216",
43-
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8mb4_unicode_ci", Loc: time.UTC, Timeout: 30 * time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, AllowAllFiles: true, AllowOldPasswords: true, ClientFoundRows: true, MaxAllowedPacket: 16777216},
43+
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8mb4_unicode_ci", Loc: time.UTC, AllowNativePasswords: true, Timeout: 30 * time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, AllowAllFiles: true, AllowOldPasswords: true, ClientFoundRows: true, MaxAllowedPacket: 16777216},
44+
}, {
45+
"user:password@/dbname?allowNativePasswords=false",
46+
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: false},
4447
}, {
4548
"user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local",
46-
&Config{User: "user", Passwd: "p@ss(word)", Net: "tcp", Addr: "[de:ad:be:ef::ca:fe]:80", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.Local},
49+
&Config{User: "user", Passwd: "p@ss(word)", Net: "tcp", Addr: "[de:ad:be:ef::ca:fe]:80", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.Local, AllowNativePasswords: true},
4750
}, {
4851
"/dbname",
49-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC},
52+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
5053
}, {
5154
"@/",
52-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC},
55+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
5356
}, {
5457
"/",
55-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC},
58+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
5659
}, {
5760
"",
58-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC},
61+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
5962
}, {
6063
"user:p@/ssword@/",
61-
&Config{User: "user", Passwd: "p@/ssword", Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC},
64+
&Config{User: "user", Passwd: "p@/ssword", Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
6265
}, {
6366
"unix/?arg=%2Fsome%2Fpath.ext",
64-
&Config{Net: "unix", Addr: "/tmp/mysql.sock", Params: map[string]string{"arg": "/some/path.ext"}, Collation: "utf8_general_ci", Loc: time.UTC},
67+
&Config{Net: "unix", Addr: "/tmp/mysql.sock", Params: map[string]string{"arg": "/some/path.ext"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
6568
}}
6669

6770
func TestDSNParser(t *testing.T) {
@@ -223,8 +226,9 @@ func TestDSNUnsafeCollation(t *testing.T) {
223226
func TestParamsAreSorted(t *testing.T) {
224227
expected := "/dbname?interpolateParams=true&foobar=baz&quux=loo"
225228
dsn := &Config{
226-
DBName: "dbname",
227-
InterpolateParams: true,
229+
DBName: "dbname",
230+
InterpolateParams: true,
231+
AllowNativePasswords: true,
228232
Params: map[string]string{
229233
"quux": "loo",
230234
"foobar": "baz",

0 commit comments

Comments
 (0)