Skip to content

Commit

Permalink
Check if MySQL supports CLIENT_CONNECT_ATTRS before sending client at…
Browse files Browse the repository at this point in the history
…tributes. (go-sql-driver#1640)
  • Loading branch information
raffertyyu authored Nov 19, 2024
1 parent f62f523 commit 41a5fa2
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,13 @@ func (mc *mysqlConn) readHandshakePacket() (data []byte, plugin string, err erro
if len(data) > pos {
// character set [1 byte]
// status flags [2 bytes]
pos += 3
// capability flags (upper 2 bytes) [2 bytes]
mc.flags |= clientFlag(binary.LittleEndian.Uint16(data[pos:pos+2])) << 16
pos += 2
// length of auth-plugin-data [1 byte]
// reserved (all [00]) [10 bytes]
pos += 1 + 2 + 2 + 1 + 10
pos += 11

// second part of the password cipher [minimum 13 bytes],
// where len=MAX(13, length of auth-plugin-data - 8)
Expand Down Expand Up @@ -261,9 +264,11 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
clientLocalFiles |
clientPluginAuth |
clientMultiResults |
clientConnectAttrs |
mc.flags&clientConnectAttrs |
mc.flags&clientLongFlag

sendConnectAttrs := mc.flags&clientConnectAttrs != 0

if mc.cfg.ClientFoundRows {
clientFlags |= clientFoundRows
}
Expand Down Expand Up @@ -296,10 +301,13 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
}

// encode length of the connection attributes
var connAttrsLEIBuf [9]byte
connAttrsLen := len(mc.connector.encodedAttributes)
connAttrsLEI := appendLengthEncodedInteger(connAttrsLEIBuf[:0], uint64(connAttrsLen))
pktLen += len(connAttrsLEI) + len(mc.connector.encodedAttributes)
var connAttrsLEI []byte
if sendConnectAttrs {
var connAttrsLEIBuf [9]byte
connAttrsLen := len(mc.connector.encodedAttributes)
connAttrsLEI = appendLengthEncodedInteger(connAttrsLEIBuf[:0], uint64(connAttrsLen))
pktLen += len(connAttrsLEI) + len(mc.connector.encodedAttributes)
}

// Calculate packet length and get buffer with that size
data, err := mc.buf.takeBuffer(pktLen + 4)
Expand Down Expand Up @@ -382,8 +390,10 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
pos++

// Connection Attributes
pos += copy(data[pos:], connAttrsLEI)
pos += copy(data[pos:], []byte(mc.connector.encodedAttributes))
if sendConnectAttrs {
pos += copy(data[pos:], connAttrsLEI)
pos += copy(data[pos:], []byte(mc.connector.encodedAttributes))
}

// Send Auth packet
return mc.writePacket(data[:pos])
Expand Down

0 comments on commit 41a5fa2

Please sign in to comment.