Skip to content

Fix Security Issue: Resolve TOCTOU Race Condition in User Tracking - #9036

Open
tamilr0727-ux wants to merge 1 commit into
JhaSourav07:mainfrom
tamilr0727-ux:fix/toctou-track-user-protection
Open

Fix Security Issue: Resolve TOCTOU Race Condition in User Tracking#9036
tamilr0727-ux wants to merge 1 commit into
JhaSourav07:mainfrom
tamilr0727-ux:fix/toctou-track-user-protection

Conversation

@tamilr0727-ux

Copy link
Copy Markdown
Contributor

Description

Fixes a race condition in verifyAndDeduplicate() that allows concurrent /api/track-user requests for the same username to bypass the cooldown and perform duplicate GitHub validations and database writes.

Changes

  • Made the cooldown check and reservation operation atomic.
  • Updated the protection flow so a request reserves the username before starting asynchronous validation or database operations.
  • Prevented concurrent requests from passing the cooldown check simultaneously.
  • Aligned the behavior with the atomic acquisition pattern used by tryAcquire() in services/github/refresh-policy.ts.
  • Added regression tests covering concurrent requests and cooldown behavior.

Verification

vitest run passes successfully.
✅ Concurrent requests cannot bypass the cooldown.
✅ Only one request can reserve a username within the cooldown window.
✅ GitHub validation and database writes are no longer unnecessarily duplicated.
✅ Existing cooldown behavior remains intact.

Fixes #9035

Pillar

  • 🎨 Pillar 1 — New Theme Design
  • 📐 Pillar 2 — Geometric SVG Improvement
  • 🕐 Pillar 3 — Timezone Logic Optimization
  • 🛠️ Other (Bug fix, refactoring, docs)

Visual Preview

Can Check in the Files Changed Section,

Checklist before requesting a review:

  • I have read the CONTRIBUTING.md file.
  • I have tested these changes locally (localhost:3000/api/streak?user=YOUR_USERNAME).
  • I have run npm run format and npm run lint locally and resolved all errors (CI will fail otherwise).
  • My commits follow the Conventional Commits format (e.g., feat(themes): ..., fix(calculate): ...).
  • I have updated README.md if I added a new theme or URL parameter.
  • I have started the repo.
  • I have made sure that i have only one commit to merge in this PR.
  • The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts).
  • (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.

@vercel

vercel Bot commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

@tamilr0727-ux is attempting to deploy a commit to the jhasourav07's projects Team on Vercel.

A member of the Team first needs to authorize it.

@retenta-bot

retenta-bot Bot commented Aug 15, 2026

Copy link
Copy Markdown

This PR effectively resolves a critical race condition that could lead to security vulnerabilities by ensuring that concurrent requests are handled correctly. It aligns with previous decisions made to enhance security in the GitHub API, particularly regarding error handling and enforcing cooldowns. Great job on adding regression tests to cover these scenarios! Looking forward to your contributions. 😊

@retenta-bot retenta-bot Bot changed the title fix(security): resolve TOCTOU race condition in trackUserProtection Fix Security Issue: Resolve TOCTOU Race Condition in User Tracking Aug 15, 2026
@github-actions github-actions Bot added the status:blocked This PR is blocked due to a failing CI check. label Aug 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

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

1. Run checks locally before pushing:

npm run format:check   # Check Prettier formatting
npm run lint           # Run ESLint
npm run typecheck      # TypeScript type check
npm run test           # Run unit tests (Vitest)
npm run build          # Verify production build passes

2. Auto-fix common issues:

npm run format         # Auto-fix formatting with Prettier
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. 💪

@Aamod007 Aamod007 added mentor:Aamod007 type:security Security fixes, dependency updates, or hardening type:bug Something isn't working as expected level:intermediate Moderate complexity tasks labels Aug 16, 2026

@Aamod007 Aamod007 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a great security fix for the TOCTOU race condition by atomically reserving the slot before async validation.

However, the PR is currently blocked by failing CI checks. Please resolve the CI issues so we can get this merged! I am requesting changes to withhold approval until the block is resolved.

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

Labels

level:intermediate Moderate complexity tasks mentor:Aamod007 status:blocked This PR is blocked due to a failing CI check. type:bug Something isn't working as expected type:security Security fixes, dependency updates, or hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: TOCTOU race condition allows concurrent double-writes in trackUserProtection

2 participants