Skip to content

Commit a66304f

Browse files
committed
Fix BSON size validation PYTHON-370
1 parent 8ef1bd5 commit a66304f

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

bson/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ def _elements_to_dict(data, as_class, tz_aware):
311311

312312
def _bson_to_dict(data, as_class, tz_aware):
313313
obj_size = struct.unpack("<i", data[:4])[0]
314-
if len(data) < obj_size:
314+
length = len(data)
315+
if length < obj_size:
315316
raise InvalidBSON("objsize too large")
316-
if data[obj_size - 1:obj_size] != ZERO:
317+
if obj_size != length or data[obj_size - 1:obj_size] != ZERO:
317318
raise InvalidBSON("bad eoo")
318319
elements = data[4:obj_size - 1]
319320
return (_elements_to_dict(elements, as_class, tz_aware), data[obj_size:])
@@ -572,4 +573,4 @@ def has_uuid():
572573
573574
.. versionadded:: 2.2.1+
574575
"""
575-
return _use_uuid
576+
return _use_uuid

bson/_cbsonmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ static PyObject* _cbson_bson_to_dict(PyObject* self, PyObject* args) {
16281628
return NULL;
16291629
}
16301630

1631-
if (string[size - 1]) {
1631+
if (size != total_size || string[size - 1]) {
16321632
PyObject* InvalidBSON = _error("InvalidBSON");
16331633
PyErr_SetString(InvalidBSON,
16341634
"bad eoo");

test/test_bson.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_basic_validation(self):
7474
self.assertFalse(is_valid(b("\x05\x00\x00\x00\x00\x00")))
7575
self.assertFalse(is_valid(b("\x07\x00\x00\x00\x02a\x00\x78\x56\x34\x12")))
7676
self.assertFalse(is_valid(b("\x09\x00\x00\x00\x10a\x00\x05\x00")))
77+
self.assertFalse(is_valid(b("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")))
7778

7879
def test_random_data_is_not_bson(self):
7980
qcheck.check_unittest(self, qcheck.isnt(is_valid),

0 commit comments

Comments
 (0)