Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query): shallow clone $or, $and if merging onto empty query filter #14580

Merged
merged 6 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2380,13 +2380,21 @@ Query.prototype.merge = function(source) {
}

opts.omit = {};
if (this._conditions && this._conditions.$and && source.$and) {
if (source.$and) {
opts.omit['$and'] = true;
this._conditions.$and = this._conditions.$and.concat(source.$and);
if (this._conditions && this._conditions.$and) {
this._conditions.$and = this._conditions.$and.concat(source.$and);
} else {
this._conditions.$and = [...source.$and];
}
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
}
if (this._conditions && this._conditions.$or && source.$or) {
if (source.$or) {
opts.omit['$or'] = true;
this._conditions.$or = this._conditions.$or.concat(source.$or);
if (this._conditions && this._conditions.$or) {
this._conditions.$or = this._conditions.$or.concat(source.$or);
} else {
this._conditions.$or = [...source.$or];
}
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
}

// plain object
Expand Down
7 changes: 6 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
const isMongooseArray = require('./types/array/isMongooseArray');
const isMongooseDocumentArray = require('./types/documentArray/isMongooseDocumentArray');
const isBsonType = require('./helpers/isBsonType');
const isPOJO = require('./helpers/isPOJO');
const getFunctionName = require('./helpers/getFunctionName');
const isMongooseObject = require('./helpers/isMongooseObject');
const promiseOrCallback = require('./helpers/promiseOrCallback');
Expand Down Expand Up @@ -264,7 +265,11 @@
continue;
}
if (to[key] == null) {
to[key] = from[key];
if (isPOJO(from[key])) {
to[key] = { ...from[key] };
} else {
to[key] = from[key];
Dismissed Show dismissed Hide dismissed
}
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
} else if (exports.isObject(from[key])) {
if (!exports.isObject(to[key])) {
to[key] = {};
Expand Down
37 changes: 37 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4054,6 +4054,25 @@ describe('Query', function() {
});
});

it('shallow clones $and, $or if merging with empty filter (gh-14567) (gh-12944)', function() {
const TestModel = db.model(
'Test',
Schema({ name: String, age: Number, active: Boolean })
);

let originalQuery = { $and: [{ active: true }] };
let q = TestModel.countDocuments(originalQuery)
.and([{ age: { $gte: 18 } }]);
assert.deepStrictEqual(originalQuery, { $and: [{ active: true }] });
assert.deepStrictEqual(q.getFilter(), { $and: [{ active: true }, { age: { $gte: 18 } }] });

originalQuery = { $or: [{ active: true }] };
q = TestModel.countDocuments(originalQuery)
.or([{ age: { $gte: 18 } }]);
assert.deepStrictEqual(originalQuery, { $or: [{ active: true }] });
assert.deepStrictEqual(q.getFilter(), { $or: [{ active: true }, { age: { $gte: 18 } }] });
});

it('should avoid sending empty session to MongoDB server (gh-13052)', async function() {
const m = new mongoose.Mongoose();

Expand Down Expand Up @@ -4236,4 +4255,22 @@ describe('Query', function() {
q.sort({}, { override: true });
assert.deepStrictEqual(q.getOptions().sort, {});
});

it('avoids mutating user-provided query selectors (gh-14567)', async function() {
const TestModel = db.model(
'Test',
Schema({ name: String, age: Number, active: Boolean })
);

await TestModel.create({ name: 'John', age: 21 });
await TestModel.create({ name: 'Bob', age: 35 });

const adultQuery = { age: { $gte: 18 } };

const docs = await TestModel.find(adultQuery).where('age').lte(25);
assert.equal(docs.length, 1);
assert.equal(docs[0].name, 'John');

assert.deepStrictEqual(adultQuery, { age: { $gte: 18 } });
});
});