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

findOne method returns first document, even when the query value passed is "undefined" - can create privacy issues #14948

Open
2 tasks done
Alef5750 opened this issue Oct 10, 2024 · 0 comments

Comments

@Alef5750
Copy link

Alef5750 commented Oct 10, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the performance issue has not already been reported

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 returns null 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

  1. Create a User collection in a mongoDB database.
  2. add a users to the database with some fields (username, password, etc.)
  3. in your server's controller, run a Mongoose method 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant