Repository navigation
fix(copilot): follow the credential's GitHub host on every quota rung - #1312
Closed
ozymandiashh wants to merge 3 commits into
Closed
ozymandiashh wants to merge 3 commits into
ozymandiashh wants to merge 3 commits into
Conversation
…d gh rungs getagentseal#1286 taught the credential files to carry the GitHub host their token came from, but the environment and `gh auth token` rungs carried none, so they were always addressed to api.github.com. A user whose only Copilot credential is a GitHub Enterprise Cloud login — GH_TOKEN exported alongside GH_HOST, or `gh auth login --hostname <tenant>.ghe.com` — got a 401 and a terminal failure with nothing to act on. The environment rung now reads GH_HOST out of the same environment as the token. The gh rung resolves the host from gh's own hosts.yml, found via GH_CONFIG_DIR, then XDG_CONFIG_HOME/gh, then ~/.config/gh, and picked the way gh picks the host for `gh auth token`: GH_HOST, else the single configured host, else dotcom. That is a file read rather than a second `gh auth status` spawn, because gh writes hosts.yml for every login, keyring-backed ones included. Both are new untrusted inputs, so they stay raw all the way to `apiHost(for:)`, where getagentseal#1286's character validation refuses a crafted value such as `evil.com?.ghe.com` before any URL exists — that host passes the `.ghe.com` suffix check but builds a URL whose host is api.evil.com, which would then receive the Authorization header.
The last rung of the chain is a token the user pastes into Settings, and an enterprise personal access token pasted there was still sent to api.github.com. Settings now takes the host beside the token, defaulting to github.com, and saves both in the same provider Keychain record so the host a request is addressed to always comes from the entry that carried the token. A host no endpoint can be derived from is refused at the field instead of being stored and reported later as a terminal fetch failure, and the crafted values getagentseal#1286 guarded against are refused there too. Clearing the token clears the host with it, so a stale tenant can never be paired with the next token. The record's decoding moves into an extension and reads every key with decodeIfPresent: Swift's synthesized init(from:) ignores a property's default and throws on a missing key, so adding a field would otherwise make every credential saved before it unreadable — for every provider, not just Copilot. The dormant connection row no longer promises api.github.com before anything has been fetched; it names a host only once one has answered. Tests cover the three new host sources with a hostile value each, the rung wiring for all five rungs, the gh hosts.yml key scan, and that a record written before the host field still decodes.
The CLI adapter read only the editor-plugin files, so `codeburn quota` and the Windows Capacity Dock — which has no Settings screen to fall back on — could not see an enterprise credential that lives anywhere else. It now walks the same rung order as the menubar for the sources a CLI can reach: the plugin files, then COPILOT_GITHUB_TOKEN / GH_TOKEN / GITHUB_TOKEN paired with GH_HOST, then gh's own hosts.yml. The gh rung reads the oauth_token out of that file rather than spawning gh, so the token and the host come from one entry and a keyring-backed login simply has no token there and falls through. The pasted-token rung stays macOS-only: it lives in that app's Keychain item, which has no CLI equivalent. Note the behaviour change this implies: a shell that exports GITHUB_TOKEN, or a gh login with no Copilot entitlement, now reports a Copilot fetch failure where it previously reported "disconnected". That is the same answer the menubar has always given for those credentials. The existing suites pin env and ghConfigPaths explicitly so a developer's own GH_TOKEN or gh login can never change what they read. Closes getagentseal#1306
Member
|
Landed as #1313 (4a316da) with your three commits and authorship intact, rebased onto current main, plus one commit dropping the CLI rung additions: #1306 was about the menubar's hostless rungs, and adding env and gh rungs to codeburn quota would turn an exported GITHUB_TOKEN or a Copilot-less gh login into a fetch failure where it said disconnected before. The macOS half is unchanged. Verified on this machine: winning rung is gh auth token, hosts.yml lists github.com only, resolved URL equals main, HTTP 200 with the expected keys; GH_HOST=acme.ghe.com resolves to api.acme.ghe.com, github.acme.net and evil.com?.ghe.com are refused with no request. 506 + 159 tests, mutation-proven. Thank you. |
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
*.ghe.comhosts #1286 fixedhosts.jsonandapps.json; the rungs that carried none —COPILOT_GITHUB_TOKEN/GH_TOKEN/GITHUB_TOKEN,gh auth token, and apasted token — were still sent to
api.github.com, so a user whose onlyCopilot credential is a GitHub Enterprise Cloud login got a 401 and a
terminal failure. The environment rung reads
GH_HOST, theghrung readsthe host out of gh's own
hosts.yml(GH_CONFIG_DIR, thenXDG_CONFIG_HOME/gh, then~/.config/gh, picked the way gh picks the hostfor
gh auth token, with no second subprocess), and macOS Settings gains aGitHub host field beside the pasted token, saved in the same Keychain record
so the token and the host can never drift apart.
GH_HOST, gh'shosts.ymlkeys andthe pasted host are three new untrusted inputs, and each reaches the existing
character validation untouched, so a crafted value such as
evil.com?.ghe.comfails closed with no request rather than pointing theAuthorization header at
api.evil.com. Settings refuses an unaddressablehost at the field instead of storing it, and the dormant connection row no
longer promises
api.github.combefore anything has been fetched.codeburn quotamirrors the environment andghrungs so the CLI and theWindows Capacity Dock report what the menubar reports; its
ghrung readsthe token and the host out of one
hosts.ymlentry instead of spawning gh.The pasted rung stays macOS-only — it lives in that app's Keychain item.
Behaviour change worth noting: a shell exporting
GITHUB_TOKEN, or a ghlogin without Copilot, now reports a Copilot fetch failure where it used to
report "disconnected", which is what the menubar has always reported for
those credentials.
hand-written
init(from:)usingdecodeIfPresent: Swift's synthesizeddecoder ignores property defaults and throws on a missing key, so every
credential saved before the field — for every provider — would otherwise stop
decoding. A test pins that.
Testing
the note below: this machine has a
github.comCopilot credential only.npm testpassesnpm run buildsucceedscd mac && swift buildpasses.swift testcannot run on this machine (CLTonly, no
Testingmodule — also true on pristinemain), so the new purelogic and all five rung resolutions were verified with a standalone
swiftcharness compiling the real sources and asserting exactly what the XCTest files
assert; each of seven mutations (dropping the host from any rung, dropping the
character guard, reading indented YAML keys as hosts, making
hosta requireddecode key) was caught.
npx tsc --noEmitis clean andnpm testreports 3987passing. Verified on this machine against a real
github.comcredential only —there is no GitHub Enterprise Cloud account or enterprise
ghlogin here, sothe
*.ghe.compaths are unit-tested, as in #1305.Closes #1306