Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bson/_cbsonmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,7 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
}
memcpy(&length, buffer + *position, 4);
length = BSON_UINT32_FROM_LE(length);
if (max < length) {
if (length > max - 5) { // Account for 5-byte header. max >= 5 guaranteed above
goto invalid;
}

Expand Down
16 changes: 16 additions & 0 deletions test/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,22 @@ def __repr__(self):
encode(doc)
self.assertEqual(cm.exception.document, doc)

def test_binary_length_accounts_for_header(self):
size = 20
binary_length = 12 # 5 more than the actual 7 bytes

payload = b""
payload += struct.pack("<i", size) # document size
payload += b"\x05" # type = Binary
payload += b"a\x00" # key "a"
payload += struct.pack("<I", binary_length) # Binary length (inflated)
payload += b"\x00" # subtype 0
payload += b"\x41" * 7 # value
payload += b"\x00" # EOO

with self.assertRaises(InvalidBSON):
decode(payload)


class TestCodecOptions(unittest.TestCase):
def test_document_class(self):
Expand Down
Loading