Skip to content

Commit 273ac59

Browse files
committed
Fixed bug in caching_sha2_password authentication that caused it to fail 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
1 parent 7504e3d commit 273ac59

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/protocol/Auth.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ function xor(a, b) {
4747
}
4848
Auth.xor = xor;
4949

50+
function xorRotating(a, seed) {
51+
if (!Buffer.isBuffer(a)) {
52+
a = Buffer.from(a, 'binary');
53+
}
54+
55+
if (!Buffer.isBuffer(seed)) {
56+
seed = Buffer.from(seed, 'binary');
57+
}
58+
59+
const result = Buffer.allocUnsafe(a.length);
60+
const seedLen = seed.length;
61+
62+
for (let i = 0; i < a.length; i++) {
63+
result[i] = a[i] ^ seed[i % seedLen];
64+
}
65+
return result;
66+
}
67+
Auth.xorRotating = xorRotating;
68+
5069
Auth.token = function(password, scramble) {
5170
if (!password) {
5271
return Buffer.alloc(0);
@@ -78,7 +97,7 @@ Auth.encrypt = function(password, scramble, key) {
7897
throw err;
7998
}
8099

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@vlasky/mysql",
33
"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.",
4-
"version": "2.18.2",
4+
"version": "2.18.3",
55
"license": "MIT",
66
"author": "Vlad Lasky <[email protected]> (https://github.com/vlasky)",
77
"contributors": [

0 commit comments

Comments
 (0)