Skip to content

fix(auth): add safety verification gate to deserializeUser to prevent stale session crash#681

Open
Aryan0819 wants to merge 3 commits into
GitMetricsLab:mainfrom
Aryan0819:pass
Open

fix(auth): add safety verification gate to deserializeUser to prevent stale session crash#681
Aryan0819 wants to merge 3 commits into
GitMetricsLab:mainfrom
Aryan0819:pass

Conversation

@Aryan0819
Copy link
Copy Markdown
Contributor

Related Issue


Description

🧱 The Architectural Context:
Passport uses session deserialization routines to rebuild user state data from incoming browser cookies on every network request, attaching the verified database document directly to req.user.

❌ The Failure Mechanism:
When an administrator or user deletes an account profile from MongoDB, the web client's active session cookie remains cached locally in their browser. On subsequent API calls, Passport triggers User.findById(id). Because the backend record has been expunged, Mongoose evaluates the query to null.

💥 The Impact:
Passing a raw null downstream means any route execution block attempting to process user profiles (such as evaluating authorization privileges via req.user.role) immediately hits a fatal runtime error: TypeError: Cannot read properties of null. This completely crashes the main Node.js process thread and takes the server infrastructure offline.

✅ The Solution:
Introduced an explicit document existence guard clause directly following the User.findById query in passportConfig.js. If the lookup resolves empty, the execution path intercepts the sequence early and cleanly invokes done(null, false). This commands Express to terminate the stale session and revoke cookie validity without interrupting server uptime.


How Has This Been Tested?

Stale Cookie Runtime Resilience: Simulating an active login session, manually deleted the target user document directly from MongoDB Compass, and re-sent an API request. Verified that the server intercepts the missing reference safely, strips the cookie validation, and redirects with a clean 401 Unauthorized instead of crashing.

Valid Session Continuity: Verified that uncompromised, active account sessions continue to deserialize and authorize access without interruption.


Type of Change

  • Bug fix
  • New feature
  • Code style update
  • Breaking change
  • Documentation update

Aryan0819 added 2 commits June 2, 2026 18:50
Add session middleware with cookie settings
@netlify
Copy link
Copy Markdown

netlify Bot commented Jun 2, 2026

Deploy Preview for github-spy ready!

Name Link
🔨 Latest commit 4953fde
🔍 Latest deploy log https://app.netlify.com/projects/github-spy/deploys/6a1edd83223b530008cbf72c
😎 Deploy Preview https://deploy-preview-681--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 Jun 2, 2026

Warning

Review limit reached

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

More reviews will be available in 41 minutes and 41 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: 01a076d4-838c-4170-8521-6240e26027df

📥 Commits

Reviewing files that changed from the base of the PR and between 53f820b and 4953fde.

📒 Files selected for processing (2)
  • backend/config/passportConfig.js
  • backend/server.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.

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 Report: Stale Session Deserialization Crash (Mongoose + Passport

1 participant