Skip to content

Commit a2392e2

Browse files
committed
feat: cleanup buffer/string conversions in hashing/xor helpers that were failing in Bun
1 parent 703ecb2 commit a2392e2

File tree

6 files changed

+17
-62
lines changed

6 files changed

+17
-62
lines changed

.github/workflows/ci-bun.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ jobs:
5959

6060
- name: Run tests
6161
# todo: run full test suite once test createServer is implemented using Bun.listen
62-
run: DEBUG=1 MYSQL_PORT=3306 bun test/integration/connection/test-select-1.js
62+
run: FILTER=test-select MYSQL_PORT=3306 bun run test

.github/workflows/ci-linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
mysql-version: "mysql:5.7"
5656
use-compression: 0
5757
use-tls: 0
58-
- filter: "5.1only"
58+
- filter: "test-select-1"
5959
node-version: "16.x"
6060
mysql-version: "datagrip/mysql:5.1"
6161
use-compression: 0

lib/auth_41.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,7 @@ function sha1(msg, msg1, msg2) {
4141
}
4242

4343
function xor(a, b) {
44-
if (!Buffer.isBuffer(a)) {
45-
a = Buffer.from(a, 'binary');
46-
}
47-
48-
if (!Buffer.isBuffer(b)) {
49-
b = Buffer.from(b, 'binary');
50-
}
51-
5244
const result = Buffer.allocUnsafe(a.length);
53-
5445
for (let i = 0; i < a.length; i++) {
5546
result[i] = a[i] ^ b[i];
5647
}
@@ -60,7 +51,6 @@ function xor(a, b) {
6051
exports.xor = xor;
6152

6253
function token(password, scramble1, scramble2) {
63-
// TODO: use buffers (not sure why strings here)
6454
if (!password) {
6555
return Buffer.alloc(0);
6656
}
@@ -94,14 +84,6 @@ exports.doubleSha1 = function(password) {
9484
};
9585

9686
function xorRotating(a, seed) {
97-
if (!Buffer.isBuffer(a)) {
98-
a = Buffer.from(a, 'binary');
99-
}
100-
101-
if (!Buffer.isBuffer(seed)) {
102-
seed = Buffer.from(seed, 'binary');
103-
}
104-
10587
const result = Buffer.allocUnsafe(a.length);
10688
const seedLen = seed.length;
10789

lib/auth_plugins/caching_sha2_password.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ const STATE_FINAL = -1;
1717

1818
function sha256(msg) {
1919
const hash = crypto.createHash('sha256');
20-
hash.update(msg, 'binary');
21-
return hash.digest('binary');
20+
hash.update(msg);
21+
return hash.digest();
2222
}
2323

2424
function calculateToken(password, scramble) {
2525
if (!password) {
2626
return Buffer.alloc(0);
2727
}
28-
const stage1 = sha256(Buffer.from(password, 'utf8').toString('binary'));
28+
const stage1 = sha256(Buffer.from(password));
2929
const stage2 = sha256(stage1);
30-
const stage3 = sha256(stage2 + scramble.toString('binary'));
30+
const stage3 = sha256(Buffer.concat([stage2, scramble]));
3131
return xor(stage1, stage3);
3232
}
3333

3434
function encrypt(password, scramble, key) {
3535
const stage1 = xorRotating(
36-
Buffer.from(`${password}\0`, 'utf8').toString('binary'),
37-
scramble.toString('binary')
36+
Buffer.from(`${password}\0`, 'utf8'),
37+
scramble
3838
);
3939
return crypto.publicEncrypt(key, stage1);
4040
}
@@ -86,6 +86,7 @@ module.exports = (pluginOptions = {}) => ({ connection }) => {
8686
`Invalid AuthMoreData packet received by ${PLUGIN_NAME} plugin in STATE_TOKEN_SENT state.`
8787
);
8888
case STATE_WAIT_SERVER_KEY:
89+
console.log('Server pub key:', data);
8990
if (pluginOptions.onServerPublicKey) {
9091
pluginOptions.onServerPublicKey(data);
9192
}

lib/auth_plugins/sha256_password.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const STATE_FINAL = -1;
1212

1313
function encrypt(password, scramble, key) {
1414
const stage1 = xorRotating(
15-
Buffer.from(`${password}\0`, 'utf8').toString('binary'),
16-
scramble.toString('binary')
15+
Buffer.from(`${password}\0`, 'utf8'),
16+
scramble
1717
);
1818
return crypto.publicEncrypt(key, stage1);
1919
}
Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,13 @@
11
'use strict';
22

3-
console.log('Hello from bun test', typeof Bun);
4-
3+
const assert = require('assert');
54
const common = require('../../common');
6-
7-
console.log('after import');
8-
95
const connection = common.createConnection();
10-
connection.on('connect', function(hello) {
11-
console.log('connect', hello.serverVersion, hello.protocolVersion);
12-
})
13-
console.log('after create connection');
14-
//const assert = require('assert');
15-
connection.query('SELECT 1', (err, _rows, _fields) => {
16-
console.log('query callback', err, _rows, _fields);
17-
connection.end();
18-
console.log('after end connection');
19-
});
20-
21-
22-
/*
236

24-
let rows = undefined;
25-
let fields = undefined;
26-
connection.query('SELECT 1', (err, _rows, _fields) => {
27-
if (err) {
28-
throw err;
29-
}
30-
31-
rows = _rows;
32-
fields = _fields;
33-
connection.end();
34-
});
35-
36-
process.on('exit', () => {
7+
connection.query('SELECT 1', (err, rows, fields) => {
8+
console.log('query callback', err, rows, fields);
9+
assert.ifError(err);
3710
assert.deepEqual(rows, [{ 1: 1 }]);
3811
assert.equal(fields[0].name, '1');
39-
});
40-
41-
*/
12+
connection.end();
13+
});

0 commit comments

Comments
 (0)