Skip to content

fix(auth): exclude password hash from req.user in deserializeUser#577

Open
anshul23102 wants to merge 1 commit into
GitMetricsLab:mainfrom
anshul23102:fix/deserialize-user-password-555
Open

fix(auth): exclude password hash from req.user in deserializeUser#577
anshul23102 wants to merge 1 commit into
GitMetricsLab:mainfrom
anshul23102:fix/deserialize-user-password-555

Conversation

@anshul23102
Copy link
Copy Markdown
Contributor

Summary

Fixes #555

deserializeUser called User.findById(id) with no field projection, attaching the full Mongoose document (including the password bcrypt hash) to req.user on every authenticated request.

Risk

Any route handler that returns req.user to the client — a profile endpoint, an admin panel, or a future /api/auth/me — would include the password field 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

// Before
const user = await User.findById(id);

// After
const user = await User.findById(id).select('-password -__v').lean();
  • .select('-password -__v') excludes the hash and the internal __v version key.
  • .lean() returns a plain JS object instead of a Mongoose document, so model methods are not accessible via req.user.

Test Plan

  • Login and call any authenticated endpoint that returns req.user. Confirm the response does not contain a password field.
  • Confirm req.user.comparePassword is undefined (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!

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.
@netlify
Copy link
Copy Markdown

netlify Bot commented May 27, 2026

Deploy Preview for github-spy ready!

Name Link
🔨 Latest commit 55706df
🔍 Latest deploy log https://app.netlify.com/projects/github-spy/deploys/6a170fd3b657ab00086e4e3a
😎 Deploy Preview https://deploy-preview-577--github-spy.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

Warning

Review limit reached

@anshul23102, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 39f4149b-2415-4eca-921a-419378b9376b

📥 Commits

Reviewing files that changed from the base of the PR and between 4ae0ef6 and 55706df.

📒 Files selected for processing (1)
  • backend/config/passportConfig.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@anshul23102
Copy link
Copy Markdown
Contributor Author

Hey @GitMetricsLab, pinging this as part of NSoC'26. Happy to make any adjustments you need. Thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [GSSoC'26] deserializeUser returns full Mongoose document, silently attaching password hash to req.user on every authenticated request

1 participant