Skip to content

Commit 0706b90

Browse files
author
Emily Giurleo
committed
RUBY-2241 Have driver raise MaxBSONSizeError when document exceeds MaxBsonObjectSize (#2033)
1 parent 44184e1 commit 0706b90

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

lib/mongo/protocol/msg.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ def payload
140140
#
141141
# @since 2.5.0
142142
def serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil)
143+
validate_document_size!(max_bson_size)
144+
143145
super
144146
add_check_sum(buffer)
145147
buffer
@@ -268,6 +270,23 @@ def bulk_write?
268270

269271
private
270272

273+
# Validate that the documents in this message are all smaller than the
274+
# maxBsonObjectSize. If not, raise an exception.
275+
def validate_document_size!(max_bson_size)
276+
max_bson_size ||= Mongo::Server::ConnectionBase::DEFAULT_MAX_BSON_OBJECT_SIZE
277+
278+
contains_too_large_document = @sections.any? do |section|
279+
section[:type] == 1 &&
280+
section[:payload][:sequence].any? do |document|
281+
document.to_bson.length > max_bson_size
282+
end
283+
end
284+
285+
if contains_too_large_document
286+
raise Error::MaxBSONSize.new('The document exceeds maximum allowed BSON object size after serialization')
287+
end
288+
end
289+
271290
def command
272291
@command ||= if @main_document
273292
@main_document.dup.tap do |cmd|

spec/integration/client_side_encryption/auto_encryption_bulk_writes_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
it 'raises an exception' do
114114
expect do
115115
bulk_write.execute
116-
end.to raise_error(Mongo::Error::MaxBSONSize, /maximum allowed size: 16777216 bytes/)
116+
end.to raise_error(Mongo::Error::MaxBSONSize, /The document exceeds maximum allowed BSON object size after serialization/)
117117
end
118118
end
119119
end
@@ -255,7 +255,7 @@
255255
it 'raises an exception' do
256256
expect do
257257
bulk_write.execute
258-
end.to raise_error(Mongo::Error::MaxBSONSize, /maximum allowed size: 16777216 bytes/)
258+
end.to raise_error(Mongo::Error::MaxBSONSize, /The document exceeds maximum allowed BSON object size after serialization/)
259259
end
260260
end
261261
end
@@ -346,7 +346,7 @@
346346
it 'raises an exception' do
347347
expect do
348348
perform_bulk_write
349-
end.to raise_error(Mongo::Error::MaxBSONSize, /maximum allowed size: 16777216 bytes/)
349+
end.to raise_error(Mongo::Error::MaxBSONSize, /The document exceeds maximum allowed BSON object size after serialization/)
350350
end
351351
end
352352
end

spec/integration/size_limit_spec.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,30 @@
6262
authorized_collection.insert_one(document)
6363
end
6464

65-
it 'fails on the server when a document larger than 16MiB is inserted' do
66-
document = { key: 'a' * (max_document_size - 27), _id: 'foo' }
67-
expect(document.to_bson.length).to eq(max_document_size+1)
65+
context 'on server versions >= 3.6' do
66+
min_server_fcv '3.6'
6867

69-
lambda do
70-
authorized_collection.insert_one(document)
71-
end.should raise_error(Mongo::Error::OperationFailure, /object to insert too large/)
68+
it 'fails on the driver when a document larger than 16MiB is inserted' do
69+
document = { key: 'a' * (max_document_size - 27), _id: 'foo' }
70+
expect(document.to_bson.length).to eq(max_document_size+1)
71+
72+
lambda do
73+
authorized_collection.insert_one(document)
74+
end.should raise_error(Mongo::Error::MaxBSONSize, /The document exceeds maximum allowed BSON object size after serialization/)
75+
end
76+
end
77+
78+
context 'on server versions <= 3.4' do
79+
max_server_fcv '3.4'
80+
81+
it 'fails on the server when a document larger than 16MiB is inserted' do
82+
document = { key: 'a' * (max_document_size - 27), _id: 'foo' }
83+
expect(document.to_bson.length).to eq(max_document_size+1)
84+
85+
lambda do
86+
authorized_collection.insert_one(document)
87+
end.should raise_error(Mongo::Error::OperationFailure, /object to insert too large/)
88+
end
7289
end
7390

7491
it 'fails in the driver when a document larger than 16MiB+16KiB is inserted' do

0 commit comments

Comments
 (0)