Skip to content

fix(menubar): follow the credential's GitHub host on every quota rung - #1313

Merged
iamtoruk merged 4 commits into
mainfrom
fix/copilot-host-rungs
Sep 12, 2026
Merged

iamtoruk merged 4 commits into
mainfrom
fix/copilot-host-rungs

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Supersedes #1312 by @ozymandiashh. Same three commits rebased onto main, plus one commit that drops the CLI half.

Closes #1306.

What ships

The macOS menubar's Copilot rungs that carried no host:

  • env rung reads GH_HOST out of the same environment as COPILOT_GITHUB_TOKEN / GH_TOKEN / GITHUB_TOKEN
  • gh rung resolves the host from gh's own hosts.yml (GH_CONFIG_DIR, then XDG_CONFIG_HOME/gh, then ~/.config/gh), picked the way gh's DefaultHost picks it: GH_HOST, else the single configured host, else github.com. File read, not a second subprocess: gh writes that file for keyring logins too.
  • pasted token gets a GitHub host field in Settings, saved in the same Keychain record as the token, defaulting to github.com. CapacityDockProviderCredential decoding moves to a hand-written init(from:) with decodeIfPresent so every record saved before the field still decodes, for all providers.
  • the dormant Settings connection row stops promising api.github.com.

Every new host source stays raw until CopilotHostEndpoint.apiHost(for:), where #1286's character check refuses it. evil.com?.ghe.com and a GHES host like github.acme.net fail closed with no request instead of falling back to dotcom with an enterprise token.

What was dropped

#1312's third commit also added the env and gh rungs to src/quota/copilot.ts. #1306 did not ask for that, and it changes behaviour: a shell with GITHUB_TOKEN exported, or a gh login without Copilot, would start reporting a Copilot fetch failure where codeburn quota reported "disconnected". 20bde265 puts src/quota/copilot.ts and its three suites back to main byte for byte, hostname check included, and trims the CLI claims from CHANGELOG and docs. git diff main -- src/ tests/ is empty.

Checks

This machine, github.com only. *.ghe.com is unit-tested, not exercised live.

Rung state here: no COPILOT_GITHUB_TOKEN / GH_TOKEN / GITHUB_TOKEN, no GH_HOST, no ~/.config/github-copilot/*, ~/.copilot/config.json present but carrying no token, ~/.config/gh/hosts.yml listing one host (github.com, keyring-backed, no oauth_token in the file). Winning rung is 4, gh auth token.

The PR's resolver compiled standalone and run against that state:

gh hosts.yml resolved to: /Users/<me>/.config/gh/hosts.yml
gh hosts.yml top-level hosts: ["github.com"]

rung 4 (gh auth token)   host github.com -> https://api.github.com/copilot_internal/user
rung 3 (env, no GH_HOST) host nil        -> https://api.github.com/copilot_internal/user
rung 5 (pasted, no host) host nil        -> https://api.github.com/copilot_internal/user

GH_HOST=acme.ghe.com      -> https://api.acme.ghe.com/copilot_internal/user
GH_HOST=github.acme.net   -> REFUSED, no request built, Settings rejects the same value
GH_HOST=evil.com?.ghe.com -> REFUSED, no request built, Settings rejects the same value
GH_HOST=GitHub.COM        -> https://api.github.com/copilot_internal/user

Same URL main builds. Issuing it with the local gh token and the headers this code sends: HTTP 200, body keys include copilot_plan (individual) and quota_snapshots (chat, completions, premium_interactions).

Builds and tests: swift build clean, swift test 506 swift-testing in 59 suites + 159 XCTest, 0 failures, swift build -c release clean. Root npx tsc --noEmit clean, vitest tests/quota-copilot-* 20/20, tests/quota-providers.test.ts 17/17.

Mutation check: making the env rung ignore GH_HOST fails testEnvironmentTokenFollowsGHHost and testHostileHostFromAnyNewSourceFailsClosedWithoutARequest (4 assertions).

ozymandiashh and others added 4 commits September 12, 2026 12:02
…d gh rungs

#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 #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 #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 #1306
Issue #1306 is about the macOS menubar's rungs. The CLI adapter gaining the
environment and gh rungs is a behaviour change it did not ask for: a shell with
GITHUB_TOKEN exported, or a gh login without Copilot, would start reporting a
fetch failure where `codeburn quota` reported "disconnected".

src/quota/copilot.ts and its suites go back to main untouched, including the
hostname character check the endpoint helpers already had. CHANGELOG and docs
drop the CLI claims.
@iamtoruk
iamtoruk merged commit 4a316da into main Sep 12, 2026
11 checks passed
iamtoruk added a commit that referenced this pull request Sep 12, 2026
Rebase resolution for #1289 onto main (#1306, #1313, #1314, #1315, #1328,
#1329). Conflicts kept both intents: main's second-row title composition,
glance-window accessibility actions, cache-read line, host field and merged
Notifications section keep their behaviour and now resolve through L(…).

Adds 14 keys to both tables, drops 4 that no longer have a call site, and
retranslates the two Copilot explanations main rewrote.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copilot quota: gh auth token, env var and pasted-token rungs always target api.github.com

2 participants