summaryrefslogtreecommitdiff
path: root/src/mongo/bson
diff options
context:
space:
mode:
authorRichard Hausman <richard.hausman@mongodb.com>2022-08-02 13:45:56 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-08-02 14:41:11 +0000
commitfc5d35bb08d81f0cd165296760c4ab7506fcff1c (patch)
tree4b2cdaea5e6ad42abe4b1df8f24288e484472218 /src/mongo/bson
parentb7cebdc9e7ea7b1c9be1bbfa8a9b52689c0bb450 (diff)
downloadmongo-fc5d35bb08d81f0cd165296760c4ab7506fcff1c.tar.gz
SERVER-67877 : Check for UUID validity in BSON documents.
Diffstat (limited to 'src/mongo/bson')
-rw-r--r--src/mongo/bson/bson_validate.cpp11
-rw-r--r--src/mongo/bson/bson_validate_test.cpp23
2 files changed, 34 insertions, 0 deletions
diff --git a/src/mongo/bson/bson_validate.cpp b/src/mongo/bson/bson_validate.cpp
index b4e438a3ad3..c53607a4bb1 100644
--- a/src/mongo/bson/bson_validate.cpp
+++ b/src/mongo/bson/bson_validate.cpp
@@ -113,6 +113,17 @@ public:
NonConformantBSON,
fmt::format("Use of deprecated BSON binary data subtype {}", subtype));
break;
+ case BinDataType::newUUID: {
+ constexpr uint32_t UUIDLength = 16;
+ uint32_t l = ConstDataView(ptr).read<LittleEndian<uint32_t>>();
+ uassert(
+ ErrorCodes::NonConformantBSON,
+ fmt::format("BSON UUID length should be {} bytes. Found {} instead.",
+ UUIDLength,
+ l),
+ l == UUIDLength);
+ break;
+ }
case BinDataType::MD5Type:
constexpr uint32_t md5Length = 16;
auto md5Size = ConstDataView(ptr).read<LittleEndian<uint32_t>>();
diff --git a/src/mongo/bson/bson_validate_test.cpp b/src/mongo/bson/bson_validate_test.cpp
index 538f0fc5c28..455c9382927 100644
--- a/src/mongo/bson/bson_validate_test.cpp
+++ b/src/mongo/bson/bson_validate_test.cpp
@@ -451,6 +451,29 @@ TEST(BSONValidateFast, MaxNestingDepth) {
ASSERT_EQ(status.code(), ErrorCodes::Overflow);
}
+TEST(BSONValidateExtended, UUIDLength) {
+ // Checks that an invalid UUID length (!= 16 bytes) throws a warning.
+ std::pair<Status, Status> stats{Status::OK(), Status::OK()};
+ auto fullyValidate = [&](BSONObj obj) {
+ return std::pair{validateBSON(obj.objdata(), obj.objsize(), BSONValidateMode::kExtended),
+ validateBSON(obj.objdata(), obj.objsize(), BSONValidateMode::kFull)};
+ };
+ BSONObj x = BSON("u" << BSONBinData("de", 2, BinDataType::newUUID));
+ stats = fullyValidate(x);
+ ASSERT_EQ(stats.first.code(), ErrorCodes::NonConformantBSON);
+ ASSERT_EQ(stats.second.code(), ErrorCodes::NonConformantBSON);
+ x = BSON("u" << BSONBinData("aaaaaaaaaaaaaaaaaaaaaa", 22, BinDataType::newUUID));
+ stats = fullyValidate(x);
+ ASSERT_EQ(stats.first.code(), ErrorCodes::NonConformantBSON);
+ ASSERT_EQ(stats.second.code(), ErrorCodes::NonConformantBSON);
+
+ // Checks that a valid UUID does not throw any warnings.
+ x = BSON("u" << BSONBinData("abcdabcdabcdabcd", 16, BinDataType::newUUID));
+ stats = fullyValidate(x);
+ ASSERT_OK(stats.first);
+ ASSERT_OK(stats.second);
+}
+
TEST(BSONValidateExtended, DeprecatedTypes) {
BSONObj obj = BSON("a" << BSONUndefined);
Status status = validateBSON(obj.objdata(), obj.objsize(), BSONValidateMode::kExtended);