@@ -77,21 +77,25 @@ func (c *Conn) readInitialHandshake() error {
77
77
c .capability = uint32 (binary .LittleEndian .Uint16 (data [pos :pos + 2 ]))<< 16 | c .capability
78
78
pos += 2
79
79
80
- // skip auth data len or [00]
80
+ // auth_data is end with 0x00, min data length is 13 + 8 = 21
81
+ // ref to https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::Handshake
82
+ maxAuthDataLen := 21
83
+ if c .capability & CLIENT_PLUGIN_AUTH != 0 && int (data [pos ]) > maxAuthDataLen {
84
+ maxAuthDataLen = int (data [pos ])
85
+ }
86
+
81
87
// skip reserved (all [00])
82
88
pos += 10 + 1
83
89
84
- // The documentation is ambiguous about the length.
85
- // The official Python library uses the fixed length 12
86
- // mysql-proxy also use 12
87
- // which is not documented but seems to work.
88
- c .salt = append (c .salt , data [pos :pos + 12 ]... )
89
- pos += 13
90
- // auth plugin
91
- if end := bytes .IndexByte (data [pos :], 0x00 ); end != - 1 {
92
- c .authPluginName = string (data [pos : pos + end ])
93
- } else {
94
- c .authPluginName = string (data [pos :])
90
+ // auth_data is end with 0x00, so we need to trim 0x00
91
+ resetOfAuthDataEndPos := pos + maxAuthDataLen - 8 - 1
92
+ c .salt = append (c .salt , data [pos :resetOfAuthDataEndPos ]... )
93
+
94
+ // skip reset of end pos
95
+ pos = resetOfAuthDataEndPos + 1
96
+
97
+ if c .capability & CLIENT_PLUGIN_AUTH != 0 {
98
+ c .authPluginName = string (data [pos : len (data )- 1 ])
95
99
}
96
100
}
97
101
0 commit comments