summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Stearn <mathias@10gen.com>2017-06-16 13:42:56 -0400
committerMathias Stearn <mathias@10gen.com>2017-06-19 19:02:31 -0400
commit9938d658c44140555245943a292e9e33dc75d9c5 (patch)
treecca7580911ff39e87aa0522d69fd3894b7a6b643
parentd8ef9342757fb729169c5cd44baa9052f5809b3f (diff)
downloadmongo-9938d658c44140555245943a292e9e33dc75d9c5.tar.gz
SERVER-29319 Check $db in explain
-rw-r--r--jstests/core/explain_db_mismatch.js7
-rw-r--r--src/mongo/db/commands/explain_cmd.cpp8
-rw-r--r--src/mongo/s/commands/cluster_explain_cmd.cpp8
3 files changed, 23 insertions, 0 deletions
diff --git a/jstests/core/explain_db_mismatch.js b/jstests/core/explain_db_mismatch.js
new file mode 100644
index 00000000000..13d54cae77f
--- /dev/null
+++ b/jstests/core/explain_db_mismatch.js
@@ -0,0 +1,7 @@
+// Ensure that explain command errors if the inner command has a $db field that doesn't match the
+// outer command.
+(function() {
+ assert.commandFailedWithCode(
+ db.runCommand({explain: {find: 'some_collection', $db: 'not_my_db'}}),
+ ErrorCodes.InvalidNamespace);
+}());
diff --git a/src/mongo/db/commands/explain_cmd.cpp b/src/mongo/db/commands/explain_cmd.cpp
index e55da5b2974..92f56e864f5 100644
--- a/src/mongo/db/commands/explain_cmd.cpp
+++ b/src/mongo/db/commands/explain_cmd.cpp
@@ -133,6 +133,14 @@ public:
// This is the nested command which we are explaining.
BSONObj explainObj = cmdObj.firstElement().Obj();
+ if (auto innerDb = explainObj["$db"]) {
+ uassert(ErrorCodes::InvalidNamespace,
+ str::stream() << "Mismatched $db in explain command. Expected " << dbname
+ << " but got "
+ << innerDb.checkAndGetStringData(),
+ innerDb.checkAndGetStringData() == dbname);
+ }
+
Command* commToExplain = Command::findCommand(explainObj.firstElementFieldName());
if (NULL == commToExplain) {
mongoutils::str::stream ss;
diff --git a/src/mongo/s/commands/cluster_explain_cmd.cpp b/src/mongo/s/commands/cluster_explain_cmd.cpp
index 2fa4184a5c0..c21a9db40b0 100644
--- a/src/mongo/s/commands/cluster_explain_cmd.cpp
+++ b/src/mongo/s/commands/cluster_explain_cmd.cpp
@@ -120,6 +120,14 @@ public:
// Command::explain() method.
const BSONObj explainObj = ([&] {
const auto innerObj = cmdObj.firstElement().Obj();
+ if (auto innerDb = innerObj["$db"]) {
+ uassert(ErrorCodes::InvalidNamespace,
+ str::stream() << "Mismatched $db in explain command. Expected " << dbName
+ << " but got "
+ << innerDb.checkAndGetStringData(),
+ innerDb.checkAndGetStringData() == dbName);
+ }
+
BSONObjBuilder bob;
bob.appendElements(innerObj);
for (auto outerElem : cmdObj) {