Skip to content

Commit 7b5f965

Browse files
committed
Update operations should check for empty documents
JAVA-1795
1 parent 49654cf commit 7b5f965

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

driver-core/src/main/com/mongodb/connection/UpdateCommandMessage.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ protected UpdateCommandMessage writeTheWrites(final BsonOutput bsonOutput, final
8080
writer.writeName("q");
8181
getCodec(update.getFilter()).encode(writer, update.getFilter(), EncoderContext.builder().build());
8282
writer.writeName("u");
83+
84+
int bufferPosition = bsonOutput.getPosition();
8385
getCodec(update.getUpdate()).encode(writer, update.getUpdate(), EncoderContext.builder().build());
86+
87+
if (update.getType() == WriteRequest.Type.UPDATE && bsonOutput.getPosition() == bufferPosition + 8) {
88+
throw new IllegalArgumentException("Invalid BSON document for an update");
89+
}
90+
8491
if (update.isMulti()) {
8592
writer.writeBoolean("multi", update.isMulti());
8693
}

driver-core/src/main/com/mongodb/connection/UpdateMessage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ protected RequestMessage encodeMessageBody(final BsonOutput bsonOutput, final in
6565
if (updateRequest.getType() == REPLACE) {
6666
addCollectibleDocument(updateRequest.getUpdate(), bsonOutput, new CollectibleDocumentFieldNameValidator());
6767
} else {
68+
int bufferPosition = bsonOutput.getPosition();
6869
addDocument(updateRequest.getUpdate(), bsonOutput, new UpdateFieldNameValidator());
70+
if (bsonOutput.getPosition() == bufferPosition + 5) {
71+
throw new IllegalArgumentException("Invalid BSON document for an update");
72+
}
6973
}
7074

7175
if (updates.size() == 1) {

driver-core/src/test/functional/com/mongodb/operation/MixedBulkWriteOperationAsyncSpecification.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,44 @@ class MixedBulkWriteOperationAsyncSpecification extends OperationFunctionalSpeci
253253
ordered << [true, false]
254254
}
255255

256+
257+
def 'when updating with an empty document, update should throw IllegalArgumentException'() {
258+
given:
259+
def id = new ObjectId()
260+
def op = new MixedBulkWriteOperation(getNamespace(),
261+
[new UpdateRequest(new BsonDocument('_id', new BsonObjectId(id)), new BsonDocument(), UPDATE)],
262+
true, ACKNOWLEDGED)
263+
264+
when:
265+
executeAsync(op)
266+
267+
then:
268+
def ex = thrown(MongoException)
269+
ex.getCause() instanceof IllegalArgumentException
270+
271+
where:
272+
ordered << [true, false]
273+
}
274+
275+
def 'when updating with an invalid document, update should throw IllegalArgumentException'() {
276+
given:
277+
def id = new ObjectId()
278+
def op = new MixedBulkWriteOperation(getNamespace(),
279+
[new UpdateRequest(new BsonDocument('_id', new BsonObjectId(id)), new BsonDocument('a', new BsonInt32(1)), UPDATE)],
280+
true, ACKNOWLEDGED)
281+
282+
when:
283+
executeAsync(op)
284+
285+
then:
286+
def ex = thrown(MongoException)
287+
ex.getCause() instanceof IllegalArgumentException
288+
289+
where:
290+
ordered << [true, false]
291+
}
292+
293+
256294
def 'when a document contains a key with an illegal character, replacing a document with it should throw IllegalArgumentException'() {
257295
given:
258296
def id = new ObjectId()

driver-core/src/test/functional/com/mongodb/operation/MixedBulkWriteOperationSpecification.groovy

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package com.mongodb.operation
1818

1919
import category.Slow
2020
import com.mongodb.ClusterFixture
21+
import com.mongodb.MongoBulkWriteException
2122
import com.mongodb.OperationFunctionalSpecification
2223
import com.mongodb.ReadPreference
2324
import com.mongodb.WriteConcern
2425
import com.mongodb.binding.SingleConnectionBinding
25-
import com.mongodb.MongoBulkWriteException
2626
import com.mongodb.bulk.BulkWriteResult
2727
import com.mongodb.bulk.BulkWriteUpsert
2828
import com.mongodb.bulk.DeleteRequest
@@ -252,6 +252,40 @@ class MixedBulkWriteOperationSpecification extends OperationFunctionalSpecificat
252252
ordered << [true, false]
253253
}
254254

255+
def 'when updating with an empty document, update should throw IllegalArgumentException'() {
256+
given:
257+
def id = new ObjectId()
258+
def op = new MixedBulkWriteOperation(getNamespace(),
259+
[new UpdateRequest(new BsonDocument('_id', new BsonObjectId(id)), new BsonDocument(), UPDATE)],
260+
true, ACKNOWLEDGED)
261+
262+
when:
263+
op.execute(getBinding())
264+
265+
then:
266+
thrown(IllegalArgumentException)
267+
268+
where:
269+
ordered << [true, false]
270+
}
271+
272+
def 'when updating with an invalid document, update should throw IllegalArgumentException'() {
273+
given:
274+
def id = new ObjectId()
275+
def op = new MixedBulkWriteOperation(getNamespace(),
276+
[new UpdateRequest(new BsonDocument('_id', new BsonObjectId(id)), new BsonDocument('a', new BsonInt32(1)), UPDATE)],
277+
true, ACKNOWLEDGED)
278+
279+
when:
280+
op.execute(getBinding())
281+
282+
then:
283+
thrown(IllegalArgumentException)
284+
285+
where:
286+
ordered << [true, false]
287+
}
288+
255289
def 'when a document contains a key with an illegal character, replacing a document with it should throw IllegalArgumentException'() {
256290
given:
257291
def id = new ObjectId()

driver-core/src/test/functional/com/mongodb/operation/UpdateOperationSpecification.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,19 @@ class UpdateOperationSpecification extends OperationFunctionalSpecification {
223223
ordered << [true, false]
224224
}
225225

226+
227+
def 'when an update document is empty, update should throw IllegalArgumentException'() {
228+
when:
229+
new UpdateOperation(getNamespace(), ordered, ACKNOWLEDGED,
230+
[new UpdateRequest(new BsonDocument(),
231+
new BsonDocument(), WriteRequest.Type.UPDATE)])
232+
.execute(getBinding())
233+
234+
then:
235+
thrown(IllegalArgumentException)
236+
237+
where:
238+
ordered << [true, false]
239+
}
240+
226241
}

0 commit comments

Comments
 (0)