diff --git a/.evergreen/scripts/setup_tests.py b/.evergreen/scripts/setup_tests.py index c12599cbc0..ed98064d80 100644 --- a/.evergreen/scripts/setup_tests.py +++ b/.evergreen/scripts/setup_tests.py @@ -57,13 +57,15 @@ # Map the test name to test group. GROUP_MAP = dict(fuzz="fuzz", mockupdb="mockupdb", perf="perf") -# The test files run under ASan/UBSan (PYTHON-6048). They exercise the -# buffer-protocol acquisition path in bson/_cbsonmodule.c -# (_get_buffer/_prepare_input_buffer) and the wire-message builders in +# The test files run under ASan/UBSan (PYTHON-6048, PYTHON-6285). They +# exercise the buffer-protocol acquisition path in bson/_cbsonmodule.c +# (_get_buffer/_prepare_input_buffer), the wire-message builders in # pymongo/_cmessagemodule.c (_op_msg, _batched_op_msg, -# _encode_batched_write_command). +# _encode_batched_write_command), and the hostile-input and boundary tests +# driving the decoder and builders directly. SANITIZER_FILES = [ "test/test_bson.py", + "test/test_message.py", "test/test_raw_bson.py", "test/test_raw_bson_shared.py", "test/test_client.py", diff --git a/test/test_bson.py b/test/test_bson.py index 0380d1f22e..8f1e549edd 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -31,6 +31,7 @@ import uuid from collections import OrderedDict, abc from io import BytesIO +from typing import Any sys.path[0:0] = [""] @@ -63,7 +64,8 @@ from bson.codec_options import CodecOptions, DatetimeConversion from bson.datetime_ms import _DATETIME_ERROR_SUGGESTION from bson.dbref import DBRef -from bson.errors import InvalidBSON, InvalidDocument +from bson.decimal128 import Decimal128 +from bson.errors import BSONError, InvalidBSON, InvalidDocument from bson.int64 import Int64 from bson.max_key import MaxKey from bson.min_key import MinKey @@ -2081,5 +2083,191 @@ def test_long_long_to_string(self): print("_cbson was not imported. Check compilation logs.") +def _nested_document(depth: int) -> dict[str, Any]: + doc: dict[str, Any] = {"leaf": 1} + for _ in range(depth): + doc = {"d": doc} + return doc + + +def _extreme_typed_documents() -> list[dict[str, Any]]: + """One document per BSON type, at value extremes where applicable.""" + return [ + {}, + {"int32": 1}, + {"int64 max": Int64(2**63 - 1), "int64 min": Int64(-(2**63))}, + {"double": 1.5, "double min": sys.float_info.min, "double max": sys.float_info.max}, + {"string": "hello é", "string empty": ""}, + {"string big": "x" * 70000}, + { + "binary": Binary(b"\x00\xffbin", 0), + "binary old": Binary(b"old", 2), + "binary empty": Binary(b"", 0), + }, + { + "datetime zero": DatetimeMS(0), + "datetime min": DatetimeMS(-(2**63)), + "datetime max": DatetimeMS(2**63 - 1), + }, + {"timestamp": Timestamp(0, 1), "timestamp max": Timestamp(0xFFFFFFFF, 0xFFFFFFFF)}, + { + "decimal128": Decimal128("1.25"), + "decimal128 nan": Decimal128("NaN"), + "decimal128 inf": Decimal128("Infinity"), + "decimal128 neg0": Decimal128("-0"), + }, + {"regex": Regex("a*b", "im"), "regex empty": Regex("", "")}, + {"code": Code("function() { return 1; }"), "code scope": Code("f", {"x": [1, 2]})}, + { + "dbref": DBRef("coll", "id"), + "objectid": ObjectId(b"0123456789ab"), + "minkey": MinKey(), + "maxkey": MaxKey(), + "null": None, + "bool": True, + }, + {"array": [1, "two", None, {"nested": True}], "subdoc": {"b": {"c": 1}}}, + {"bytes": b"raw bytes"}, + {"key" * 63 + "e": 1}, + {"deep": _nested_document(50)}, + ] + + +# Maximum int32 value; the largest length field a malformed document can claim. +_MAX_INT32 = 0x7FFFFFFF + +_DATETIME_MS_CODEC = CodecOptions(datetime_conversion=DatetimeConversion.DATETIME_MS) + + +class TestBSONEncodeDecodeBoundaries(unittest.TestCase): + """Encode every BSON type at value extremes and re-encode byte-stably. + + The boundary-sized documents walk the C encoder's buffer through its + doubling thresholds (bson/buffer.c), and the string, binary, and regex + values exercise the C encoder's _downcast_and_check length validations, + all under sanitizer builds. + """ + + def _round_trip(self, doc): + encoded = bson.encode(doc, codec_options=_DATETIME_MS_CODEC) + decoded = bson.decode(encoded, codec_options=_DATETIME_MS_CODEC) + # Compare byte-level stability rather than decoded == doc so that + # NaN payloads (which are not equal to themselves) round-trip too. + self.assertEqual(bson.encode(decoded, codec_options=_DATETIME_MS_CODEC), encoded) + + def test_round_trip_value_extremes(self): + for doc in _extreme_typed_documents(): + with self.subTest(next(iter(doc), "empty")): + self._round_trip(doc) + + def test_round_trip_size_boundaries(self): + # Encoded sizes that exactly fill the C encoder's buffer at each + # doubling boundary (no realloc), and one byte more (forces a + # realloc). Encoding {"a": "x" * n} adds 13 bytes of BSON + # overhead, so use n = capacity - 13 and capacity - 12. A capacity + # of _MAX_INT32 (the maximum BSON document size) is deliberately + # excluded: exercising buffer_grow's > INT_MAX/2 branch would + # require allocating a multi-GiB document. + for capacity in (256, 512, 1024, 2**16, 2**17): + for size in (capacity - 13, capacity - 12): + with self.subTest(capacity=capacity, size=size): + self._round_trip({"a": "x" * size}) + + +class TestBSONHostileLengths(unittest.TestCase): + """Decode must reject malformed length fields with a BSONError. + + Exercises the C decoder's int-boundary validation without allocating + multi-GiB buffers. + """ + + def _assert_bson_error(self, data, msg=None): + with self.assertRaises(BSONError, msg=msg): + bson.decode(data) + + def test_declared_document_lengths(self): + base = bson.encode({"a": "b" * 20, "c": [1, 2]}) + for length in (0, -1, 0x7F7F7F7F, _MAX_INT32, len(base) + 1): + with self.subTest(length=length): + data = bytearray(base) + data[0:4] = struct.pack(" 8192. The + # later documents are small, so only the first drives the growth. + docs = [{"s": "x" * n} for n in (5000, 50, 5)] + _, msg, _, max_doc_size = _cmessage._op_msg(0, {"insert": "coll"}, "documents", docs, _OPTS) + self._assert_total_length(msg) + self.assertEqual(max_doc_size, max(len(encode(doc)) for doc in docs)) + + def test_batched_op_msg_insert(self): + docs = [{"_id": i, "s": "x" * 10} for i in range(3)] + _, msg, to_publish = _cmessage._batched_op_msg( + 0, {"insert": "coll"}, docs, True, _OPTS, _FakeBulkWriteContext() + ) + self.assertIsInstance(to_publish, list) + self._assert_total_length(msg) + command_size = struct.unpack("