fix: resolve critical race condition in signup endpoint#535
fix: resolve critical race condition in signup endpoint#535riddhima25bet10005-a11y wants to merge 1 commit into
Conversation
|
@riddhima25bet10005-a11y is attempting to deploy a commit to the firefistisdead's projects Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughIn ChangesSignup duplicate-user guard refactor
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Possibly related issues
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/controllers/authController.js (1)
65-69: ⚡ Quick winRace condition fix correctly implemented.
The second fetch and duplicate-check after the async bcrypt operation is the critical race-prevention mechanism. Since Node.js executes lines 65-76 atomically (all synchronous operations), any concurrent request that reaches line 65 after this request completes line 76 will see the newly-saved user and correctly exit at line 68.
💡 Optional: Add clarifying comment for the dual-check pattern
Consider adding a brief comment to explain why the duplicate-user check happens twice, since the pattern may not be immediately obvious to future maintainers:
const hashedPassword = await bcrypt.hash( password, 10 ); +// Re-fetch and re-check after async bcrypt to prevent race condition: +// concurrent requests may have saved the same email while we were hashing. users = getUsers(); existingUser = users.find((u) => u.email === email); if (existingUser) { return res.status(400).json({ message: "User already exists" }); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/controllers/authController.js` around lines 65 - 69, The race condition fix is correctly implemented with a dual-check pattern, but add a clarifying comment before the second getUsers() call in the authController.js file to explain why the duplicate-user check happens twice. The comment should explain that the first check (before async operations) and second check (after bcrypt) together prevent race conditions by ensuring no concurrent request creates a user with the same email between the initial validation and the save operation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/controllers/authController.js`:
- Around line 65-69: The race condition fix is correctly implemented with a
dual-check pattern, but add a clarifying comment before the second getUsers()
call in the authController.js file to explain why the duplicate-user check
happens twice. The comment should explain that the first check (before async
operations) and second check (after bcrypt) together prevent race conditions by
ensuring no concurrent request creates a user with the same email between the
initial validation and the save operation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 8df2f4f5-7eb1-4f5f-b8a7-b45a6fb29eb2
📒 Files selected for processing (1)
src/controllers/authController.js
|
Hi @FireFistisDead |
Summary
Fixes a critical race condition in the
signupendpoint. The synchronous array read (getUsers()) previously occurred before the asynchronousbcrypt.hash()which yields the event loop, causing concurrent signups to overwrite each other leading to silent data loss.The fix fetches the user array twice: once before hashing to quickly prevent duplicates and DoS attacks, and once immediately before the synchronous append-and-write to ensure thread safety.
Related issue
Closes #522
Testing
Checklist:
Screenshots / recordings
N/A
Notes
None
Security
Summary by CodeRabbit