SERVER-120668 Disallow $trim with too large mask string (#51722)

GitOrigin-RevId: b63bdeaac30bf821c12f059235459bb809f126f5
This commit is contained in:
Rui Liu 2026-04-15 12:57:41 +02:00 committed by MongoDB Bot
parent e932e0ec0f
commit 4c077b5c19
4 changed files with 23 additions and 2 deletions

View File

@ -659,6 +659,8 @@ last-continuous:
ticket: SERVER-119413
- test_file: jstests/core/index/index_on_incorrect_collection.js
ticket: SERVER-116327
- test_file: jstests/aggregation/expressions/trim.js
ticket: SERVER-120668
suites: null
last-lts:
all:
@ -1370,4 +1372,6 @@ last-lts:
ticket: SERVER-119413
- test_file: jstests/core/index/index_on_incorrect_collection.js
ticket: SERVER-116327
- test_file: jstests/aggregation/expressions/trim.js
ticket: SERVER-120668
suites: null

View File

@ -88,3 +88,12 @@ assertErrorCode(coll, [{$project: {x: {$trim: {input: {$add: [4, 2]}}}}}], 50699
assertErrorCode(coll, [{$project: {x: {$trim: {input: "$_id"}}}}], 50699);
assertErrorCode(coll, [{$project: {x: {$trim: {input: " x ", chars: "$_id"}}}}], 50700);
}());
assert(coll.drop());
assert.commandWorked(coll.insert([{_id: 0, str: " x "}]));
// Test that characters limit is triggered correctly (for all of $trim, $ltrim, $rtrim).
for (const op of ["$trim", "$ltrim", "$rtrim"]) {
assertErrorCode(
coll, [{$project: {x: {[op]: {input: "$str", chars: " ".repeat(4097)}}}}], 12066800);
}

View File

@ -6169,8 +6169,14 @@ Value ExpressionTrim::evaluate(const Document& root, Variables* variables) const
<< typeName(unvalidatedUserChars.getType()) << ") instead.",
unvalidatedUserChars.getType() == BSONType::String);
return Value(
doTrim(input, extractCodePointsFromChars(unvalidatedUserChars.getStringData(), _name)));
auto unvalidatedUserCharsStringData = unvalidatedUserChars.getStringData();
uassert(12066800,
str::stream() << _name << " requires 'chars' to be not greater than "
<< _kMaximumAllowedTrimStringBytes << " bytes, got "
<< unvalidatedUserCharsStringData.size() << " bytes instead.",
unvalidatedUserCharsStringData.size() <= _kMaximumAllowedTrimStringBytes);
return Value(doTrim(input, extractCodePointsFromChars(unvalidatedUserCharsStringData, _name)));
}
bool ExpressionTrim::codePointMatchesAtIndex(const StringData& input,

View File

@ -3689,6 +3689,8 @@ private:
static constexpr size_t _kInput = 0;
static constexpr size_t _kCharacters = 1; // Optional, null if not specified.
static constexpr size_t _kMaximumAllowedTrimStringBytes = 4096;
TrimType _trimType;
std::string _name; // "$trim", "$ltrim", or "$rtrim".
};