summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Wlodarek <gregory.wlodarek@mongodb.com>2020-06-05 13:17:02 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-06-08 21:17:50 +0000
commit9f2697e77352d9a9bda91d9ecc506ef7cf4eb362 (patch)
tree1ae8d93f524217abaf4fd78df5b9f41ffd41909a
parent14cb5634011e86800084cc822b2557992cc7222e (diff)
downloadmongo-9f2697e77352d9a9bda91d9ecc506ef7cf4eb362.tar.gz
SERVER-48621 userAllowedCreateNS() should check the collection name length in FCV4.4
-rw-r--r--jstests/multiVersion/long_collection_names.js3
-rw-r--r--jstests/replsets/rename_collection_long.js27
-rw-r--r--src/mongo/db/ops/insert.cpp9
3 files changed, 35 insertions, 4 deletions
diff --git a/jstests/multiVersion/long_collection_names.js b/jstests/multiVersion/long_collection_names.js
index 53a2d684d9b..d72b56b3dcf 100644
--- a/jstests/multiVersion/long_collection_names.js
+++ b/jstests/multiVersion/long_collection_names.js
@@ -104,8 +104,7 @@ assert.neq(null, conn, 'mongod was unable to start with version ' + tojson(resta
testDb = conn.getDB(dbName);
// Creating a long collection name on a 4.4 binary with FCV 4.2 should fail.
-assert.commandFailedWithCode(testDb.createCollection('c'.repeat(8192)),
- ErrorCodes.IncompatibleServerVersion);
+assert.commandFailedWithCode(testDb.createCollection('c'.repeat(8192)), 4862100);
// Running rename within the same database or across two databases should fail for long collection
// names.
diff --git a/jstests/replsets/rename_collection_long.js b/jstests/replsets/rename_collection_long.js
new file mode 100644
index 00000000000..8f1c0ec0b7d
--- /dev/null
+++ b/jstests/replsets/rename_collection_long.js
@@ -0,0 +1,27 @@
+/**
+ * Verifies that collections cannot be renamed to a new name over 255 characters.
+ *
+ * V4.2 binaries do not return an error code in the 'renameCollection' command, only {ok: 0}.
+ * @tags: [requires_fcv_44]
+ */
+(function() {
+var replSetName = "rename_collection_long";
+const replTest = new ReplSetTest({name: replSetName, nodes: 2});
+replTest.startSet();
+replTest.initiate();
+
+const primary = replTest.getPrimary();
+const db = primary.getDB("test");
+
+assert.commandWorked(db.createCollection("a"));
+
+const longCollName = "1".repeat(512);
+assert.commandFailedWithCode(
+ db.adminCommand({renameCollection: "test.a", to: "test." + longCollName}),
+ [4862100, ErrorCodes.InvalidNamespace]);
+assert.commandFailedWithCode(
+ db.adminCommand({renameCollection: "test.a", to: "test2." + longCollName}),
+ [4862100, ErrorCodes.InvalidNamespace]);
+
+replTest.stopSet();
+}());
diff --git a/src/mongo/db/ops/insert.cpp b/src/mongo/db/ops/insert.cpp
index dc479731533..0e5e908b7cb 100644
--- a/src/mongo/db/ops/insert.cpp
+++ b/src/mongo/db/ops/insert.cpp
@@ -203,15 +203,20 @@ Status userAllowedCreateNS(StringData db, StringData coll) {
if (!NamespaceString::validCollectionName(coll))
return Status(ErrorCodes::InvalidNamespace, "invalid collection name");
+ NamespaceString nss(db, coll);
+ uassert(4862100,
+ str::stream() << "Fully qualified namespace is too long. Namespace: " << nss.ns()
+ << " Max: " << NamespaceString::MaxNsCollectionLen,
+ !nss.isNormalCollection() || nss.size() <= NamespaceString::MaxNsCollectionLen);
const auto& fcv = serverGlobalParams.featureCompatibility;
if (!fcv.isVersionInitialized() ||
fcv.getVersion() < ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo44) {
uassert(ErrorCodes::IncompatibleServerVersion,
str::stream() << "Fully qualified namespace is too long for FCV 4.2. Upgrade to "
"FCV 4.4 to create this namespace. Namespace: "
- << db << "." << coll
+ << nss.ns()
<< " FCV 4.2 Limit: " << NamespaceString::MaxNSCollectionLenFCV42,
- db.size() + 1 + coll.size() <= NamespaceString::MaxNSCollectionLenFCV42);
+ nss.size() <= NamespaceString::MaxNSCollectionLenFCV42);
}
// check special areas