fix(copilot): import a token any Copilot client left on disk - #3527
Open
olifarhaan wants to merge 1 commit into
Open
fix(copilot): import a token any Copilot client left on disk#3527olifarhaan wants to merge 1 commit into
olifarhaan wants to merge 1 commit into
Conversation
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.
What
crush only picks up a Copilot token from
~/.config/github-copilot/apps.jsonwhen copilot.vim wrote it. A token any other Copilot client left there is ignored, so login starts a device flow with a perfectly good credential sitting on diskWhy it happened
RefreshTokenFromDisk(disk.go:23) looks up one hardcoded key,github.com:Iv1.b507a08c87ecfe98. Clients key their entry by host plus their own app id, so any other id, and the plaingithub.comform, miss.crush login copilot(login.go:149) and the copilot model switch in the TUI (ui.go:2209) both fall through to the device flow when that lookup comes back emptycodecompanion.nvim, avante.nvim and jcode all match on the host part of the key rather than one app id
Fix
Keep preferring our own app id, then fall back to any
github.comentry that carries a token. Keys are sorted so a file with several entries always resolves to the same one instead of following map order. Enterprise hosts stay excluded, since aghe.comtoken is not valid against api.github.comRelated to #3399
Proof
apps.jsonholding one entry from another client:The 401 is the placeholder token in the fixture. The point is which branch runs: before, the credential on disk is invisible and you get a device code
Tests
TestRefreshTokenFromDiskcovers our app id, a foreign app id, the baregithub.comkey, our id winning when both are present, aghe.comentry being skipped, agithub.com.evil.examplelookalike being rejected, an empty token being passed over, and a malformed file.TestRefreshTokenFromDiskIsStableAcrossCallspins the ordering. Three of them fail onmaingo test -race ./internal/oauth/...and golangci-lint v2.11 cleanOne correction on the issue: its main claim is that the device-flow poller fires early. It cannot,
ticker.Resetonly runs after the response lands, so every gap is at least the mandated interval plus the round trip, and the 1s deltas in the capture look like whole-second timestamps. This fixes the other half it reports, that a token placed inapps.jsonis not picked up, so I have left it open