Skip to content

Commit 04f4a77

Browse files
committed
Merge branch '6.x' into 7.x
2 parents bc2809d + 15bdccf commit 04f4a77

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
6.13.5 / 2024-11-26
2+
===================
3+
* fix: disallow using $where in match
4+
15
6.13.4 / 2024-11-15
26
===================
37
* fix: save execution stack in query as string #15043 #15039

lib/helpers/populate/assignVals.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function numDocs(v) {
250250

251251
function valueFilter(val, assignmentOpts, populateOptions, allIds) {
252252
const userSpecifiedTransform = typeof populateOptions.transform === 'function';
253-
const transform = userSpecifiedTransform ? populateOptions.transform : noop;
253+
const transform = userSpecifiedTransform ? populateOptions.transform : v => v;
254254
if (Array.isArray(val)) {
255255
// find logic
256256
const ret = [];
@@ -342,7 +342,3 @@ function isPopulatedObject(obj) {
342342
obj.$__ != null ||
343343
leanPopulateMap.has(obj);
344344
}
345-
346-
function noop(v) {
347-
return v;
348-
}

lib/helpers/populate/getModelsMapForPopulate.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
184184
if (hasMatchFunction) {
185185
match = match.call(doc, doc);
186186
}
187+
if (Array.isArray(match)) {
188+
for (const item of match) {
189+
if (item != null && item.$where) {
190+
throw new MongooseError('Cannot use $where filter with populate() match');
191+
}
192+
}
193+
} else if (match != null && match.$where != null) {
194+
throw new MongooseError('Cannot use $where filter with populate() match');
195+
}
187196
data.match = match;
188197
data.hasMatchFunction = hasMatchFunction;
189198
data.isRefPath = isRefPath;
@@ -461,6 +470,16 @@ function _virtualPopulate(model, docs, options, _virtualRes) {
461470
data.match = match;
462471
data.hasMatchFunction = hasMatchFunction;
463472

473+
if (Array.isArray(match)) {
474+
for (const item of match) {
475+
if (item != null && item.$where) {
476+
throw new MongooseError('Cannot use $where filter with populate() match');
477+
}
478+
}
479+
} else if (match != null && match.$where != null) {
480+
throw new MongooseError('Cannot use $where filter with populate() match');
481+
}
482+
464483
// Get local fields
465484
const ret = _getLocalFieldValues(doc, localField, model, options, virtual);
466485

test/model.populate.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,6 +3553,52 @@ describe('model: populate:', function() {
35533553
assert.deepEqual(band.members.map(b => b.name).sort(), ['AA', 'AB']);
35543554
});
35553555

3556+
it('match prevents using $where', async function() {
3557+
const ParentSchema = new Schema({
3558+
name: String,
3559+
child: {
3560+
type: mongoose.Schema.Types.ObjectId,
3561+
ref: 'Child'
3562+
},
3563+
children: [{
3564+
type: mongoose.Schema.Types.ObjectId,
3565+
ref: 'Child'
3566+
}]
3567+
});
3568+
3569+
const ChildSchema = new Schema({
3570+
name: String
3571+
});
3572+
ChildSchema.virtual('parent', {
3573+
ref: 'Parent',
3574+
localField: '_id',
3575+
foreignField: 'parent'
3576+
});
3577+
3578+
const Parent = db.model('Parent', ParentSchema);
3579+
const Child = db.model('Child', ChildSchema);
3580+
3581+
const child = await Child.create({ name: 'Luke' });
3582+
const parent = await Parent.create({ name: 'Anakin', child: child._id });
3583+
3584+
await assert.rejects(
3585+
() => Parent.findOne().populate({ path: 'child', match: { $where: 'console.log("oops!");' } }),
3586+
/Cannot use \$where filter with populate\(\) match/
3587+
);
3588+
await assert.rejects(
3589+
() => Parent.find().populate({ path: 'child', match: { $where: 'console.log("oops!");' } }),
3590+
/Cannot use \$where filter with populate\(\) match/
3591+
);
3592+
await assert.rejects(
3593+
() => parent.populate({ path: 'child', match: { $where: 'console.log("oops!");' } }),
3594+
/Cannot use \$where filter with populate\(\) match/
3595+
);
3596+
await assert.rejects(
3597+
() => Child.find().populate({ path: 'parent', match: { $where: 'console.log("oops!");' } }),
3598+
/Cannot use \$where filter with populate\(\) match/
3599+
);
3600+
});
3601+
35563602
it('multiple source docs', async function() {
35573603
const PersonSchema = new Schema({
35583604
name: String,

0 commit comments

Comments
 (0)