@@ -349,25 +349,35 @@ public ByteBuffer fetchOnePacket() throws IOException {
349349 // before read, set limit to make read only one packet
350350 result .limit (result .position () + packetLen );
351351 readLen = readAll (result , false );
352- if (isSslMode && !isSslHandshaking && remainingBuffer .position () == 0 && result .hasRemaining ()) {
352+ boolean hasBufferedRemainder = remainingBuffer != null && remainingBuffer .limit () != 0 ;
353+ if (isSslMode && !isSslHandshaking && !hasBufferedRemainder && result .hasRemaining ()) {
353354 byte [] header = result .array ();
355+ int available = result .limit ();
356+ if (available < PACKET_HEADER_LEN ) {
357+ LOG .warn ("SSL mode: packet header incomplete. available bytes: " + available );
358+ throw new IOException ("Incomplete MySQL packet header." );
359+ }
354360 int mysqlPacketLength = (header [0 ] & 0xFF ) | ((header [1 ] & 0xFF ) << 8 ) | ((header [2 ] & 0xFF ) << 16 );
355- if (result .position () >= 4 && mysqlPacketLength > 0 && mysqlPacketLength
356- <= MAX_PHYSICAL_PACKET_LENGTH ) {
357- int packetId = header [3 ] & 0xFF ;
358- if (packetId != sequenceId ) {
359- LOG .warn ("receive packet sequence id[" + packetId + "] want to get[" + sequenceId + "]" );
360- throw new IOException ("Bad packet sequence." );
361- }
362- } else {
363- if (LOG .isDebugEnabled ()) {
364- LOG .debug ("SSL mode: skipping sequence check, packet length: " + mysqlPacketLength
365- + ", buffer position: " + result .position ());
366- }
361+ if (mysqlPacketLength <= 0 || mysqlPacketLength > MAX_PHYSICAL_PACKET_LENGTH ) {
362+ LOG .warn ("SSL mode: invalid mysql packet length: " + mysqlPacketLength );
363+ throw new IOException ("Invalid MySQL packet length: " + mysqlPacketLength );
364+ }
365+ int packetId = header [3 ] & 0xFF ;
366+ if (packetId != sequenceId ) {
367+ LOG .warn ("receive packet sequence id[" + packetId + "] want to get[" + sequenceId + "]" );
368+ throw new IOException ("Bad packet sequence." );
367369 }
368370 // remove mysql packet header
369371 result .position (4 );
370372 result .compact ();
373+ int payloadBytes = result .position ();
374+ if (payloadBytes > 0 ) {
375+ int commandCode = result .array ()[0 ] & 0xFF ;
376+ if (MysqlCommand .fromCode (commandCode ) == null ) {
377+ LOG .warn ("SSL mode: unknown mysql command code: " + commandCode );
378+ throw new IOException ("Unknown MySQL command: " + commandCode );
379+ }
380+ }
371381 // when encounter large sql query, one mysql packet will be packed as multiple ssl packets.
372382 // we need to read all ssl packets to combine the complete mysql packet.
373383 while (mysqlPacketLength > result .limit ()) {
0 commit comments