Skip to content

Commit 663f21e

Browse files
authored
Merge pull request Automattic#14985 from Automattic/vkarpov15/Automatticgh-14657
fix(query): make sanitizeFilter disable implicit $in
2 parents 429f855 + 6076d1f commit 663f21e

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

lib/cast.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
2828
* @param {Object} [options] the query options
2929
* @param {Boolean|"throw"} [options.strict] Wheter to enable all strict options
3030
* @param {Boolean|"throw"} [options.strictQuery] Enable strict Queries
31+
* @param {Boolean} [options.sanitizeFilter] avoid adding implict query selectors ($in)
3132
* @param {Boolean} [options.upsert]
3233
* @param {Query} [context] passed to setters
3334
* @api private
@@ -372,7 +373,7 @@ module.exports = function cast(schema, obj, options, context) {
372373

373374
}
374375
}
375-
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {
376+
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1 && !options.sanitizeFilter) {
376377
const casted = [];
377378
const valuesArray = val;
378379

lib/query.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4900,6 +4900,9 @@ Query.prototype.cast = function(model, obj) {
49004900
opts.strictQuery = this.options.strictQuery;
49014901
}
49024902
}
4903+
if ('sanitizeFilter' in this._mongooseOptions) {
4904+
opts.sanitizeFilter = this._mongooseOptions.sanitizeFilter;
4905+
}
49034906

49044907
try {
49054908
return cast(model.schema, obj, opts, this);

test/query.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,21 @@ describe('Query', function() {
35203520
assert.ifError(q.error());
35213521
assert.deepEqual(q._conditions, { username: 'val', pwd: { $gt: null } });
35223522
});
3523+
3524+
it('sanitizeFilter disables implicit $in (gh-14657)', function() {
3525+
const schema = new mongoose.Schema({
3526+
name: {
3527+
type: String
3528+
}
3529+
});
3530+
const Test = db.model('Test', schema);
3531+
3532+
const q = Test.find({ name: ['foobar'] }).setOptions({ sanitizeFilter: true });
3533+
q._castConditions();
3534+
assert.ok(q.error());
3535+
assert.equal(q.error().name, 'CastError');
3536+
});
3537+
35233538
it('should not error when $not is used with $size (gh-10716)', async function() {
35243539
const barSchema = Schema({
35253540
bar: String

0 commit comments

Comments
 (0)