fix(pwa): claim device codes atomically in /cli/device/token#51
Open
khipu-agent wants to merge 1 commit into
Open
fix(pwa): claim device codes atomically in /cli/device/token#51khipu-agent wants to merge 1 commit into
khipu-agent wants to merge 1 commit into
Conversation
The read-check-claim sequence let two concurrent polls both read "approved" before either UPDATE landed, so one single-use device code minted two API keys. Make the claim conditional (status = 'approved') and reject the loser, mirroring the /cli/token fix (moshcoder#46).
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.
Bug
POST /cli/device/token(themoshcode login --deviceflow) reads the device code row, checksstatus === 'approved', and only then marks it claimed with an unconditionalUPDATE. With a remote (network) database, two concurrent polls with the samedevice_codecan both pass the status check before either UPDATE lands — so one single-use device code mints two API keys.This is the same race as the one fixed in
/cli/token(#46); the device-code exchange right below it has the identical read-check-write sequence.Repro:
apps/pwa/test/cli-device-token.test.mjsfires two concurrent polls (separate connections, statements deferred to macrotasks to simulate the remote DB the app runs against in production) — before the fix both return 200 and two rows land inapi_keys.Fix
Claim the code with a conditional
UPDATE … SET status = 'claimed' WHERE device_code = ? AND status = 'approved'and reject the poll when no row was claimed — the same pattern as/cli/token.Tests
New integration tests in
apps/pwa/test/cli-device-token.test.mjs:authorization_pendingand mints nothing.Full suite: 167/167 green.