diff options
author | Alya Berciu <alyacarina@gmail.com> | 2021-06-03 14:33:55 +0100 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-06-09 10:45:11 +0000 |
commit | a11aba2bf9f18a8e73a23099519ad45a2939d238 (patch) | |
tree | 64b6c9346414a781a85fe0f110e2773d26f26d40 /src/mongo/db/update | |
parent | cd5391608ae87d378431c42f3eee1a2fa51b5bc9 (diff) | |
download | mongo-a11aba2bf9f18a8e73a23099519ad45a2939d238.tar.gz |
SERVER-57382 Add _id $-prefix validation to storage validation
Diffstat (limited to 'src/mongo/db/update')
-rw-r--r-- | src/mongo/db/update/storage_validation.cpp | 35 | ||||
-rw-r--r-- | src/mongo/db/update/storage_validation.h | 6 |
2 files changed, 31 insertions, 10 deletions
diff --git a/src/mongo/db/update/storage_validation.cpp b/src/mongo/db/update/storage_validation.cpp index cac577f414c..a314e8c3065 100644 --- a/src/mongo/db/update/storage_validation.cpp +++ b/src/mongo/db/update/storage_validation.cpp @@ -125,6 +125,30 @@ void validateDollarPrefixElement(mutablebson::ConstElement elem) { } } // namespace +Status storageValidIdField(const mongo::BSONElement& element) { + switch (element.type()) { + case BSONType::RegEx: + case BSONType::Array: + case BSONType::Undefined: + return Status(ErrorCodes::InvalidIdField, + str::stream() + << "The '_id' value cannot be of type " << typeName(element.type())); + case BSONType::Object: { + auto status = element.Obj().storageValidEmbedded(); + if (!status.isOK() && status.code() == ErrorCodes::DollarPrefixedFieldName && + feature_flags::gFeatureFlagDotsAndDollars.isEnabledAndIgnoreFCV()) { + return Status(status.code(), + str::stream() << "_id fields may not contain '$'-prefixed fields: " + << status.reason()); + } + return status; + } + default: + break; + } + return Status::OK(); +} + void storageValid(const mutablebson::Document& doc, const bool allowTopLevelDollarPrefixes, const bool shouldValidate, @@ -132,16 +156,7 @@ void storageValid(const mutablebson::Document& doc, auto currElem = doc.root().leftChild(); while (currElem.ok()) { if (currElem.getFieldName() == idFieldName && shouldValidate) { - switch (currElem.getType()) { - case BSONType::RegEx: - case BSONType::Array: - case BSONType::Undefined: - uasserted(ErrorCodes::InvalidIdField, - str::stream() << "The '_id' value cannot be of type " - << typeName(currElem.getType())); - default: - break; - } + uassertStatusOK(storageValidIdField(currElem.getValue())); } // Validate this child element. diff --git a/src/mongo/db/update/storage_validation.h b/src/mongo/db/update/storage_validation.h index 9e3ae50ee84..951f5fe7e95 100644 --- a/src/mongo/db/update/storage_validation.h +++ b/src/mongo/db/update/storage_validation.h @@ -36,6 +36,12 @@ namespace mongo { namespace storage_validation { /** + * Returns a status to indicate whether or not 'element' is a valid _id field for storage in a + * collection. + */ +Status storageValidIdField(const mongo::BSONElement& element); + +/** * Validates that the MutableBSON document 'doc' is acceptable for storage in a collection. The * check is performed recursively on subdocuments. Uasserts if the validation fails or if the depth * exceeds the maximum allowable depth. |