Skip to content

PYTHON-6285 Add hostile-input and boundary tests for the BSON and message C extensions - #3233

Open
blink1073 wants to merge 4 commits into
mongodb:mainfrom
blink1073:PYTHON-6285
Open

blink1073 wants to merge 4 commits into
mongodb:mainfrom
blink1073:PYTHON-6285

Conversation

@blink1073

@blink1073 blink1073 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

PYTHON-6285

Changes in this PR

Adds deterministic hostile-input and boundary tests for the C BSON decoder and OP_MSG builders, with no production code changes. Ports the test-only portion of PYTHON-6168 (its TSan work stays with that ticket) and adds the OP_MSG tests to the ASan/UBSan sanitizer task.

  • Round-trips every BSON type at value extremes, plus string sizes that cross buffer growth boundaries.
  • Feeds the decoder forged length fields, truncations, and byte mutations; every input must be rejected cleanly or decode without a crash.
  • Drives the C OP_MSG builders directly and asserts their wire layout.

Test Plan

  • New tests: 14 passed with the C extensions built (pytest test/test_bson.py -k "Boundaries or Hostile or Mutation", pytest test/test_message.py -k OpMsg), 14 passed against the pure-Python decoder, clean skips when _cmessage is absent.
  • Full test_bson.py and test_message.py suites: 158 passed, 2 pre-existing skips; mypy and pre-commit hooks clean.
  • BSON fuzz targets over the regression reproducers and seed corpus, 7.6M executions: no findings.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)? Test-only change.
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s). TSan coverage will be added in PYTHON-6168.

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The boundary tests use payload sizes that place both documents in each pair within the same buffer capacity, missing the intended exact-fit and one-byte-overflow cases.

1 open issue
What changed in this PR

Adds deterministic robustness tests for PyMongo’s BSON decoder and C OP_MSG builders, extending sanitizer coverage without changing production code. ThreadSanitizer work remains separate under PYTHON-6168.

Changes:

  • Adds BSON round-trip, forged-length, truncation, and byte-mutation tests.
  • Exercises C OP_MSG builders directly with wire-layout and buffer-growth checks.
  • Includes message tests in the ASan/UBSan task.
File Description
test/​test_message.py Adds direct C OP_MSG builder tests, skipped when the extension is unavailable.
test/​test_bson.py Adds value-extreme, size-boundary, and hostile-input BSON tests.
.evergreen/​scripts/​setup_tests.py Adds message tests to sanitizer coverage and updates explanatory comments.

Review effort: Balanced

Comment thread test/test_bson.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified in the BSON and wire-message test additions or the expanded sanitizer coverage.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@blink1073
blink1073 marked this pull request as ready for review November 17, 2026 17:20
@blink1073
blink1073 requested a review from a team as a code owner November 17, 2026 17:20
@blink1073
blink1073 requested a review from NoahStapp November 17, 2026 17:20
Comment thread test/test_message.py
max_message_size = 48 * 1024 * 1024


class TestOpMsgBuilders(unittest.TestCase):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also have a case that forces a batch split due to the number of documents exceeding max_write_batch_size?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/test_message.py
self.assertEqual(max_doc_size, 0)

def test_op_msg_multiple_buffer_doublings(self):
docs = [{"s": "x" * n} for n in (5000, 50, 5)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this have a comment explaining how/why these sizes cause the buffer to double multiple times? It's not obvious from the code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/test_message.py
1, {"update": "coll"}, docs, True, _OPTS, _FakeBulkWriteContext()
)
self.assertEqual(struct.unpack("<i", msg[:4])[0], len(msg))
self.assertIsInstance(to_publish, list)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also check the length of to_publish, similar to the test above?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/test_bson.py Outdated
return doc


def _typed_documents() -> list[dict[str, Any]]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _typed_documents() -> list[dict[str, Any]]:
def _extreme_typed_documents() -> list[dict[str, Any]]:

The name is a little too generic considering this is specifically about extreme values.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment thread test/test_bson.py Outdated
class TestBSONEncodeDecodeBoundaries(unittest.TestCase):
"""Encode every BSON type at value extremes and re-encode byte-stably.

Drives buffer growth transitions and every _downcast_and_check path

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this sentence means. Do the tests cause buffer growth transitions intentionally? What happens to every _downcast_and_check path exactly?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment thread test/test_bson.py Outdated
def test_element_string_lengths(self):
base = bson.encode({"a": "x" * 20})
# The string length field starts at offset 7.
for length in (-1, 0x7FFFFFFF):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x7FFFFFFF should be a defined constant since it's used several times in this file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/test_bson.py Outdated
Comment on lines +2238 to +2250
def test_mutations_decode_cleanly(self):
for doc in _typed_documents():
data = bson.encode(doc, codec_options=_DATETIME_MS_CODEC)
for corrupted in self._mutated(data):
try:
bson.decode(corrupted, codec_options=_DATETIME_MS_CODEC)
except (BSONError, ValueError):
pass
for truncated in self._truncated(data):
try:
bson.decode(truncated, codec_options=_DATETIME_MS_CODEC)
except (BSONError, ValueError):
pass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_mutations_decode_cleanly(self):
for doc in _typed_documents():
data = bson.encode(doc, codec_options=_DATETIME_MS_CODEC)
for corrupted in self._mutated(data):
try:
bson.decode(corrupted, codec_options=_DATETIME_MS_CODEC)
except (BSONError, ValueError):
pass
for truncated in self._truncated(data):
try:
bson.decode(truncated, codec_options=_DATETIME_MS_CODEC)
except (BSONError, ValueError):
pass
def test_mutations_decode_cleanly(self):
def _assert_decodes_or_errors(data):
for doc in data:
try:
bson.decode(doc, codec_options=_DATETIME_MS_CODEC)
except (BSONError, ValueError):
pass
for doc in _typed_documents():
data = bson.encode(doc, codec_options=_DATETIME_MS_CODEC)
_assert_encodes_or_errors(self._mutated(data)
_assert_encodes_or_errors(self._truncated(data)

Reduce duplication.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied

Comment thread test/test_message.py Outdated
def test_op_msg_no_identifier(self):
_, msg, total_size, max_doc_size = _cmessage._op_msg(0, {"ismaster": 1}, "", None, _OPTS)
self.assertIsInstance(msg, bytes)
self.assertEqual(struct.unpack("<i", msg[:4])[0], len(msg))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct.unpack("<i", msg[:4])[0] is repeated 6 times, should it be a helper method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/test_message.py Outdated
self.assertIsInstance(msg, bytes)
self.assertEqual(struct.unpack("<i", msg[:4])[0], len(msg))
# 21 bytes of header precede the command document.
self.assertEqual(total_size, len(msg) - 21)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 21 bytes of header should be a defined constant.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/test_bson.py Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# realloc). Encoding {"a": "x" * n} adds 13 bytes of BSON
# realloc). Encoding {"a": "x" * n} adds 13 bytes of BSON

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@blink1073
blink1073 requested a review from NoahStapp November 17, 2026 19:25
Comment thread test/test_bson.py Outdated
Comment on lines +2258 to +2263
for doc in _extreme_typed_documents():
data = bson.encode(doc, codec_options=_DATETIME_MS_CODEC)
for corrupted in self._mutated(data):
self._assert_decodes_or_errors(corrupted)
for truncated in self._truncated(data):
self._assert_decodes_or_errors(truncated)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each self._assert_decodes_or_errors call should be in its own subtest.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread test/test_bson.py
# 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.
for capacity in (256, 512, 1024, 2**16, 2**17):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have a case that tests against _MAX_INT32?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not, it would require too much memory, added a comment.

Comment thread test/test_bson.py Outdated
"""Decode must either succeed or raise; never crash."""
try:
bson.decode(data, codec_options=_DATETIME_MS_CODEC)
except (BSONError, ValueError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are either of these equally valid? Or should we expect decode to always raise a BSONError?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tightened to only BSONError

@blink1073
blink1073 requested a review from NoahStapp November 17, 2026 21:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants