fix: prevent double-activation of machine tokens via atomic CAS - #197
Open
gusye1234 wants to merge 2 commits into
Open
fix: prevent double-activation of machine tokens via atomic CAS#197gusye1234 wants to merge 2 commits into
gusye1234 wants to merge 2 commits into
Conversation
Adds status='pending' condition to the UPDATE WHERE clause so only one concurrent request can activate a token. Returns success/failure to the route handler which now returns 409 on conflict. Closes #150
The function now returns a boolean indicating success, so the test mock must return true for the happy-path tests.
gusye1234
commented
May 28, 2026
gusye1234
left a comment
Contributor
Author
There was a problem hiding this comment.
Review — PR #197: prevent double-activation of machine tokens via atomic CAS
Solid fix for a TOCTOU race condition. The approach is textbook CAS:
- The WHERE clause now includes
eq(machineToken.status, "pending")— two concurrent activations can't both succeed because only the first UPDATE will match a row. .returning()+ boolean return makes the caller aware of whether activation actually happened.- 409 response for already-activated tokens is the correct HTTP semantic (Conflict).
- Tests properly updated to expect
truefrom the mock.
Checklist:
- Functionality: eliminates race window for double-activation
- Edge cases: concurrent requests handled atomically
- Security: prevents duplicate token activation and associated side-effects
- Code quality: minimal, clear changes
- Tests: CI green, mock return values updated
LGTM — ready to merge.
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.
Summary
status = 'pending'condition to the UPDATE WHERE clause inactivateMachineToken()so only one concurrent request can succeedChanges
src/shared/src/db/queries/machine-token.ts: UPDATE now uses atomic compare-and-swap, returns success booleansrc/web/src/app/api/machine-tokens/activate/route.ts: Checks activation result, returns 409 on race conflictTest plan
Closes #150