Skip to content

Commit

Permalink
feat: cleanup buffer/string conversions in hashing/xor helpers that w…
Browse files Browse the repository at this point in the history
…ere failing in Bun
  • Loading branch information
sidorares committed Jan 30, 2023
1 parent 703ecb2 commit a2392e2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-bun.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ jobs:

- name: Run tests
# todo: run full test suite once test createServer is implemented using Bun.listen
run: DEBUG=1 MYSQL_PORT=3306 bun test/integration/connection/test-select-1.js
run: FILTER=test-select MYSQL_PORT=3306 bun run test
2 changes: 1 addition & 1 deletion .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
mysql-version: "mysql:5.7"
use-compression: 0
use-tls: 0
- filter: "5.1only"
- filter: "test-select-1"
node-version: "16.x"
mysql-version: "datagrip/mysql:5.1"
use-compression: 0
Expand Down
18 changes: 0 additions & 18 deletions lib/auth_41.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,7 @@ function sha1(msg, msg1, msg2) {
}

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

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

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

for (let i = 0; i < a.length; i++) {
result[i] = a[i] ^ b[i];
}
Expand All @@ -60,7 +51,6 @@ function xor(a, b) {
exports.xor = xor;

function token(password, scramble1, scramble2) {
// TODO: use buffers (not sure why strings here)
if (!password) {
return Buffer.alloc(0);
}
Expand Down Expand Up @@ -94,14 +84,6 @@ exports.doubleSha1 = function(password) {
};

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;

Expand Down
13 changes: 7 additions & 6 deletions lib/auth_plugins/caching_sha2_password.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ const STATE_FINAL = -1;

function sha256(msg) {
const hash = crypto.createHash('sha256');
hash.update(msg, 'binary');
return hash.digest('binary');
hash.update(msg);
return hash.digest();
}

function calculateToken(password, scramble) {
if (!password) {
return Buffer.alloc(0);
}
const stage1 = sha256(Buffer.from(password, 'utf8').toString('binary'));
const stage1 = sha256(Buffer.from(password));
const stage2 = sha256(stage1);
const stage3 = sha256(stage2 + scramble.toString('binary'));
const stage3 = sha256(Buffer.concat([stage2, scramble]));
return xor(stage1, stage3);
}

function encrypt(password, scramble, key) {
const stage1 = xorRotating(
Buffer.from(`${password}\0`, 'utf8').toString('binary'),
scramble.toString('binary')
Buffer.from(`${password}\0`, 'utf8'),
scramble
);
return crypto.publicEncrypt(key, stage1);
}
Expand Down Expand Up @@ -86,6 +86,7 @@ module.exports = (pluginOptions = {}) => ({ connection }) => {
`Invalid AuthMoreData packet received by ${PLUGIN_NAME} plugin in STATE_TOKEN_SENT state.`
);
case STATE_WAIT_SERVER_KEY:
console.log('Server pub key:', data);

This comment has been minimized.

Copy link
@sthuck

sthuck Feb 1, 2023

Contributor

@sidorares
is this console.log intentional?

This comment has been minimized.

Copy link
@sidorares

sidorares Feb 1, 2023

Author Owner

Accident debug leftover, will remove tomorrow ( feel free to raise a pr with cleanup, if you use "fix: ..." Conventional commit format that would also result in release-please publishing package to npm )

if (pluginOptions.onServerPublicKey) {
pluginOptions.onServerPublicKey(data);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/auth_plugins/sha256_password.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const STATE_FINAL = -1;

function encrypt(password, scramble, key) {
const stage1 = xorRotating(
Buffer.from(`${password}\0`, 'utf8').toString('binary'),
scramble.toString('binary')
Buffer.from(`${password}\0`, 'utf8'),
scramble
);
return crypto.publicEncrypt(key, stage1);
}
Expand Down
40 changes: 6 additions & 34 deletions test/integration/connection/test-select-1.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
'use strict';

console.log('Hello from bun test', typeof Bun);

const assert = require('assert');
const common = require('../../common');

console.log('after import');

const connection = common.createConnection();
connection.on('connect', function(hello) {
console.log('connect', hello.serverVersion, hello.protocolVersion);
})
console.log('after create connection');
//const assert = require('assert');
connection.query('SELECT 1', (err, _rows, _fields) => {
console.log('query callback', err, _rows, _fields);
connection.end();
console.log('after end connection');
});


/*

let rows = undefined;
let fields = undefined;
connection.query('SELECT 1', (err, _rows, _fields) => {
if (err) {
throw err;
}
rows = _rows;
fields = _fields;
connection.end();
});
process.on('exit', () => {
connection.query('SELECT 1', (err, rows, fields) => {
console.log('query callback', err, rows, fields);
assert.ifError(err);
assert.deepEqual(rows, [{ 1: 1 }]);
assert.equal(fields[0].name, '1');
});
*/
connection.end();
});

0 comments on commit a2392e2

Please sign in to comment.