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): make sanitizeFilter disable implicit $in #14985

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
fix(query): make sanitizeFilter disable implicit $in
Re: #14657
  • Loading branch information
vkarpov15 committed Oct 26, 2024
commit 6076d1f8ba2072af210a1b4cb607777afac33150
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 @@ -4863,6 +4863,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