PYTHON-5906 Raise ProtocolError for malformed OP_COMPRESSED messages in sync receive_message#2937
Open
aclark4life wants to merge 2 commits into
Open
PYTHON-5906 Raise ProtocolError for malformed OP_COMPRESSED messages in sync receive_message#2937aclark4life wants to merge 2 commits into
aclark4life wants to merge 2 commits into
Conversation
…in sync receive_message
Contributor
There was a problem hiding this comment.
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 synchronousreceive_message, raisingProtocolError. - Add a sync-only regression test asserting
ProtocolErrorfor 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. |
|
Assigned |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PYTHON-5906
Changes in this PR
The sync
receive_messageinpymongo/network_layer.pywas missing the OP_COMPRESSED minimum-length guard that exists in the asyncPyMongoProtocol.process_header. For a malformed OP_COMPRESSED frame with16 < length <= 24,length - 25is negative, soreceive_datacallsbytearray(negative)and raisesValueErrorinstead ofProtocolError; forlength == 25,decompresswas called on 0 bytes.Added the same guard before entering the
op_code == 2012branch so the sync path raisesProtocolError, matching async parity. No public API changes.Test Plan
Added
test_compressed_length_too_small_raisestotest/test_network_layer.py, covering lengths 17, 24, and 25 and assertingProtocolError. All tests in the file pass.Checklist
Checklist for Author
Checklist for Reviewer