Skip to content

Commit 7daee5b

Browse files
authored
Fix OldAuthSwitchRequest support (go-sql-driver#870)
1 parent 0f257fc commit 7daee5b

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

auth_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,22 @@ func TestAuthSwitchOldPasswordNotAllowed(t *testing.T) {
10641064
}
10651065
}
10661066

1067+
// Same to TestAuthSwitchOldPasswordNotAllowed, but use OldAuthSwitch request.
1068+
func TestOldAuthSwitchNotAllowed(t *testing.T) {
1069+
conn, mc := newRWMockConn(2)
1070+
1071+
// OldAuthSwitch request
1072+
conn.data = []byte{1, 0, 0, 2, 0xfe}
1073+
conn.maxReads = 1
1074+
authData := []byte{95, 84, 103, 43, 61, 49, 123, 61, 91, 50, 40, 113, 35,
1075+
84, 96, 101, 92, 123, 121, 107}
1076+
plugin := "mysql_native_password"
1077+
err := mc.handleAuthResult(authData, plugin)
1078+
if err != ErrOldPassword {
1079+
t.Errorf("expected ErrOldPassword, got %v", err)
1080+
}
1081+
}
1082+
10671083
func TestAuthSwitchOldPassword(t *testing.T) {
10681084
conn, mc := newRWMockConn(2)
10691085
mc.cfg.AllowOldPasswords = true
@@ -1092,6 +1108,32 @@ func TestAuthSwitchOldPassword(t *testing.T) {
10921108
}
10931109
}
10941110

1111+
// Same to TestAuthSwitchOldPassword, but use OldAuthSwitch request.
1112+
func TestOldAuthSwitch(t *testing.T) {
1113+
conn, mc := newRWMockConn(2)
1114+
mc.cfg.AllowOldPasswords = true
1115+
mc.cfg.Passwd = "secret"
1116+
1117+
// OldAuthSwitch request
1118+
conn.data = []byte{1, 0, 0, 2, 0xfe}
1119+
1120+
// auth response
1121+
conn.queuedReplies = [][]byte{{8, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0}}
1122+
conn.maxReads = 2
1123+
1124+
authData := []byte{95, 84, 103, 43, 61, 49, 123, 61, 91, 50, 40, 113, 35,
1125+
84, 96, 101, 92, 123, 121, 107}
1126+
plugin := "mysql_native_password"
1127+
1128+
if err := mc.handleAuthResult(authData, plugin); err != nil {
1129+
t.Errorf("got error: %v", err)
1130+
}
1131+
1132+
expectedReply := []byte{9, 0, 0, 3, 86, 83, 83, 79, 74, 78, 65, 66, 0}
1133+
if !bytes.Equal(conn.written, expectedReply) {
1134+
t.Errorf("got unexpected data: %v", conn.written)
1135+
}
1136+
}
10951137
func TestAuthSwitchOldPasswordEmpty(t *testing.T) {
10961138
conn, mc := newRWMockConn(2)
10971139
mc.cfg.AllowOldPasswords = true
@@ -1120,6 +1162,33 @@ func TestAuthSwitchOldPasswordEmpty(t *testing.T) {
11201162
}
11211163
}
11221164

1165+
// Same to TestAuthSwitchOldPasswordEmpty, but use OldAuthSwitch request.
1166+
func TestOldAuthSwitchPasswordEmpty(t *testing.T) {
1167+
conn, mc := newRWMockConn(2)
1168+
mc.cfg.AllowOldPasswords = true
1169+
mc.cfg.Passwd = ""
1170+
1171+
// OldAuthSwitch request.
1172+
conn.data = []byte{1, 0, 0, 2, 0xfe}
1173+
1174+
// auth response
1175+
conn.queuedReplies = [][]byte{{8, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0}}
1176+
conn.maxReads = 2
1177+
1178+
authData := []byte{95, 84, 103, 43, 61, 49, 123, 61, 91, 50, 40, 113, 35,
1179+
84, 96, 101, 92, 123, 121, 107}
1180+
plugin := "mysql_native_password"
1181+
1182+
if err := mc.handleAuthResult(authData, plugin); err != nil {
1183+
t.Errorf("got error: %v", err)
1184+
}
1185+
1186+
expectedReply := []byte{1, 0, 0, 3, 0}
1187+
if !bytes.Equal(conn.written, expectedReply) {
1188+
t.Errorf("got unexpected data: %v", conn.written)
1189+
}
1190+
}
1191+
11231192
func TestAuthSwitchSHA256PasswordEmpty(t *testing.T) {
11241193
conn, mc := newRWMockConn(2)
11251194
mc.cfg.Passwd = ""

packets.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ func (mc *mysqlConn) readAuthResult() ([]byte, string, error) {
479479
return data[1:], "", err
480480

481481
case iEOF:
482-
if len(data) < 1 {
482+
if len(data) == 1 {
483483
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::OldAuthSwitchRequest
484484
return nil, "mysql_old_password", nil
485485
}

0 commit comments

Comments
 (0)