summaryrefslogtreecommitdiff
path: root/src/mongo/bson
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2021-12-13 12:15:08 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-12-13 17:46:29 +0000
commit886e09ce457d5cdbef16710678c386ec6765db3c (patch)
treecabe4bb339af9e1b8f8b552cec0eb80f4b7cd253 /src/mongo/bson
parentc8de2a7f419f2486780fed5ae06ade4e78aa2a5d (diff)
downloadmongo-886e09ce457d5cdbef16710678c386ec6765db3c.tar.gz
SERVER-61846 redact document using null instead of default mask to stay under BSON limit
Diffstat (limited to 'src/mongo/bson')
-rw-r--r--src/mongo/bson/bsonobj.cpp23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/mongo/bson/bsonobj.cpp b/src/mongo/bson/bsonobj.cpp
index c12cf7f77c1..a2f6e120ea4 100644
--- a/src/mongo/bson/bsonobj.cpp
+++ b/src/mongo/bson/bsonobj.cpp
@@ -142,25 +142,38 @@ BSONObj BSONObj::redact() const {
// Helper to get an "internal function" to be able to do recursion
struct redactor {
- void operator()(BSONObjBuilder& builder, const BSONObj& obj) {
+ void operator()(BSONObjBuilder& builder, const BSONObj& obj, bool appendMask) {
for (BSONElement e : obj) {
if (e.type() == Object) {
BSONObjBuilder subBuilder = builder.subobjStart(e.fieldNameStringData());
- operator()(subBuilder, e.Obj());
+ operator()(subBuilder, e.Obj(), appendMask);
subBuilder.done();
} else if (e.type() == Array) {
BSONObjBuilder subBuilder = builder.subarrayStart(e.fieldNameStringData());
- operator()(subBuilder, e.Obj());
+ operator()(subBuilder, e.Obj(), appendMask);
subBuilder.done();
- } else {
+ } else if (appendMask) {
builder.append(e.fieldNameStringData(), "###"_sd);
+ } else {
+ builder.appendNull(e.fieldNameStringData());
}
}
}
};
+ try {
+ BSONObjBuilder builder;
+ redactor()(builder, *this, /*appendMask=*/true);
+ return builder.obj();
+ } catch (const ExceptionFor<ErrorCodes::BSONObjectTooLarge>&) {
+ }
+
+ // For some BSONObj with lots of small fields, replacing each element's value with the default
+ // redaction mask "###" may cause us to exceed the maximum allowed BSON size. In this case,
+ // we use BSONType::jstNull, which ensures the redacted object will not be larger than the
+ // original.
BSONObjBuilder builder;
- redactor()(builder, *this);
+ redactor()(builder, *this, /*appendMask=*/false);
return builder.obj();
}