Skip to content

fix(team): add row lock to accept_invitation preventing double-accept race#356

Merged
robotlearning123 merged 5 commits intomainfrom
fix/invite-accept-race
Mar 28, 2026
Merged

fix(team): add row lock to accept_invitation preventing double-accept race#356
robotlearning123 merged 5 commits intomainfrom
fix/invite-accept-race

Conversation

@robotlearning123
Copy link
Copy Markdown
Member

Summary

  • Add with_for_update() row locks to both the invitation lookup and the email-uniqueness check in accept_invitation()
  • Two concurrent POST /join/{token} requests could both read the invitation as "pending" (no lock) and proceed to create duplicate staff accounts
  • Follows the existing with_for_update() pattern already used in src/lab_manager/services/inventory.py

Root Cause

TOCTOU (Time-of-Check-Time-of-Use) race condition: the invitation status check and email uniqueness check at lines 367-384 were done without row locks, so two concurrent requests with the same token could both pass the checks before either transaction committed.

Test plan

  • Added test_join_concurrent_accept_prevented — verifies only one staff account is created and the invitation ends as "accepted" when the same token is used twice
  • All 27 tests in tests/test_team.py pass

Closes #353
Found by bug-hunter autonomous loop.

🤖 Generated with Claude Code

… race

Two concurrent POST /join/{token} requests could both read the invitation
as "pending" and create duplicate staff accounts. Add with_for_update() to
both the invitation and email-uniqueness queries so the second transaction
blocks until the first commits and sees the updated status.

Closes #353

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@robotlearning123
Copy link
Copy Markdown
Member Author

QA: APPROVED

@github-actions github-actions bot added the ci-verified All required CI checks have passed label Mar 28, 2026
@robotlearning123 robotlearning123 merged commit 434b101 into main Mar 28, 2026
20 of 21 checks passed
@robotlearning123 robotlearning123 deleted the fix/invite-accept-race branch March 28, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-verified All required CI checks have passed python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(security): CRITICAL TOCTOU race in invitation acceptance allows duplicate staff

2 participants