-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
findOne method returns first document, even when the query value passed is "undefined" - can create privacy issues #14948
Comments
I believe this is expected behavior. This is not the first time this issue has been raised. If I'm not confusing this with something else, what happens under the hood is that it takes out the undefined in the search query and so what gets sent to mongo is |
…rror instead of returning first doc Re: #14948
This is currently expected behavior, and not unique to Mongoose. The following script shows that 'use strict';
const mongoose = require('mongoose');
void async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
const Test = mongoose.model('Test', mongoose.Schema({ name: String }));
await Test.findOneAndUpdate({ name: 'test' }, { name: 'test' }, { upsert: true });
// The following all print the first document in the db
console.log(await Test.findOne(undefined));
console.log(await Test.findOne(null));
console.log(await Test.collection.findOne(undefined));
// Throws "MongoServerError: Expected field filterto be of type object"
console.log(await Test.collection.findOne(null));
}(); The major difference is that Mongoose |
fix(model+query): make `findOne(null)`, `find(null)`, etc. throw an error instead of returning first doc
We will ship the |
Prerequisites
Last performant version
8.7.0
Slowed down in version
8.7.0
Node.js version
20.17.0
🦥 Performance issue
When using the findOne() method, passing it a query value of undefined, like such:
User.findOne(undefined)
the response is the first document in the
User
collection.This is unique to Mongoose, and different than
db.collection.findOne(undefined)
in regular MongoDB, which returnsnull
in such a case.The problem: privacy issues. A hacker can pass 'undefined' and retrieve first user in the database which may have sensitive information.
Steps to Reproduce
getUser
:export const getUser = async (req: Request, res: Response) => { try { const user = await User.findOne(undefined); **console.log(user);** res.status(200).json(user); } catch (error) { res.status(500).json({ message: "Error fetching user", error }); } };
Expected Behavior
Notice what the console logs as "user" to be the first user in your database, (and not
null
)The text was updated successfully, but these errors were encountered: