Skip to content

Commit 12a698d

Browse files
committed
PYTHON-1029 - Assert messageLength <= maxMessageSizeBytes
1 parent c34eec8 commit 12a698d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

pymongo/network.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
_HAS_POLL = False
3232

3333
from pymongo import helpers, message
34+
from pymongo.common import MAX_MESSAGE_SIZE
3435
from pymongo.errors import AutoReconnect, NotMasterError, OperationFailure
3536
from pymongo.read_concern import DEFAULT_READ_CONCERN
3637

@@ -115,7 +116,8 @@ def command(sock, dbname, spec, slave_ok, is_mongos,
115116
return response_doc
116117

117118

118-
def receive_message(sock, operation, request_id):
119+
def receive_message(
120+
sock, operation, request_id, max_message_size=MAX_MESSAGE_SIZE):
119121
"""Receive a raw BSON message or raise socket.error."""
120122
header = _receive_data_on_socket(sock, 16)
121123
length = _UNPACK_INT(header[:4])[0]
@@ -133,6 +135,10 @@ def receive_message(sock, operation, request_id):
133135
assert length > 16, ("wire protocol error: message length is shorter"
134136
" than standard message header: %r" % (length,))
135137

138+
assert length <= max_message_size, (
139+
"wire protocol error: message length (%r) is larger than server max "
140+
"message size (%r)" % (length, max_message_size))
141+
136142
return _receive_data_on_socket(sock, length - 16)
137143

138144

pymongo/pool.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from bson import DEFAULT_CODEC_OPTIONS
2121
from bson.py3compat import u, itervalues
2222
from pymongo import auth, helpers, thread_util
23+
from pymongo.common import MAX_MESSAGE_SIZE
2324
from pymongo.errors import (AutoReconnect,
2425
ConnectionFailure,
2526
ConfigurationError,
@@ -165,7 +166,8 @@ def __init__(self, sock, pool, ismaster, address):
165166
self.is_writable = ismaster.is_writable if ismaster else None
166167
self.max_wire_version = ismaster.max_wire_version if ismaster else None
167168
self.max_bson_size = ismaster.max_bson_size if ismaster else None
168-
self.max_message_size = ismaster.max_message_size if ismaster else None
169+
self.max_message_size = (
170+
ismaster.max_message_size if ismaster else MAX_MESSAGE_SIZE)
169171
self.max_write_batch_size = (
170172
ismaster.max_write_batch_size if ismaster else None)
171173

@@ -238,7 +240,8 @@ def receive_message(self, operation, request_id):
238240
If any exception is raised, the socket is closed.
239241
"""
240242
try:
241-
return receive_message(self.sock, operation, request_id)
243+
return receive_message(
244+
self.sock, operation, request_id, self.max_message_size)
242245
except BaseException as error:
243246
self._raise_connection_failure(error)
244247

0 commit comments

Comments
 (0)