Skip to content

Commit

Permalink
fix: throw if invalid _bsontype is detected
Browse files Browse the repository at this point in the history
If an invalid value for `_bsontype` is encountered during object
serialization, the serializer will now throw an error rather than
skipping the value completely.

NODE-2514
  • Loading branch information
daprahamian authored and mbroadst committed Mar 24, 2020
1 parent e4de7b5 commit 3809c13
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/bson/parser/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,8 @@ var serializeInto = function serializeInto(
index = serializeInt32(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index, true);
} else if (typeof value['_bsontype'] !== 'undefined') {
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
}
}
} else if (object instanceof Map) {
Expand Down Expand Up @@ -875,6 +877,8 @@ var serializeInto = function serializeInto(
index = serializeInt32(buffer, key, value, index);
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index);
} else if (typeof value['_bsontype'] !== 'undefined') {
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
}
}
} else {
Expand Down Expand Up @@ -977,6 +981,8 @@ var serializeInto = function serializeInto(
index = serializeInt32(buffer, key, value, index);
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index);
} else if (typeof value['_bsontype'] !== 'undefined') {
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var Buffer = require('buffer').Buffer,
vm = require('vm');

var createBSON = require('../utils');
var M = require('../../lib/bson/map');

// for tests
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
Expand Down Expand Up @@ -2400,3 +2401,23 @@ exports['Should serialize _bsontype=ObjectID (capital D) from v4.0.0/4.0.1'] = f
test.equal(doc._id.toHexString(), createBSON().deserialize(serialized_data)._id.toHexString());
test.done();
};

exports['should throw if invalid BSON types are input to BSON serializer'] = function(test) {
var oid = new ObjectId('111111111111111111111111');
var badBsonType = new ObjectId('111111111111111111111111');
badBsonType._bsontype = 'bogus';
var badDoc = { bad: badBsonType };
var badArray = [oid, badDoc];
var badMap = new M([['a', badBsonType], ['b', badDoc], ['c', badArray]]);
var BSON = createBSON();
test.throws(function() {
BSON.serialize(badDoc);
});
test.throws(function() {
BSON.serialize(badArray);
});
test.throws(function() {
BSON.serialize(badMap);
});
test.done();
}

0 comments on commit 3809c13

Please # to comment.