Skip to content

Commit

Permalink
chore(NODE-5581): pull in bson alpha.1 and mongodb-legacy main (#3843)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Aug 24, 2023
1 parent ecb2e20 commit 91152b9
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 52 deletions.
25 changes: 17 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -25,9 +25,9 @@
"email": "dbx-node@mongodb.com"
},
"dependencies": {
"bson": "^5.4.0",
"mongodb-connection-string-url": "^2.6.0",
"@mongodb-js/saslprep": "^1.1.0"
"@mongodb-js/saslprep": "^1.1.0",
"bson": "^6.0.0-alpha.1",
"mongodb-connection-string-url": "^2.6.0"
},
"peerDependencies": {
"@aws-sdk/credential-providers": "^3.188.0",
Expand Down Expand Up @@ -97,7 +97,7 @@
"mocha": "^10.2.0",
"mocha-sinon": "^2.1.2",
"mongodb-client-encryption": "6.0.0-alpha.3",
"mongodb-legacy": "^5.0.0",
"mongodb-legacy": "github:mongodb-js/nodejs-mongodb-legacy#main",
"nyc": "^15.1.0",
"prettier": "^2.8.8",
"semver": "^7.5.0",
Expand Down
21 changes: 12 additions & 9 deletions src/cmap/auth/scram.ts
Expand Up @@ -138,10 +138,11 @@ async function continueScramConversation(
const processedPassword =
cryptoMethod === 'sha256' ? saslprep(password) : passwordDigest(username, password);

const payload = Buffer.isBuffer(response.payload)
const payload: Binary = Buffer.isBuffer(response.payload)
? new Binary(response.payload)
: response.payload;
const dict = parsePayload(payload.value());

const dict = parsePayload(payload);

const iterations = parseInt(dict.i, 10);
if (iterations && iterations < 4096) {
Expand All @@ -168,9 +169,11 @@ async function continueScramConversation(
const clientKey = HMAC(cryptoMethod, saltedPassword, 'Client Key');
const serverKey = HMAC(cryptoMethod, saltedPassword, 'Server Key');
const storedKey = H(cryptoMethod, clientKey);
const authMessage = [clientFirstMessageBare(username, nonce), payload.value(), withoutProof].join(
','
);
const authMessage = [
clientFirstMessageBare(username, nonce),
payload.toString('utf8'),
withoutProof
].join(',');

const clientSignature = HMAC(cryptoMethod, storedKey, authMessage);
const clientProof = `p=${xor(clientKey, clientSignature)}`;
Expand All @@ -184,7 +187,7 @@ async function continueScramConversation(
};

const r = await connection.commandAsync(ns(`${db}.$cmd`), saslContinueCmd, undefined);
const parsedResponse = parsePayload(r.payload.value());
const parsedResponse = parsePayload(r.payload);

if (!compareDigest(Buffer.from(parsedResponse.v, 'base64'), serverSignature)) {
throw new MongoRuntimeError('Server returned an invalid signature');
Expand All @@ -204,14 +207,14 @@ async function continueScramConversation(
await connection.commandAsync(ns(`${db}.$cmd`), retrySaslContinueCmd, undefined);
}

function parsePayload(payload: string) {
function parsePayload(payload: Binary) {
const payloadStr = payload.toString('utf8');
const dict: Document = {};
const parts = payload.split(',');
const parts = payloadStr.split(',');
for (let i = 0; i < parts.length; i++) {
const valueParts = parts[i].split('=');
dict[valueParts[0]] = valueParts[1];
}

return dict;
}

Expand Down
11 changes: 8 additions & 3 deletions test/action/dependency.test.ts
Expand Up @@ -6,9 +6,12 @@ import { expect } from 'chai';

import { dependencies, peerDependencies, peerDependenciesMeta } from '../../package.json';
import { setDifference } from '../mongodb';
import { itInNodeProcess } from '../tools/utils';
import { alphabetically, itInNodeProcess, sorted } from '../tools/utils';

const EXPECTED_DEPENDENCIES = ['bson', 'mongodb-connection-string-url', '@mongodb-js/saslprep'];
const EXPECTED_DEPENDENCIES = sorted(
['@mongodb-js/saslprep', 'bson', 'mongodb-connection-string-url'],
alphabetically
);
const EXPECTED_PEER_DEPENDENCIES = [
'@aws-sdk/credential-providers',
'@mongodb-js/zstd',
Expand All @@ -22,7 +25,9 @@ const EXPECTED_PEER_DEPENDENCIES = [
describe('package.json', function () {
describe('dependencies', function () {
it('only contains the expected dependencies', function () {
expect(Object.keys(dependencies)).to.deep.equal(EXPECTED_DEPENDENCIES);
expect(sorted(Object.keys(dependencies), alphabetically)).to.deep.equal(
EXPECTED_DEPENDENCIES
);
});
});

Expand Down
36 changes: 21 additions & 15 deletions test/integration/crud/insert.test.js
Expand Up @@ -247,7 +247,10 @@ describe('crud - insert', function () {
test.equal(date.toString(), doc.date.toString());
test.equal(date.getTime(), doc.date.getTime());
test.equal(motherOfAllDocuments.oid.toHexString(), doc.oid.toHexString());
test.equal(motherOfAllDocuments.binary.value(), doc.binary.value());
test.equal(
motherOfAllDocuments.binary.value().toString('hex'),
doc.binary.value().toString('hex')
);

test.equal(motherOfAllDocuments.int, doc.int);
test.equal(motherOfAllDocuments.long, doc.long);
Expand Down Expand Up @@ -398,7 +401,10 @@ describe('crud - insert', function () {
test.equal(date.toString(), doc.date.toString());
test.equal(date.getTime(), doc.date.getTime());
test.equal(motherOfAllDocuments.oid.toHexString(), doc.oid.toHexString());
test.equal(motherOfAllDocuments.binary.value(), doc.binary.value());
test.equal(
motherOfAllDocuments.binary.value().toString('hex'),
doc.binary.value().toString('hex')
);

test.equal(motherOfAllDocuments.int, doc.int);
test.equal(motherOfAllDocuments.long, doc.long);
Expand Down Expand Up @@ -1482,28 +1488,28 @@ describe('crud - insert', function () {
// in this case we are setting that node needs to be higher than 0.10.X to run
metadata: {
requires: {
topology: ['single', 'replicaset', 'ssl', 'heap', 'wiredtiger'],
mongodb: '>=2.8.0'
}
},

test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });

var document = {
string: 'abcdefghijkl',
objid: new ObjectId(Buffer.alloc(12, 1)),
double: new Double(1),
binary: new Binary(Buffer.from('hello world')),
minkey: new MinKey(),
maxkey: new MaxKey(),
code: new Code('function () {}', { a: 55 })
};

client.connect(function (err, client) {
var db = client.db(configuration.db);
var collection = db.collection('bson_types_insert_1');

var document = {
string: 'abcdefghijkl',
objid: new ObjectId('abcdefghijkl'),
double: new Double(1),
binary: new Binary(Buffer.from('hello world')),
minkey: new MinKey(),
maxkey: new MaxKey(),
code: new Code('function () {}', { a: 55 })
};

collection.insert(document, configuration.writeConcernMax(), function (err, result) {
expect(err).to.not.exist;
test.ok(result);
Expand All @@ -1512,9 +1518,9 @@ describe('crud - insert', function () {
expect(err).to.not.exist;
test.equal('abcdefghijkl', doc.string.toString());

collection.findOne({ objid: new ObjectId('abcdefghijkl') }, function (err, doc) {
collection.findOne({ objid: new ObjectId(Buffer.alloc(12, 1)) }, function (err, doc) {
expect(err).to.not.exist;
test.equal('6162636465666768696a6b6c', doc.objid.toString());
test.equal('01'.repeat(12), doc.objid.toString());

collection.findOne({ double: new Double(1) }, function (err, doc) {
expect(err).to.not.exist;
Expand Down
4 changes: 2 additions & 2 deletions test/integration/crud/pk_factory.test.js
Expand Up @@ -19,7 +19,7 @@ describe('PkFactory', function () {
// Custom factory (need to provide a 12 byte array);
var CustomPKFactory = {
createPk() {
return new ObjectId('aaaaaaaaaaaa');
return new ObjectId('a'.repeat(24));
}
};

Expand All @@ -39,7 +39,7 @@ describe('PkFactory', function () {

collection.insert({ a: 1 }, { writeConcern: { w: 1 } }, function (err) {
expect(err).to.not.exist;
collection.find({ _id: new ObjectId('aaaaaaaaaaaa') }).toArray(function (err, items) {
collection.find({ _id: new ObjectId('a'.repeat(24)) }).toArray(function (err, items) {
expect(items.length).to.equal(1);

client.close(done);
Expand Down
2 changes: 1 addition & 1 deletion test/tools/common.js
Expand Up @@ -122,7 +122,7 @@ class ReplSetFixture {
function genClusterTime(time) {
return {
clusterTime: new BSON.Timestamp(BSON.Long.fromNumber(time, true)),
signature: { hash: new BSON.Binary('test'), keyId: new BSON.Long(1) }
signature: { hash: new BSON.Binary(Buffer.from('test', 'utf8')), keyId: new BSON.Long(1) }
};
}

Expand Down
8 changes: 6 additions & 2 deletions test/tools/utils.ts
Expand Up @@ -476,8 +476,12 @@ export class UnifiedTestSuiteBuilder {
}
}

export const byStrings = (a: any, b: any) => {
const res = `${a}`.localeCompare(`${b}`);
export const alphabetically = (a: any, b: any) => {
const res = `${a}`.localeCompare(`${b}`, 'en-US', {
usage: 'sort',
numeric: true,
ignorePunctuation: false
});
return res < 0 ? -1 : res > 0 ? 1 : 0;
};

Expand Down
4 changes: 2 additions & 2 deletions test/types/community/collection/filterQuery.test-d.ts
Expand Up @@ -94,7 +94,7 @@ const spot = {
treats: ['kibble', 'bone'],
playTimePercent: new Decimal128('0.999999'),

binary: new Binary('', 2),
binary: new Binary(Buffer.from('', 'utf8'), 2),
code: new Code(() => true),
minKey: new MinKey(),
maxKey: new MaxKey(),
Expand Down Expand Up @@ -161,7 +161,7 @@ expectNotType<Filter<PetModel>>({ bestFriend: [spot] });
expectNotType<Filter<PetModel>>({ bestFriend: [{ name: 'Andersons' }] });

// it should permit all our BSON types as query values
expectAssignable<Filter<PetModel>>({ binary: new Binary('', 2) });
expectAssignable<Filter<PetModel>>({ binary: new Binary(Buffer.from('abc', 'utf8'), 2) });
expectAssignable<Filter<PetModel>>({ code: new Code(() => true) });
expectAssignable<Filter<PetModel>>({ minKey: new MinKey() });
expectAssignable<Filter<PetModel>>({ maxKey: new MaxKey() });
Expand Down
8 changes: 6 additions & 2 deletions test/unit/client-side-encryption/auto_encrypter.test.ts
Expand Up @@ -191,8 +191,12 @@ describe('AutoEncrypter', function () {
expect(decrypted.filter[Symbol.for('@@mdb.decryptedKeys')]).to.eql(['ssn']);

// The same, but with an object containing different data types as the input
decrypted = await mc.decrypt({ a: [null, 1, { c: new bson.Binary('foo', 1) }] });
expect(decrypted).to.eql({ a: [null, 1, { c: new bson.Binary('foo', 1) }] });
decrypted = await mc.decrypt({
a: [null, 1, { c: new bson.Binary(Buffer.from('foo', 'utf8'), 1) }]
});
expect(decrypted).to.eql({
a: [null, 1, { c: new bson.Binary(Buffer.from('foo', 'utf8'), 1) }]
});
expect(decrypted).to.not.have.property(Symbol.for('@@mdb.decryptedKeys'));

// The same, but with nested data inside the decrypted input
Expand Down
6 changes: 3 additions & 3 deletions test/unit/index.test.ts
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
// Exception to the import from mongodb rule we're unit testing our public API
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import * as mongodb from '../../src/index';
import { byStrings, sorted } from '../tools/utils';
import { alphabetically, sorted } from '../tools/utils';

/**
* TS-NODE Adds these keys but they are undefined, they are not present when you import from lib
Expand Down Expand Up @@ -134,8 +134,8 @@ const EXPECTED_EXPORTS = [

describe('mongodb entrypoint', () => {
it('should export all and only the expected keys in expected_exports', () => {
expect(sorted(Object.keys(mongodb), byStrings)).to.deep.equal(
sorted(EXPECTED_EXPORTS, byStrings)
expect(sorted(Object.keys(mongodb), alphabetically)).to.deep.equal(
sorted(EXPECTED_EXPORTS, alphabetically)
);
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/sessions.test.js
Expand Up @@ -119,7 +119,7 @@ describe('Sessions - unit', function () {
it('sets clusterTime to the one provided when the signature.keyId is a bigint', () => {
const validClusterTime = {
clusterTime: new BSON.Timestamp(BSON.Long.fromNumber(1, true)),
signature: { hash: new BSON.Binary('test'), keyId: 100n }
signature: { hash: new BSON.Binary(Buffer.from('test', 'utf8')), keyId: 100n }
};

session.advanceClusterTime(validClusterTime);
Expand Down

0 comments on commit 91152b9

Please sign in to comment.