fix(auth): exclude password hash from req.user in deserializeUser#577
fix(auth): exclude password hash from req.user in deserializeUser#577anshul23102 wants to merge 1 commit into
Conversation
Fixes GitMetricsLab#555 deserializeUser called User.findById(id) with no projection, attaching the full Mongoose document including the bcrypt password hash to req.user on every authenticated request. Any route handler returning req.user would silently expose the hash to the client. Added .select('-password -__v') to exclude the hash and internal version key. Added .lean() to return a plain object rather than a full Mongoose document, so model methods are not accessible on req.user.
✅ Deploy Preview for github-spy ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Warning Review limit reached
More reviews will be available in 18 minutes and 57 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Hey @GitMetricsLab, pinging this as part of NSoC'26. Happy to make any adjustments you need. Thanks! |
Summary
Fixes #555
deserializeUsercalledUser.findById(id)with no field projection, attaching the full Mongoose document (including thepasswordbcrypt hash) toreq.useron every authenticated request.Risk
Any route handler that returns
req.userto the client — a profile endpoint, an admin panel, or a future/api/auth/me— would include thepasswordfield in the response without the author realising it. A leaked bcrypt hash is directly attackable offline with GPU-accelerated wordlist cracking.Change
backend/config/passportConfig.js.select('-password -__v')excludes the hash and the internal__vversion key..lean()returns a plain JS object instead of a Mongoose document, so model methods are not accessible viareq.user.Test Plan
req.user. Confirm the response does not contain apasswordfield.req.user.comparePasswordisundefined(lean object has no model methods).Important
GSSoC'26 contribution - Please add labels (
gssoc26,type:security,level:intermediate) to help with point tracking. Thank you!