Skip to content

Commit

Permalink
Merge pull request #14985 from Automattic/vkarpov15/gh-14657
Browse files Browse the repository at this point in the history
fix(query): make sanitizeFilter disable implicit $in
  • Loading branch information
vkarpov15 authored Oct 28, 2024
2 parents 429f855 + 6076d1f commit 663f21e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
* @param {Object} [options] the query options
* @param {Boolean|"throw"} [options.strict] Wheter to enable all strict options
* @param {Boolean|"throw"} [options.strictQuery] Enable strict Queries
* @param {Boolean} [options.sanitizeFilter] avoid adding implict query selectors ($in)
* @param {Boolean} [options.upsert]
* @param {Query} [context] passed to setters
* @api private
Expand Down Expand Up @@ -372,7 +373,7 @@ module.exports = function cast(schema, obj, options, context) {

}
}
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1 && !options.sanitizeFilter) {
const casted = [];
const valuesArray = val;

Expand Down
3 changes: 3 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4900,6 +4900,9 @@ Query.prototype.cast = function(model, obj) {
opts.strictQuery = this.options.strictQuery;
}
}
if ('sanitizeFilter' in this._mongooseOptions) {
opts.sanitizeFilter = this._mongooseOptions.sanitizeFilter;
}

try {
return cast(model.schema, obj, opts, this);
Expand Down
15 changes: 15 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3520,6 +3520,21 @@ describe('Query', function() {
assert.ifError(q.error());
assert.deepEqual(q._conditions, { username: 'val', pwd: { $gt: null } });
});

it('sanitizeFilter disables implicit $in (gh-14657)', function() {
const schema = new mongoose.Schema({
name: {
type: String
}
});
const Test = db.model('Test', schema);

const q = Test.find({ name: ['foobar'] }).setOptions({ sanitizeFilter: true });
q._castConditions();
assert.ok(q.error());
assert.equal(q.error().name, 'CastError');
});

it('should not error when $not is used with $size (gh-10716)', async function() {
const barSchema = Schema({
bar: String
Expand Down

0 comments on commit 663f21e

Please sign in to comment.