diff options
author | Kyle Suarez <kyle.suarez@mongodb.com> | 2022-09-19 14:53:31 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-09-19 16:01:28 +0000 |
commit | c9fe899fff347770c0e30fa0272f6157be6676a8 (patch) | |
tree | 91e11f7735fde79839e51ccb5b93c6b72a7fe753 /src/mongo/db | |
parent | 293bcb83c26a18ddaa74db375d3b1e3dac9a1bfe (diff) | |
download | mongo-c9fe899fff347770c0e30fa0272f6157be6676a8.tar.gz |
SERVER-67715 escape $changeStream regex
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/pipeline/change_stream_rewrite_helpers.cpp | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/mongo/db/pipeline/change_stream_rewrite_helpers.cpp b/src/mongo/db/pipeline/change_stream_rewrite_helpers.cpp index bee55d5c709..cb385656a63 100644 --- a/src/mongo/db/pipeline/change_stream_rewrite_helpers.cpp +++ b/src/mongo/db/pipeline/change_stream_rewrite_helpers.cpp @@ -29,6 +29,8 @@ #include "mongo/db/pipeline/change_stream_rewrite_helpers.h" +#include <boost/algorithm/string/replace.hpp> + #include "mongo/db/matcher/expression_always_boolean.h" #include "mongo/db/matcher/expression_expr.h" #include "mongo/db/pipeline/document_source_change_stream.h" @@ -888,9 +890,12 @@ std::unique_ptr<MatchExpression> matchRewriteGenericNamespace( }(); // Convert the MatchExpression $regex into a $regexMatch on the corresponding field. + // Backslashes must be escaped to ensure they retain their special behavior. + const auto regex = + boost::replace_all_copy(std::string(nsElem.regex()), R"(\)", R"(\\)"); const std::string exprRegexMatch = str::stream() - << "{$regexMatch: {input: " << exprDbOrCollName << ", regex: '" - << nsElem.regex() << "', options: '" << nsElem.regexFlags() << "'}}"; + << "{$regexMatch: {input: " << exprDbOrCollName << ", regex: '" << regex + << "', options: '" << nsElem.regexFlags() << "'}}"; // Finally, wrap the regex in a $let which defines the '$$oplogField' variable. const std::string exprRewrittenPredicate = str::stream() |