Repository navigation
Closed
Labels
bugSomething isn't workinghelp wantedContributions welcomeneeds-designAn engineering task needs design to proceedpriority-3Affects a small number of users or is largely cosmetic
Description
Describe the bug
Running gh auth status reports account as active, but API calls use another account.
> gh --version
gh version 2.63.0 (1980-01-01)
https://github.com/cli/cli/releases/tag/v2.63.0Steps to reproduce the behavior
- You have two users on same hostname
github.com, last active account isuser1. - Switch to another folder with custom
GH_CONFIG_DIR(via direnv), with a custom config which has one useruser2 - Run
gh auth status
> gh auth status
github.com
✓ Logged in to github.com account user2 (keyring)
- Active account: true
- Git operations protocol: ssh
- Token: gho_************************************
- Token scopes: 'gist', 'read:org', 'repo', 'workflow'- Run
gh api /user | jq .name=> printsuser1, contradictingauth statusoutput
The expected behavior is to use the token for the given host+user of current repository without needing to gh auth switch, and report active account correctly.
Context
Active token is fetched from keyring using only hostname
Lines 202 to 218 in 5402e20
| // ActiveToken will retrieve the active auth token for the given hostname, | |
| // searching environment variables, plain text config, and | |
| // lastly encrypted storage. | |
| func (c *AuthConfig) ActiveToken(hostname string) (string, string) { | |
| if c.tokenOverride != nil { | |
| return c.tokenOverride(hostname) | |
| } | |
| token, source := ghauth.TokenFromEnvOrConfig(hostname) | |
| if token == "" { | |
| var err error | |
| token, err = c.TokenFromKeyring(hostname) | |
| if err == nil { | |
| source = "keyring" | |
| } | |
| } | |
| return token, source | |
| } |
Related to #9111 (comment))
Metadata
Metadata
Assignees
Labels
bugSomething isn't workinghelp wantedContributions welcomeneeds-designAn engineering task needs design to proceedpriority-3Affects a small number of users or is largely cosmetic
Activity
med8bra commentedon Dec 23, 2024
Hello @williammartin, for your question about using
GH_CONFIG_DIRwith direnv, I prefer separating accounts config and I like gitincludeIfpattern.For
ghI had to use direnv to achieve this, and also I prefergh auth statusto only report one account, which is the one I'm expecting in my filesystem context.jtmcg commentedon Dec 26, 2024
Interesting issue... I have been able to repro this, and it looks like it actually might be an issue with how
GH_CONFIG_DIRis working alongsidegh api... It seems thatgh apiis not respecting the custom config pointed to by theGH_CONFIG_DIRenv var. I suspect that it's defaulting to the global config before falling back to the specified config.Repro steps
mkdir .configGH_CONFIG_DIR=./.config gh auth loginGH_CONFIG_DIR=./.config gh auth status. Alternatively, you can runcat ./.config/hosts.ymlGH_CONFIG_DIR=./.config gh api /user | jq .login. This should show the same account you are logged in withgh auth loginGH_CONFIG_DIR=./.config api /user | jq .login. This will show the account you've logged into globally and not the one logged into with the specified config.I'm not entirely sure what's going on, yet, but I am concerned that this might not be scoped only to
gh api. Thanks for pointing this out! We'll discuss and get back to you.williammartin commentedon Dec 27, 2024
@jtmcg this is a bit of a funny scenario that has fallen between the Triage rotation cracks. I probably could have been clearer in #9111 (comment) but I'm pretty confident the issue at hand is a disconnect between the way the Active User is determined, and the Active Token is fetched.
I'll explain those now.
Active User
This is the user referenced by the
userkey under a particular host in thehosts.ymlActive Token
This is, in order:
oauth_tokenkey under a particular host in thehosts.yml(legacy, insecure)So in this case, we're dealing with a changing hosts.yml containing the same host (github.com), but a keyring value that is keyed by a particular host.
So when the active token is fetched, it returns whatever token happened to be put in there last.
The reason
auth switchresolves this is because auth switching was implemented by also keying tokens in the keyring byhostandusername, then swapping the token into thehostkeyed entry to make it active. This was for backwards compatibility reasons.I have some thoughts on how we might resolve this but I'm on vacation and just wanted to drop this message so that you don't wild goose chase.
williammartin commentedon Dec 27, 2024
@jtmcg this is a bit of a funny scenario that has fallen between the Triage rotation cracks. I probably could have been clearer in #9111 (comment) but I'm pretty confident the issue at hand is a disconnect between the way the Active User is determined, and the Active Token is fetched.
I'll explain those now.
Active User
This is the user referenced by the
userkey under a particular host in thehosts.ymlActive Token
This is, in order:
oauth_tokenkey under a particular host in thehosts.yml(legacy, insecure)So in this case, we're dealing with a changing hosts.yml containing the same host (github.com), but a keyring value that is keyed by a particular host. When the active token is fetched, it returns whatever token happened to be put in there last.
The reason
auth switchresolves this is because auth switching was implemented by also keying tokens in the keyring byhostandusername, then swapping the token into thehostkeyed entry to make it active. This was for backwards compatibility reasons.I have some thoughts on how we might resolve this but I'm on vacation and just wanted to drop this message so that you don't wild goose chase.
jtmcg commentedon Dec 27, 2024
Ah, this is my bad. I totally missed the linked issue #9111 😅 This did, at least, give me a chance to explore this a bit and I can confirm the behavior! lol
kyoh86 commentedon Apr 15, 2025
I am hoping this issue will be resolved.
I want to switch the active account of
ghfor each directory.For only my use case, I don't necessarily want to support the
GH_CONFIG_DIRswitch by direnv, since I only need to be able to specify the active account ofghper directory.hosts.yml#11009 as a duplicate of this issuewilliammartin commentedon Jun 17, 2025
Acceptance Criteria
Given I have two accounts on a single github host
And Given I have each of these accounts as the active user in separate
hosts.ymlfilesWhen I set
GH_CONFIG_DIRto either of the filesAnd When I run
gh api /user .loginThen I see the active user is correct as per the hosts.yml file
Shadow-web-pixel commentedon Nov 10, 2025
feat(identity): 계정 라우팅을 라벨이 아니라 실체로 검증 (L2 관측) (#188)
fix(identity): 게이트 오탐 2건 — 대소문자 비교 · probe 가 주변 환경을 보던 문제 (#189)