diff options
author | Drew Paroski <drew.paroski@mongodb.com> | 2020-07-15 00:32:39 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-08-03 22:49:10 +0000 |
commit | 8c59fe07686d3d3f2fdeb2a7b5ab61eaac7e6231 (patch) | |
tree | 16b497c0fa32cca5e55861fd9118cecc2cc5ae6b | |
parent | 96006491b33820538a5dd850fd0df9fdb6aaa1c9 (diff) | |
download | mongo-8c59fe07686d3d3f2fdeb2a7b5ab61eaac7e6231.tar.gz |
SERVER-49404 Enforce additional checks in $arrayToObject
(cherry picked from commit 1772b9a0393b55e6a280a35e8f0a1f75c014f301)
-rw-r--r-- | jstests/aggregation/expressions/arrayToObject.js | 14 | ||||
-rw-r--r-- | src/mongo/db/pipeline/expression.cpp | 16 |
2 files changed, 28 insertions, 2 deletions
diff --git a/jstests/aggregation/expressions/arrayToObject.js b/jstests/aggregation/expressions/arrayToObject.js index df78b9f1aaf..4a2eea9332f 100644 --- a/jstests/aggregation/expressions/arrayToObject.js +++ b/jstests/aggregation/expressions/arrayToObject.js @@ -72,4 +72,18 @@ assertPipelineErrors([{k: null, v: "nullKey"}], 40394); assertPipelineErrors([{k: undefined, v: "undefinedKey"}], 40394); assertPipelineErrors([{y: "ignored", k: "item", v: "pear"}], 40392); assertPipelineErrors(NaN, 40386); + +// Check that $arrayToObject produces an error when the key contains a null byte. +assertErrorCode( + coll, [{$replaceWith: {$arrayToObject: {$literal: [["a\0b", "abra cadabra"]]}}}], 4940400); +assertErrorCode( + coll, [{$replaceWith: {$arrayToObject: {$literal: [{k: "a\0b", v: "blah"}]}}}], 4940401); +assertErrorCode( + coll, + [{$replaceWith: {$arrayToObject: {$literal: [["a\0b", "abra cadabra"]]}}}, {$out: "output"}], + 4940400); +assertErrorCode( + coll, + [{$replaceWith: {$arrayToObject: {$literal: [{k: "a\0b", v: "blah"}]}}}, {$out: "output"}], + 4940401); }()); diff --git a/src/mongo/db/pipeline/expression.cpp b/src/mongo/db/pipeline/expression.cpp index 387ff0ab1cb..78932b2c044 100644 --- a/src/mongo/db/pipeline/expression.cpp +++ b/src/mongo/db/pipeline/expression.cpp @@ -633,7 +633,13 @@ Value ExpressionArrayToObject::evaluate(const Document& root, Variables* variabl << typeName(valArray[0].getType()), (valArray[0].getType() == BSONType::String)); - output[valArray[0].getString()] = valArray[1]; + auto keyName = valArray[0].getStringData(); + + uassert(4940400, + "Key field cannot contain an embedded null byte", + keyName.find('\0') == std::string::npos); + + output[keyName] = valArray[1]; } else { uassert( @@ -665,7 +671,13 @@ Value ExpressionArrayToObject::evaluate(const Document& root, Variables* variabl << typeName(key.getType()), (key.getType() == BSONType::String)); - output[key.getString()] = value; + auto keyName = key.getStringData(); + + uassert(4940401, + "Key field cannot contain an embedded null byte", + keyName.find('\0') == std::string::npos); + + output[keyName] = value; } } |