Skip to content

PYTHON-5906 Raise ProtocolError for malformed OP_COMPRESSED messages in sync receive_message#2937

Open
aclark4life wants to merge 2 commits into
mongodb:mainfrom
aclark4life:PYTHON-5906-op-compressed-guard
Open

PYTHON-5906 Raise ProtocolError for malformed OP_COMPRESSED messages in sync receive_message#2937
aclark4life wants to merge 2 commits into
mongodb:mainfrom
aclark4life:PYTHON-5906-op-compressed-guard

Conversation

@aclark4life

@aclark4life aclark4life commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

PYTHON-5906

Changes in this PR

The sync receive_message in pymongo/network_layer.py was missing the OP_COMPRESSED minimum-length guard that exists in the async PyMongoProtocol.process_header. For a malformed OP_COMPRESSED frame with 16 < length <= 24, length - 25 is negative, so receive_data calls bytearray(negative) and raises ValueError instead of ProtocolError; for length == 25, decompress was called on 0 bytes.

Added the same guard before entering the op_code == 2012 branch so the sync path raises ProtocolError, matching async parity. No public API changes.

Test Plan

Added test_compressed_length_too_small_raises to test/test_network_layer.py, covering lengths 17, 24, and 25 and asserting ProtocolError. All tests in the file pass.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

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?

@aclark4life
aclark4life marked this pull request as ready for review July 16, 2026 19:26
@aclark4life
aclark4life requested a review from a team as a code owner July 16, 2026 19:26
@aclark4life
aclark4life requested review from blink1073 and Copilot July 16, 2026 19:26

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.

Pull request overview

Aligns the synchronous wire-protocol receive path (receive_message) with the existing async PyMongoProtocol.process_header behavior by rejecting malformed OP_COMPRESSED messages whose declared length is too small to include the compression sub-header plus payload. This ensures malformed frames raise ProtocolError (instead of bubbling up ValueError from an invalid receive_data size or attempting to decompress an empty payload).

Changes:

  • Add a minimum-length guard for OP_COMPRESSED (length <= 25) in synchronous receive_message, raising ProtocolError.
  • Add a sync-only regression test asserting ProtocolError for malformed compressed message lengths (17, 24, 25).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pymongo/network_layer.py Adds an OP_COMPRESSED minimum-length check in sync receive_message to raise ProtocolError for malformed frames.
test/test_network_layer.py Adds a regression test covering malformed OP_COMPRESSED lengths that previously could raise ValueError or attempt decompression on empty payload.

@codeowners-service-app

Copy link
Copy Markdown

Assigned sleepyStick for team dbx-python because blink1073 is out of office.

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.

2 participants