Skip to content

Commit

Permalink
Fixed bug in caching_sha2_password authentication that caused it to f…
Browse files Browse the repository at this point in the history
…ail for passwords longer than 19 characters.

Thanks to sidorares and normano for their guidance.

Refer to:

mysqljs#2233 (comment)
sidorares/node-mysql2#1044
sidorares/node-mysql2#1045

Updated version to 2.18.3
  • Loading branch information
vlasky committed Apr 16, 2021
1 parent 7504e3d commit 273ac59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/protocol/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ function xor(a, b) {
}
Auth.xor = xor;

function xorRotating(a, seed) {
if (!Buffer.isBuffer(a)) {
a = Buffer.from(a, 'binary');
}

if (!Buffer.isBuffer(seed)) {
seed = Buffer.from(seed, 'binary');
}

const result = Buffer.allocUnsafe(a.length);
const seedLen = seed.length;

for (let i = 0; i < a.length; i++) {
result[i] = a[i] ^ seed[i % seedLen];
}
return result;
}
Auth.xorRotating = xorRotating;

Auth.token = function(password, scramble) {
if (!password) {
return Buffer.alloc(0);
Expand Down Expand Up @@ -78,7 +97,7 @@ Auth.encrypt = function(password, scramble, key) {
throw err;
}

var stage1 = xor((Buffer.from(password + '\0', 'utf8')).toString('binary'), scramble.toString('binary'));
var stage1 = xorRotating((Buffer.from(password + '\0', 'utf8')).toString('binary'), scramble.toString('binary'));
return Crypto.publicEncrypt(key, stage1);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vlasky/mysql",
"description": "A fork of mysqljs/mysql with partial support for the MySQL compressed protocol (reads compressed data sent by server). It is written in JavaScript, does not require compiling, and is 100% MIT licensed.",
"version": "2.18.2",
"version": "2.18.3",
"license": "MIT",
"author": "Vlad Lasky <[email protected]> (https://github.com/vlasky)",
"contributors": [
Expand Down

0 comments on commit 273ac59

Please sign in to comment.