Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServerHandshake calls Packet.readNullTerminatedString without encoding #1975

Closed
tims-bsquare opened this issue Apr 24, 2023 · 4 comments · Fixed by #1976
Closed

ServerHandshake calls Packet.readNullTerminatedString without encoding #1975

tims-bsquare opened this issue Apr 24, 2023 · 4 comments · Fixed by #1976

Comments

@tims-bsquare
Copy link
Contributor

tims-bsquare commented Apr 24, 2023

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.

@sidorares
Copy link
Owner

sure, happy to discuss a PR

@tims-bsquare
Copy link
Contributor Author

Thanks @sidorares PR open

@sidorares
Copy link
Owner

@tims-bsquare
Copy link
Contributor Author

That's brilliant, thanks @sidorares !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants