Skip to content

Commit ecb738a

Browse files
author
Divjot Arora
committed
Disallow empty update documents.
GODRIVER-743 Change-Id: I57ef34e7ce9368128c560fef056eff1ccede9e3b
1 parent 81f961d commit ecb738a

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

mongo/collection.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,11 @@ func (coll *Collection) FindOneAndUpdate(ctx context.Context, filter interface{}
11891189
return &SingleResult{err: err}
11901190
}
11911191

1192-
if len(u) > 0 && !strings.HasPrefix(u[0].Key, "$") {
1193-
return &SingleResult{err: errors.New("update document must contain key beginning with '$")}
1192+
err = ensureDollarKey(u)
1193+
if err != nil {
1194+
return &SingleResult{
1195+
err: err,
1196+
}
11941197
}
11951198

11961199
sess := sessionFromContext(ctx)

mongo/collection_internal_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,12 @@ func TestCollection_DeleteOne_WriteConcernError(t *testing.T) {
817817
}
818818
}
819819

820+
func TestCollection_UpdateOne_EmptyUpdate(t *testing.T) {
821+
coll := createTestCollection(t, nil, nil)
822+
_, err := coll.UpdateOne(ctx, bsonx.Doc{}, bsonx.Doc{})
823+
require.NotNil(t, err)
824+
}
825+
820826
func TestCollection_UpdateOne_found(t *testing.T) {
821827
if testing.Short() {
822828
t.Skip("skipping integration test in short mode")
@@ -933,6 +939,12 @@ func TestCollection_UpdateOne_WriteConcernError(t *testing.T) {
933939
}
934940
}
935941

942+
func TestCollection_UpdateMany_EmptyUpdate(t *testing.T) {
943+
coll := createTestCollection(t, nil, nil)
944+
_, err := coll.UpdateMany(ctx, bsonx.Doc{}, bsonx.Doc{})
945+
require.NotNil(t, err)
946+
}
947+
936948
func TestCollection_UpdateMany_found(t *testing.T) {
937949
if testing.Short() {
938950
t.Skip("skipping integration test in short mode")
@@ -1790,6 +1802,12 @@ func TestCollection_FindOneAndUpdate_found(t *testing.T) {
17901802
require.Equal(t, int(elem.Int32()), 3)
17911803
}
17921804

1805+
func TestCollection_FindOneAndUpdate_EmptyUpdate(t *testing.T) {
1806+
coll := createTestCollection(t, nil, nil)
1807+
res := coll.FindOneAndUpdate(context.Background(), bsonx.Doc{}, bsonx.Doc{})
1808+
require.NotNil(t, res.Err())
1809+
}
1810+
17931811
func TestCollection_FindOneAndUpdate_found_ignoreResult(t *testing.T) {
17941812
if testing.Short() {
17951813
t.Skip("skipping integration test in short mode")

mongo/mongo.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ func ensureID(d bsonx.Doc) (bsonx.Doc, interface{}) {
170170
}
171171

172172
func ensureDollarKey(doc bsonx.Doc) error {
173-
if len(doc) > 0 && !strings.HasPrefix(doc[0].Key, "$") {
173+
if len(doc) == 0 {
174+
return errors.New("update document must have at least one element")
175+
}
176+
if !strings.HasPrefix(doc[0].Key, "$") {
174177
return errors.New("update document must contain key beginning with '$'")
175178
}
176179
return nil

0 commit comments

Comments
 (0)