Skip to content

Commit 934a43a

Browse files
durranmbroadst
authored andcommitted
fix(executeOperation): don't mutate options passed to commands
When the topology believes it has session support, options passed to commands were getting mutated with the session added to them. It is our expectation that the driver will never be mutating arguments passed to it via the public facing API. This commit creates a new object and copies into it, then replaces in the args array.
1 parent 46e14d1 commit 934a43a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ const executeOperation = (topology, operation, args, options) => {
383383
opOptions = args[args.length - 2];
384384
if (opOptions == null || opOptions.session == null) {
385385
session = topology.startSession();
386-
Object.assign(args[args.length - 2], { session: session });
386+
const optionsIndex = args.length - 2;
387+
args[optionsIndex] = Object.assign({}, args[optionsIndex], { session: session });
387388
} else if (opOptions.session && opOptions.session.hasEnded) {
388389
throw new MongoError('Use of expired sessions is not permitted');
389390
}

test/unit/sessions/collection_tests.js

+25
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,30 @@ describe('Sessions', function() {
4949
});
5050
}
5151
});
52+
53+
it('does not mutate command options', {
54+
metadata: { requires: { topology: 'single' } },
55+
56+
test: function() {
57+
const options = Object.freeze({});
58+
test.server.setMessageHandler(request => {
59+
const doc = request.document;
60+
if (doc.ismaster) {
61+
request.reply(mock.DEFAULT_ISMASTER_36);
62+
} else if (doc.count) {
63+
request.reply({ ok: 1 });
64+
}
65+
});
66+
67+
return MongoClient.connect(`mongodb://${test.server.uri()}/test`).then(client => {
68+
const coll = client.db('foo').collection('bar');
69+
70+
return coll.count({}, options).then(() => {
71+
expect(options).to.deep.equal({});
72+
return client.close();
73+
});
74+
});
75+
}
76+
});
5277
});
5378
});

0 commit comments

Comments
 (0)