summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/query_request_helper.cpp
diff options
context:
space:
mode:
authorDavis Haupt <davis.haupt@mongodb.com>2023-04-04 19:22:32 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-04-04 21:29:09 +0000
commit6989337f513dcbe867638dc1a32e71f71a31c758 (patch)
treee481213d680cef919ff110b035224bf4204f2f6e /src/mongo/db/query/query_request_helper.cpp
parent46fb4a53d088832a761b8732ab69708465918788 (diff)
downloadmongo-6989337f513dcbe867638dc1a32e71f71a31c758.tar.gz
SERVER-68922 reject invalid $natural inputs
Diffstat (limited to 'src/mongo/db/query/query_request_helper.cpp')
-rw-r--r--src/mongo/db/query/query_request_helper.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/mongo/db/query/query_request_helper.cpp b/src/mongo/db/query/query_request_helper.cpp
index 55e806c7495..16d95be8c87 100644
--- a/src/mongo/db/query/query_request_helper.cpp
+++ b/src/mongo/db/query/query_request_helper.cpp
@@ -93,6 +93,15 @@ Status validateFindCommandRequest(const FindCommandRequest& findCommand) {
}
}
+ if (hasInvalidNaturalParam(findCommand.getSort())) {
+ return Status(ErrorCodes::BadValue,
+ "$natural sort cannot be set to a value other than -1 or 1.");
+ }
+ if (hasInvalidNaturalParam(findCommand.getHint())) {
+ return Status(ErrorCodes::BadValue,
+ "$natural hint cannot be set to a value other than -1 or 1.");
+ }
+
if (query_request_helper::getTailableMode(findCommand) != TailableModeEnum::kNormal) {
// Tailable cursors cannot have any sort other than {$natural: 1}.
const BSONObj expectedSort = BSON(query_request_helper::kNaturalSortField << 1);
@@ -379,5 +388,21 @@ StatusWith<BSONObj> asAggregationCommand(const FindCommandRequest& findCommand)
return StatusWith<BSONObj>(aggregationBuilder.obj());
}
+bool hasInvalidNaturalParam(const BSONObj& obj) {
+ if (!obj.hasElement(query_request_helper::kNaturalSortField)) {
+ return false;
+ }
+ auto naturalElem = obj[query_request_helper::kNaturalSortField];
+ if (!naturalElem.isNumber()) {
+ return true;
+ }
+ if (obj.woCompare(BSON(query_request_helper::kNaturalSortField << 1)) == 0 ||
+ obj.woCompare(BSON(query_request_helper::kNaturalSortField << -1)) == 0) {
+ return false;
+ }
+
+ return true;
+}
+
} // namespace query_request_helper
} // namespace mongo