summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jstests/noPassthrough/max_bson_depth_parameter.js33
-rw-r--r--jstests/noPassthrough/use_disk.js2
-rw-r--r--src/mongo/bson/bson_depth.h13
3 files changed, 25 insertions, 23 deletions
diff --git a/jstests/noPassthrough/max_bson_depth_parameter.js b/jstests/noPassthrough/max_bson_depth_parameter.js
index 8914dd2bd1b..8e74598c903 100644
--- a/jstests/noPassthrough/max_bson_depth_parameter.js
+++ b/jstests/noPassthrough/max_bson_depth_parameter.js
@@ -5,17 +5,21 @@
(function() {
"use strict";
-const kTestName = "max_bson_depth_parameter";
+const maxBSONDepth = 21;
// Start mongod with a valid BSON depth, then test that it accepts and rejects command
// appropriately based on the depth.
-let conn = MongoRunner.runMongod({setParameter: "maxBSONDepth=5"});
-assert.neq(null, conn, "Failed to start mongod");
-let testDB = conn.getDB("test");
+const conn = MongoRunner.runMongod({setParameter: {maxBSONDepth: maxBSONDepth}});
+const testDB = conn.getDB("test");
assert.commandWorked(testDB.runCommand({ping: 1}), "Failed to run a command on the server");
assert.commandFailedWithCode(
- testDB.runCommand({find: "coll", filter: {x: {x: {x: {x: {x: {x: 1}}}}}}}),
+ testDB.runCommand({
+ find: "coll",
+ filter: function nestedObj(depth) {
+ return {x: depth > 1 ? nestedObj(depth - 1) : 1};
+ }(maxBSONDepth + 1),
+ }),
ErrorCodes.Overflow,
"Expected server to reject command for exceeding the nesting depth limit");
@@ -29,19 +33,16 @@ assert.commandWorked(testDB.runCommand({
cursor: {}
}));
assert.commandFailedWithCode(
- testDB.runCommand({
+ testDB.runCommand(
+ {
aggregate: "coll1",
- pipeline: [{
- $lookup: {
- from: "coll2",
- as: "as",
- pipeline: [{$lookup: {from: "coll2", as: "as", pipeline: []}}]
- }
- }],
+ pipeline: function nestedPipeline(depth) {
+ return [{$lookup: {from: "coll2", as: "as", pipeline: depth > 3 ? nestedPipeline(depth - 3) : []}}];
+ }(maxBSONDepth),
cursor: {}
}),
- ErrorCodes.Overflow,
- "Expected server to reject command for exceeding the nesting depth limit");
+ ErrorCodes.Overflow,
+ "Expected server to reject command for exceeding the nesting depth limit");
// Restart mongod with a negative maximum BSON depth and test that it fails to start.
MongoRunner.stopMongod(conn);
@@ -50,7 +51,7 @@ assert.throws(() => MongoRunner.runMongod({setParameter: "maxBSONDepth=-4"}),
[],
"Expected mongod to fail at startup because depth was negative");
-assert.throws(() => MongoRunner.runMongod({setParameter: "maxBSONDepth=1"}),
+assert.throws(() => MongoRunner.runMongod({setParameter: "maxBSONDepth=20"}),
[],
"Expected mongod to fail at startup because depth was too low");
}());
diff --git a/jstests/noPassthrough/use_disk.js b/jstests/noPassthrough/use_disk.js
index 088501e8264..7f720c86c21 100644
--- a/jstests/noPassthrough/use_disk.js
+++ b/jstests/noPassthrough/use_disk.js
@@ -11,7 +11,7 @@
// For getLatestProfilerEntry and getProfilerProtocolStringForCommand
load("jstests/libs/profiler.js");
-const conn = MongoRunner.runMongod({setParameter: "maxBSONDepth=8"});
+const conn = MongoRunner.runMongod();
const testDB = conn.getDB("profile_agg");
const coll = testDB.getCollection("test");
diff --git a/src/mongo/bson/bson_depth.h b/src/mongo/bson/bson_depth.h
index e7e6836a8bf..ee0c7a2f235 100644
--- a/src/mongo/bson/bson_depth.h
+++ b/src/mongo/bson/bson_depth.h
@@ -39,16 +39,17 @@ struct BSONDepth {
// The default BSON depth nesting limit.
static constexpr std::int32_t kDefaultMaxAllowableDepth = 200;
- // The minimum allowable value for the BSON depth parameter.
- static constexpr std::int32_t kBSONDepthParameterFloor = 5;
-
- // The maximum allowable value for the BSON depth parameter.
- static constexpr std::int32_t kBSONDepthParameterCeiling = 250;
-
// The number of extra levels of nesting above the storage depth limit that the server will
// tolerate.
static constexpr std::uint32_t kExtraSystemDepthLevels = 20;
+ // The minimum allowable value for the BSON depth parameter. Choose a value such that the max
+ // depth for user storage will be at least 1.
+ static constexpr std::int32_t kBSONDepthParameterFloor = kExtraSystemDepthLevels + 1;
+
+ // The maximum allowable value for the BSON depth parameter.
+ static constexpr std::int32_t kBSONDepthParameterCeiling = 250;
+
// The depth of BSON accepted by the server. Configurable via the 'maxBSONDepth' server
// parameter.
static std::int32_t maxAllowableDepth;