-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
[Feature] Add option to disable filters on each method #14657
Comments
What you're describing sounds like the // The following will throw an error if `req.query.search` is `{ $regex: 'abc' }` or `{ $lt: 'test' }` or any other query filter
const documents = await model
.find({ name: req.query.search })
.sort({ name: 'asc' })
.options({ sanitizeFilter: true })
.exec(); Does the |
But apparently it does not strictly sanitize according to the property type and it is still possible to send data types that do not correspond to the schema, for example: console.log(mongoose.version); // 7.0.3
await mongoose.connect('mongodb://localhost:27017/mongoose_test');
const schema = new mongoose.Schema({ name: String });
const TestModel = mongoose.model('Test', schema);
const doc = new TestModel({ name: 'foobar' });
await doc.save();
const docs = await TestModel.find({ name: [ 'foobar' ] }).setOptions({ sanitizeFilter: true }).exec();
console.log(docs); The results is:
In the What I propose is an option for the input data to adhere to the schema and not just a simple elimination of filters that begin with the |
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
This issue was closed because it has been inactive for 19 days and has been marked as stale. |
#14985 adds a fix for the If you want to disable casting for all strings (both in queries and when creating new documents), you can use import mongoose from 'mongoose';
import assert from 'assert';
mongoose.set('debug', true);
mongoose.Schema.Types.String.set('cast', false);
await mongoose.connect('mongodb://localhost:27017/mongoose_test');
const schema = new mongoose.Schema({
name: {
type: String,
}
});
const TestModel = mongoose.model('Test', schema);
await TestModel.deleteMany({});
const doc = new TestModel({ name: 'foobar' });
await doc.save();
// Throws a CastError
const docs2 = await TestModel.find({ name: new Date() }).setOptions({ sanitizeFilter: true }).exec(); We will consider adding an option to disable casting on a particular query, like |
fix(query): make sanitizeFilter disable implicit $in
We're going to close this issue, because we think that |
Prerequisites
🚀 Feature Proposal
The problem is that every time you work with the express framework or any other for rest services you need two types of redundant input validations, the one that belongs to the controller and the one that belongs to the data layer.
For example, I have a collection called
users
which has a field calledname
, in mongoose I can define the data type as String and a regular expression to validate its content, but from the controller, every time a user wants to search for a list of users through their name I must use a second validation (for example with jsonschema) which validates that thename
property is of type string since if it is passed directly to thefindOne
function of mongoose it could cause a vulnerability of type NoSQL injection and bring unexpected information.The same thing happens in other types of selections such as accessing the user account or searching for a document through its id, the end user could send a conditional object instead of plain text with the
bsonId
string, especially when these Data travels through the body of the post or patch request.In summary, for each project, redundant validations must be created for almost each mongoose function to avoid unexpected executions in the functions to be executed in mongodb and this significantly increases the amount of code and complicates the development architecture, forcing the creation of additional layers of validation or custom methods that duplicate the native functions for each model to be created.
To avoid this problem, i propose something very simple, create an option to temporarily disable the use of filters in the objects that are sent as arguments in the mongoose functions, this would allow the user's input to be used in the development of the application. direct and unfiltered in a completely safe way when performing simple searches.
If the schema says that a field is of type String, then it will automatically reject any data type other than String if filters are disabled.
References:
Motivation
Improve security and save redundant code.
Example
This would help improve the security of everything that is sent to mongoose and reduce the number of vulnerable projects, it would also save a lot of code and work time by avoiding having to create additional validations, especially when there is more than one attribute to control where not all They are of type String.
The text was updated successfully, but these errors were encountered: