diff options
Diffstat (limited to 'src/mongo/rpc')
-rw-r--r-- | src/mongo/rpc/metadata.cpp | 3 | ||||
-rw-r--r-- | src/mongo/rpc/metadata_test.cpp | 13 |
2 files changed, 16 insertions, 0 deletions
diff --git a/src/mongo/rpc/metadata.cpp b/src/mongo/rpc/metadata.cpp index ba002cdc748..666e0584583 100644 --- a/src/mongo/rpc/metadata.cpp +++ b/src/mongo/rpc/metadata.cpp @@ -158,6 +158,9 @@ OpMsgRequest upconvertRequest(StringData db, BSONObj cmdObj, int queryFlags) { } if (!readPrefContainer.isEmpty()) { + uassert(ErrorCodes::InvalidOptions, + "Duplicate readPreference found in command object.", + !cmdObj.hasField("$readPreference")); cmdObj = BSONObjBuilder(std::move(cmdObj)).appendElements(readPrefContainer).obj(); } else if (!cmdObj.hasField("$readPreference") && (queryFlags & QueryOption_SecondaryOk)) { BSONObjBuilder bodyBuilder(std::move(cmdObj)); diff --git a/src/mongo/rpc/metadata_test.cpp b/src/mongo/rpc/metadata_test.cpp index d35a37d1b8d..898b98c745a 100644 --- a/src/mongo/rpc/metadata_test.cpp +++ b/src/mongo/rpc/metadata_test.cpp @@ -149,6 +149,19 @@ TEST(Metadata, UpconvertInvalidMetadata) { AssertionException); } +TEST(Metadata, UpconvertDuplicateReadPreference) { + auto secondaryReadPref = BSON("mode" + << "secondary"); + auto nearestReadPref = BSON("mode" + << "nearest"); + + BSONObjBuilder bob; + bob.append("query", BSON("$readPreference" << secondaryReadPref)); + bob.append("$readPreference", nearestReadPref); + + ASSERT_THROWS_CODE( + rpc::upconvertRequest("db", bob.obj(), 0), AssertionException, ErrorCodes::InvalidOptions); +} TEST(Metadata, UpconvertUsesDocumentSequecesCorrectly) { // These are cases where it is valid to use document sequences. |