hard: registration wallet-existence checks are TOCTOU — concurrent registers create duplicate identities - #127
Open
Godfrey-Delight wants to merge 1 commit into
Conversation
…gisters create duplicate identities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 Related Issue
Closes #120
🔖 Title
fix(auth): enforce database-level uniqueness in register and eliminate race conditions
📝 Description
Eliminated application-side pre-check queries in
AuthService.register()(findByWalletandcheckUsernameExists) which allowed race conditions during concurrent user registrations (e.g., double-click or retry storms). Uniqueness is now enforced directly by DB-levelUNIQUEconstraints onusers.wallet_addressandusers.username. Unique constraint violations (Postgres23505) are caught and mapped to existing structured 409ConflictExceptionerror responses (AUTH_WALLET_EXISTS,AUTH_USERNAME_TAKEN). In addition, failed registration attempts clean up uploaded avatar objects and partial user records to prevent orphaned state.🔄 Changes Made
20260826130000_ensure_users_unique_constraints.sqlensuringUNIQUEindexes exist onusers.wallet_addressandusers.username.UsersRepository.createProfile()to catch Postgres error23505and map wallet/username collisions to structuredConflictExceptionresponses. AddeddeleteAvataranddeleteUserByIdmethods.AuthService.register()to attempt direct insertion viacreateProfile(), eliminating racy pre-checks. Wrapped profile creation, avatar upload, and token issuance in atry...catchblock with rollback cleanup.23505error mapping, parallel duplicate-wallet and duplicate-username race tests (Promise.allSettled), sequential re-registration compatibility, and avatar/user cleanup on failure.context/progress-tracker.md.📸 Screenshots (if applicable)
N/A (Backend API logic change)
🗒️ Additional Notes
npm run buildpassed cleanly with 0 TypeScript errors.npm testpassed with 30/30 test suites green (355/355 total tests passing).