Skip to content

Commit 3809c13

Browse files
daprahamianmbroadst
authored andcommitted
fix: throw if invalid _bsontype is detected
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
1 parent e4de7b5 commit 3809c13

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/bson/parser/serializer.js

+6
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,8 @@ var serializeInto = function serializeInto(
777777
index = serializeInt32(buffer, key, value, index, true);
778778
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
779779
index = serializeMinMax(buffer, key, value, index, true);
780+
} else if (typeof value['_bsontype'] !== 'undefined') {
781+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
780782
}
781783
}
782784
} else if (object instanceof Map) {
@@ -875,6 +877,8 @@ var serializeInto = function serializeInto(
875877
index = serializeInt32(buffer, key, value, index);
876878
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
877879
index = serializeMinMax(buffer, key, value, index);
880+
} else if (typeof value['_bsontype'] !== 'undefined') {
881+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
878882
}
879883
}
880884
} else {
@@ -977,6 +981,8 @@ var serializeInto = function serializeInto(
977981
index = serializeInt32(buffer, key, value, index);
978982
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
979983
index = serializeMinMax(buffer, key, value, index);
984+
} else if (typeof value['_bsontype'] !== 'undefined') {
985+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
980986
}
981987
}
982988
}

test/node/bson_test.js

+21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var Buffer = require('buffer').Buffer,
2121
vm = require('vm');
2222

2323
var createBSON = require('../utils');
24+
var M = require('../../lib/bson/map');
2425

2526
// for tests
2627
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -2400,3 +2401,23 @@ exports['Should serialize _bsontype=ObjectID (capital D) from v4.0.0/4.0.1'] = f
24002401
test.equal(doc._id.toHexString(), createBSON().deserialize(serialized_data)._id.toHexString());
24012402
test.done();
24022403
};
2404+
2405+
exports['should throw if invalid BSON types are input to BSON serializer'] = function(test) {
2406+
var oid = new ObjectId('111111111111111111111111');
2407+
var badBsonType = new ObjectId('111111111111111111111111');
2408+
badBsonType._bsontype = 'bogus';
2409+
var badDoc = { bad: badBsonType };
2410+
var badArray = [oid, badDoc];
2411+
var badMap = new M([['a', badBsonType], ['b', badDoc], ['c', badArray]]);
2412+
var BSON = createBSON();
2413+
test.throws(function() {
2414+
BSON.serialize(badDoc);
2415+
});
2416+
test.throws(function() {
2417+
BSON.serialize(badArray);
2418+
});
2419+
test.throws(function() {
2420+
BSON.serialize(badMap);
2421+
});
2422+
test.done();
2423+
}

0 commit comments

Comments
 (0)