Skip to content

Commit 32f36e4

Browse files
author
Emily Giurleo
committed
RUBY-2241 Have driver raise MaxBSONSizeError when document exceeds MaxBsonObjectSize (#2033)
1 parent 8a6bf43 commit 32f36e4

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

Diff for: lib/mongo/protocol/msg.rb

+19
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ def payload
147147
#
148148
# @since 2.5.0
149149
def serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil)
150+
validate_document_size!(max_bson_size)
151+
150152
super
151153
add_check_sum(buffer)
152154
buffer
@@ -275,6 +277,23 @@ def bulk_write?
275277

276278
private
277279

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

Diff for: spec/integration/client_side_encryption/auto_encryption_bulk_writes_spec.rb

+3-3
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

Diff for: spec/integration/size_limit_spec.rb

+23-6
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)