diff options
author | James Wahlin <james.wahlin@10gen.com> | 2016-09-23 11:28:21 -0400 |
---|---|---|
committer | James Wahlin <james.wahlin@10gen.com> | 2016-09-23 17:21:37 -0400 |
commit | 5563428f99af20c29cb334f97e84f0dcc1cb102a (patch) | |
tree | aad314cbc290ff98b2407c801c8a5a71234dda05 /src/mongo/db/catalog/index_key_validate.cpp | |
parent | 2009fc942491098a4bd17001a2278af471e3cb08 (diff) | |
download | mongo-5563428f99af20c29cb334f97e84f0dcc1cb102a.tar.gz |
SERVER-769 Validate createIndexes field names
Diffstat (limited to 'src/mongo/db/catalog/index_key_validate.cpp')
-rw-r--r-- | src/mongo/db/catalog/index_key_validate.cpp | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/mongo/db/catalog/index_key_validate.cpp b/src/mongo/db/catalog/index_key_validate.cpp index 986454911cc..d83f8fafd4c 100644 --- a/src/mongo/db/catalog/index_key_validate.cpp +++ b/src/mongo/db/catalog/index_key_validate.cpp @@ -33,6 +33,7 @@ #include <boost/optional.hpp> #include <cmath> #include <limits> +#include <set> #include "mongo/base/status.h" #include "mongo/base/status_with.h" @@ -147,6 +148,11 @@ StatusWith<BSONObj> validateIndexSpec( bool hasVersionField = false; bool hasCollationField = false; + auto fieldNamesValidStatus = validateIndexSpecFieldNames(indexSpec); + if (!fieldNamesValidStatus.isOK()) { + return fieldNamesValidStatus; + } + boost::optional<IndexVersion> resolvedIndexVersion; for (auto&& indexSpecElem : indexSpec) { @@ -235,7 +241,8 @@ StatusWith<BSONObj> validateIndexSpec( hasCollationField = true; } else { - // TODO SERVER-769: Validate index options specified in the "createIndexes" command. + // We can assume field name is valid at this point. Validation of fieldname is handled + // prior to this in validateIndexSpecFieldNames(). continue; } } @@ -283,4 +290,46 @@ StatusWith<BSONObj> validateIndexSpec( return indexSpec; } + +Status validateIndexSpecFieldNames(const BSONObj& indexSpec) { + const std::set<StringData> allowedFieldNames = { + IndexDescriptor::k2dIndexMaxFieldName, + IndexDescriptor::k2dIndexBitsFieldName, + IndexDescriptor::k2dIndexMaxFieldName, + IndexDescriptor::k2dIndexMinFieldName, + IndexDescriptor::k2dsphereCoarsestIndexedLevel, + IndexDescriptor::k2dsphereFinestIndexedLevel, + IndexDescriptor::k2dsphereVersionFieldName, + IndexDescriptor::kBackgroundFieldName, + IndexDescriptor::kCollationFieldName, + IndexDescriptor::kDefaultLanguageFieldName, + IndexDescriptor::kDropDuplicatesFieldName, + IndexDescriptor::kExpireAfterSecondsFieldName, + IndexDescriptor::kGeoHaystackBucketSize, + IndexDescriptor::kIndexNameFieldName, + IndexDescriptor::kIndexVersionFieldName, + IndexDescriptor::kKeyPatternFieldName, + IndexDescriptor::kLanguageOverrideFieldName, + IndexDescriptor::kNamespaceFieldName, + IndexDescriptor::kPartialFilterExprFieldName, + IndexDescriptor::kSparseFieldName, + IndexDescriptor::kStorageEngineFieldName, + IndexDescriptor::kTextVersionFieldName, + IndexDescriptor::kUniqueFieldName, + IndexDescriptor::kWeightsFieldName, + // Index creation under legacy writeMode can result in an index spec with an _id field. + "_id"}; + + for (auto&& indexSpecElem : indexSpec) { + auto indexSpecElemFieldName = indexSpecElem.fieldNameStringData(); + if (!allowedFieldNames.count(indexSpecElemFieldName)) { + return {ErrorCodes::BadValue, + str::stream() << "The field '" << indexSpecElemFieldName + << "' is not valid for an index specification"}; + } + } + + return Status::OK(); +} + } // namespace mongo |