Skip to content

ServerHandshake calls Packet.readNullTerminatedString without encoding #1975

Closed
@tims-bsquare

Description

@tims-bsquare

If using server_handshake.js and adding a listener for field_list then it will call const table = packet.readNullTerminatedString();.

It appears that the function expects an encoding and if it doesn't get one I see a stack trace like this

Error: Encoding not recognized: 'undefined' (searched as: 'undefined')
    at Object.getCodec (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
    at Object.getDecoder (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:125:23)
    at Object.exports.decode (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/lib/parsers/string.js:10:25)
    at Packet.readNullTerminatedString (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/lib/packets/packet.js:412:25)
    at ServerHandshake.dispatchCommands (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/lib/commands/server_handshake.js:141:32)
    at ServerHandshake.execute (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/lib/commands/command.js:45:22)
    at Connection.handlePacket (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/lib/connection.js:489:32)
    at PacketParser.onPacket (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/lib/connection.js:94:12)
    at PacketParser.executeStart (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/lib/packet_parser.js:75:16)
    at Socket.<anonymous> (/home/tstableford/projects/sq-dev/service-db-proxy/node_modules/mysql2/lib/connection.js:101:25)

I wouldn't have noticed though if not having to make use of the workaround documented here #528 (comment)

Thanks for providing utils to run as a server though :)

Hacky fix for anybody else who stumbles across this, written in TypeScript.

try {
  const Packet = require('../node_modules/mysql2/lib/packets/packet.js');
  Packet.prototype.readNullTerminatedStringOriginal = Packet.prototype.readNullTerminatedString;
  Packet.prototype.readNullTerminatedString = function (encoding?: string): string {
    return Packet.prototype.readNullTerminatedStringOriginal.call(this, encoding || 'utf8');
  }
} catch (err) {
  console.error('Failed to overwrite readNullTerminatedString. Connections using deprecated syntax will fail', err);
}

By the way I'm happy to PR this if you like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions