diff options
-rw-r--r-- | jstests/views/views_creation.js | 20 | ||||
-rw-r--r-- | src/mongo/db/views/view_catalog.cpp | 5 |
2 files changed, 25 insertions, 0 deletions
diff --git a/jstests/views/views_creation.js b/jstests/views/views_creation.js index 4abee26ae4a..2f8bcf52142 100644 --- a/jstests/views/views_creation.js +++ b/jstests/views/views_creation.js @@ -12,6 +12,26 @@ let collNames = viewsDB.getCollectionNames(); assert.eq(0, collNames.length, tojson(collNames)); + // You cannot create a view that starts with 'system.'. + assert.commandFailedWithCode(viewsDB.runCommand({create: "system.views", viewOn: "collection"}), + ErrorCodes.InvalidNamespace, + "Created an illegal view named 'system.views'"); + + // We don't run this check on MMAPv1 as it automatically creates a system.indexes collection + // when creating a database, which causes this command to fail with NamespaceAlreadyExists. + if (jsTest.options().storageEngine !== "mmapv1") { + assert.commandFailedWithCode( + viewsDB.runCommand({create: "system.indexes", viewOn: "collection"}), + ErrorCodes.InvalidNamespace, + "Created an illegal view named 'system.indexes'"); + } + + // Collections that start with 'system.' that are not special to MongoDB fail with a different + // error code. + assert.commandFailedWithCode(viewsDB.runCommand({create: "system.foo", viewOn: "collection"}), + ErrorCodes.BadValue, + "Created an illegal view named 'system.foo'"); + // Create a collection for test purposes. assert.commandWorked(viewsDB.runCommand({create: "collection"})); diff --git a/src/mongo/db/views/view_catalog.cpp b/src/mongo/db/views/view_catalog.cpp index 68b142658fa..237414b4882 100644 --- a/src/mongo/db/views/view_catalog.cpp +++ b/src/mongo/db/views/view_catalog.cpp @@ -252,6 +252,11 @@ Status ViewCatalog::createView(OperationContext* txn, return Status(ErrorCodes::InvalidNamespace, str::stream() << "invalid name for 'viewOn': " << viewOn.coll()); + if (viewName.isSystem()) + return Status( + ErrorCodes::InvalidNamespace, + "View name cannot start with 'system.', which is reserved for system namespaces"); + auto collator = parseCollator(txn, collation); if (!collator.isOK()) return collator.getStatus(); |