Skip to content

fix(backend): prevent concurrent LeetCode profile verification race conditions (#416)#429

Open
itssagarK wants to merge 4 commits into
Ixotic27:mainfrom
itssagarK:416-fix-leetcode-verification-race
Open

fix(backend): prevent concurrent LeetCode profile verification race conditions (#416)#429
itssagarK wants to merge 4 commits into
Ixotic27:mainfrom
itssagarK:416-fix-leetcode-verification-race

Conversation

@itssagarK

@itssagarK itssagarK commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR fixes a Time-Of-Check to Time-Of-Use (TOCTOU) race condition in the LeetCode profile verification flow.

Previously, the verification endpoint checked whether a LeetCode profile was available before assigning it to a user. Under concurrent requests, multiple users could pass the availability check and attempt to claim the same profile simultaneously, leading to inconsistent ownership.

This PR introduces an atomic claim mechanism to ensure that only one user can successfully claim a profile.

Changes Made

Atomic Profile Claiming

  • Normalized the LeetCode username once and reused it throughout the verification flow.
  • Added an atomic update path for existing unclaimed profiles.
  • Added conflict detection using .is("claimed_by", null) during updates.
  • Prevented concurrent requests from claiming the same profile.

Safe Profile Creation

  • Added an atomic insert flow when no profile record exists.
  • Relies on database uniqueness constraints to prevent duplicate claims.
  • Returns a 409 Conflict if another request claims the profile first.

Improved Error Handling

  • Returns a clear conflict message when a profile is claimed during a concurrent request.
  • Prevents ambiguous ownership states.

Reduced Race Window

  • Claims the profile before performing expensive LeetCode API requests.
  • Ensures only the successful claimant proceeds with profile synchronization.

Why this matters

Before this fix

  • Two users could attempt to verify the same LeetCode profile simultaneously.
  • Both requests could pass the availability check.
  • Ownership could become inconsistent depending on request timing.
  • Identity verification integrity could be compromised.

After this fix

  • Profile claiming is protected at the database layer.
  • Only one request can successfully claim a profile.
  • Concurrent requests fail safely with a conflict response.
  • Ownership remains consistent and reliable.

Testing

Verified:

  • Existing unclaimed profiles can only be claimed once.
  • Concurrent verification attempts return proper conflict responses.
  • New profile creation respects uniqueness constraints.
  • Existing verification flow remains functional.

Related Issue

Fixes #416

Checklist

  • npm run lint passes
  • Tested locally
  • No secrets or .env values committed
  • I acknowledge that an automated AI Reviewer will perform a preliminary review of this PR.
  • I have starred this repository! (We prioritize PRs and assignments for stargazers)

@vercel

vercel Bot commented Jun 9, 2026

Copy link
Copy Markdown

@itssagarK is attempting to deploy a commit to the ixotic27-8245's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security Scan: Clean

No suspicious patterns detected. The official Copilot bot will provide detailed AI feedback shortly.

If you enjoyed contributing, please consider starring the repository!

@github-actions github-actions Bot added backend Backend/API related frontend good first issue Good for newcomers Gssoc 26 Part of GirlScript Summer of Code 2026 gssoc:approved Approved GSSoC contribution level:intermediate Intermediate difficulty level type:bug Something isn't working as expected labels Jun 9, 2026
@itssagarK

itssagarK commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

Hi @Ixotic27,

This PR fixes the TOCTOU race condition reported in #416.

The solution introduces atomic claim logic at the database layer, ensuring that only one user can successfully claim a LeetCode profile even when multiple verification requests occur simultaneously.

The existing verification workflow remains unchanged while preventing ownership conflicts and improving profile integrity.

Thank you for reviewing.

@github-actions github-actions Bot added the status:blocked This PR is blocked due to a failing CI check. label Jun 9, 2026
@itssagarK itssagarK changed the title fix(presence): prevent stale polling responses from overwriting newer websocket updates (#415) fix(backend): prevent concurrent LeetCode profile verification race conditions (#416) Jun 9, 2026
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

🚨 Hey @itssagarK, the CI Pipeline is failing on this PR and it has been marked as status:blocked.

🔍 What failed:

  • Production Build failed at step(s): Build

📋 Error Details (first 2):

Please fix the issues before this can be reviewed. Here's how:

1. Run checks locally before pushing:

npm run lint           # Run ESLint
npm run build          # Verify production build passes

2. Auto-fix common issues:

npm run lint -- --fix  # Auto-fix lint errors where possible

3. Check the full failure log here:
👉 View CI Run

Once you push a fix and the CI passes, the status:blocked label will be removed automatically. 💪

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

Labels

backend Backend/API related frontend good first issue Good for newcomers gssoc:approved Approved GSSoC contribution Gssoc 26 Part of GirlScript Summer of Code 2026 level:intermediate Intermediate difficulty level status:blocked This PR is blocked due to a failing CI check. type:bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Concurrent LeetCode profile verification requests can bypass uniqueness checks

2 participants