@@ -24,6 +24,7 @@ var Connection = exports.Connection = function(id, socketOptions) {
2424 // Connection parsing state
2525 //
2626 this . maxBsonSize = socketOptions . maxBsonSize ? socketOptions . maxBsonSize : Connection . DEFAULT_MAX_BSON_SIZE ;
27+ this . maxMessageSizeBytes = socketOptions . maxMessageSizeBytes ? socketOptions . maxMessageSizeBytes : Connection . DEFAULT_MAX_MESSAGE_SIZE ;
2728 // Contains the current message bytes
2829 this . buffer = null ;
2930 // Contains the current message size
@@ -42,6 +43,8 @@ var Connection = exports.Connection = function(id, socketOptions) {
4243
4344// Set max bson size
4445Connection . DEFAULT_MAX_BSON_SIZE = 1024 * 1024 * 4 ;
46+ // Set default to max bson to avoid overflow or bad guesses
47+ Connection . DEFAULT_MAX_MESSAGE_SIZE = Connection . DEFAULT_MAX_BSON_SIZE ;
4548
4649// Inherit event emitter so we can emit stuff wohoo
4750inherits ( Connection , EventEmitter ) ;
@@ -183,17 +186,28 @@ Connection.prototype.write = function(command, callback) {
183186 if ( Array . isArray ( command ) ) {
184187 for ( var i = 0 ; i < command . length ; i ++ ) {
185188 var binaryCommand = command [ i ] . toBinary ( )
189+
186190 if ( ! this . socketOptions [ 'disableDriverBSONSizeCheck' ] && binaryCommand . length > this . maxBsonSize )
187- return callback ( new Error ( "Document exceeds maximal allowed bson size of " + this . maxBsonSize + " bytes" ) ) ;
191+ return callback ( new Error ( "Document exceeds maximum allowed bson size of " + this . maxBsonSize + " bytes" ) ) ;
192+
193+ if ( this . socketOptions [ 'disableDriverBSONSizeCheck' ] && binaryCommand . length > this . maxMessageSizeBytes ) {
194+ return callback ( new Error ( "Command exceeds maximum message size of " + this . maxMessageSizeBytes + " bytes" ) ) ;
195+ }
196+
188197 if ( this . logger != null && this . logger . doDebug )
189198 this . logger . debug ( "writing command to mongodb" , { binary : binaryCommand , json : command [ i ] } ) ;
190199
191200 var r = this . writeSteam . write ( binaryCommand ) ;
192201 }
193202 } else {
194203 var binaryCommand = command . toBinary ( )
204+
195205 if ( ! this . socketOptions [ 'disableDriverBSONSizeCheck' ] && binaryCommand . length > this . maxBsonSize )
196- return callback ( new Error ( "Document exceeds maximal allowed bson size of " + this . maxBsonSize + " bytes" ) ) ;
206+ return callback ( new Error ( "Document exceeds maximum allowed bson size of " + this . maxBsonSize + " bytes" ) ) ;
207+
208+ if ( this . socketOptions [ 'disableDriverBSONSizeCheck' ] && binaryCommand . length > this . maxMessageSizeBytes ) {
209+ return callback ( new Error ( "Command exceeds maximum message size of " + this . maxMessageSizeBytes + " bytes" ) ) ;
210+ }
197211
198212 if ( this . logger != null && this . logger . doDebug )
199213 this . logger . debug ( "writing command to mongodb" , { binary : binaryCommand , json : command } ) ;
0 commit comments