From 64a9f9706f2428c49e0cfb8e223065acc645f7bc Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 13 Jan 2025 17:10:25 -0500 Subject: [PATCH 1/2] fix: disallow nested $where in populate match --- .../populate/getModelsMapForPopulate.js | 41 +++++++++++-------- test/model.populate.test.js | 21 ++++++++-- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/lib/helpers/populate/getModelsMapForPopulate.js b/lib/helpers/populate/getModelsMapForPopulate.js index 83aaf981d5..d95b4b4aa7 100644 --- a/lib/helpers/populate/getModelsMapForPopulate.js +++ b/lib/helpers/populate/getModelsMapForPopulate.js @@ -182,15 +182,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) { if (hasMatchFunction) { match = match.call(doc, doc); } - if (Array.isArray(match)) { - for (const item of match) { - if (item != null && item.$where) { - throw new MongooseError('Cannot use $where filter with populate() match'); - } - } - } else if (match != null && match.$where != null) { - throw new MongooseError('Cannot use $where filter with populate() match'); - } + throwOn$where(match); data.match = match; data.hasMatchFunction = hasMatchFunction; data.isRefPath = isRefPath; @@ -469,15 +461,7 @@ function _virtualPopulate(model, docs, options, _virtualRes) { data.match = match; data.hasMatchFunction = hasMatchFunction; - if (Array.isArray(match)) { - for (const item of match) { - if (item != null && item.$where) { - throw new MongooseError('Cannot use $where filter with populate() match'); - } - } - } else if (match != null && match.$where != null) { - throw new MongooseError('Cannot use $where filter with populate() match'); - } + throwOn$where(match); // Get local fields const ret = _getLocalFieldValues(doc, localField, model, options, virtual); @@ -749,3 +733,24 @@ function _findRefPathForDiscriminators(doc, modelSchema, data, options, normaliz return modelNames; } + +/** + * Throw an error if there are any $where keys + */ + +function throwOn$where(match) { + if (match == null) { + return; + } + if (typeof match !== 'object') { + return; + } + for (const key of Object.keys(match)) { + if (key === '$where') { + throw new MongooseError('Cannot use $where filter with populate() match'); + } + if (match[key] != null && typeof match[key] === 'object') { + throwOn$where(match[key]); + } + } +} diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 42be868b64..ce2f34c4f8 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -4195,21 +4195,34 @@ describe('model: populate:', function() { const parent = await Parent.create({ name: 'Anakin', child: child._id }); await assert.rejects( - () => Parent.findOne().populate({ path: 'child', match: { $where: 'console.log("oops!");' } }), + () => Parent.findOne().populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }), /Cannot use \$where filter with populate\(\) match/ ); await assert.rejects( - () => Parent.find().populate({ path: 'child', match: { $where: 'console.log("oops!");' } }), + () => Parent.find().populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }), /Cannot use \$where filter with populate\(\) match/ ); await assert.rejects( - () => parent.populate({ path: 'child', match: { $where: 'console.log("oops!");' } }), + () => parent.populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }), /Cannot use \$where filter with populate\(\) match/ ); await assert.rejects( - () => Child.find().populate({ path: 'parent', match: { $where: 'console.log("oops!");' } }), + () => Child.find().populate({ path: 'parent', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }), /Cannot use \$where filter with populate\(\) match/ ); + await assert.rejects( + () => Child.find().populate({ path: 'parent', match: () => ({ $or: [{ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }] }) }), + /Cannot use \$where filter with populate\(\) match/ + ); + await assert.rejects( + () => Child.find().populate({ path: 'parent', match: () => ({ $and: [{ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }] }) }), + /Cannot use \$where filter with populate\(\) match/ + ); + + class MyClass {} + MyClass.prototype.$where = 'typeof console !== "undefined" ? doesNotExist("foo") : true;'; + // OK because sift only looks through own properties + await Child.find().populate({ path: 'parent', match: () => new MyClass() }); }); it('multiple source docs', function(done) { From e59e342e5d01bfeee31c5d5c796745315ecf0fa9 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 13 Jan 2025 17:11:13 -0500 Subject: [PATCH 2/2] chore: release 6.13.6 --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fcd4a39b5..003a0a0f0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +6.13.6 / 2025-01-13 +=================== + * fix: disallow nested $where in populate match + 6.13.5 / 2024-11-26 =================== * fix: disallow using $where in match diff --git a/package.json b/package.json index 2c6b27f090..c871d8c367 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mongoose", "description": "Mongoose MongoDB ODM", - "version": "6.13.5", + "version": "6.13.6", "author": "Guillermo Rauch ", "keywords": [ "mongodb",