Skip to content

Commit

Permalink
fix: validate atomic operations in all update methods
Browse files Browse the repository at this point in the history
We were only validating update operations for atomic operators in
the non-pipelined case for `updateMany` and `findOneAndUpdate`.

Fixes NODE-1920
  • Loading branch information
mbroadst authored and daprahamian committed Aug 13, 2019
1 parent 037d42d commit 88bb77e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
10 changes: 1 addition & 9 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,15 +689,7 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

let err;
if (Array.isArray(update)) {
for (let i = 0; !err && i < update.length; i++) {
err = checkForAtomicOperators(update[i]);
}
} else {
err = checkForAtomicOperators(update);
}

const err = checkForAtomicOperators(update);
if (err) {
if (typeof callback === 'function') return callback(err);
return this.s.promiseLibrary.reject(err);
Expand Down
7 changes: 5 additions & 2 deletions lib/operations/collection_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,17 @@ function bulkWrite(coll, operations, options, callback) {

// Check the update operation to ensure it has atomic operators.
function checkForAtomicOperators(update) {
const keys = Object.keys(update);
const keys = Array.isArray(update)
? update.reduce((keys, u) => keys.concat(Object.keys(u)), [])
: Object.keys(update);

// same errors as the server would give for update doc lacking atomic operators
if (keys.length === 0) {
return toError('The update operation document must contain at least one atomic operator.');
}

if (keys[0][0] !== '$') {
const foundInvalid = keys.some(key => key[0] !== '$');
if (foundInvalid) {
return toError('the update operation document must contain atomic operators.');
}
}
Expand Down

0 comments on commit 88bb77e

Please sign in to comment.