Skip to content

gh auth token --secure-storage ignores the config's active account and returns the globally-active keyring token #14370

Open

Description

@idrismmc

Describe the bug

With two accounts on github.com kept in separate GH_CONFIG_DIRs, gh auth token returns the right token for each config, but gh auth token --secure-storage returns the token of whichever account was activated last globally, ignoring the active user in the config directory it was pointed at.

--secure-storage is a hidden flag, so it isn't something most people call directly. It matters because the gh stack extension shells out to exactly this command, so every gh stack call authenticates as the wrong account in a GH_CONFIG_DIR setup.

gh 2.98.0, also present on main as of v2.100.0. Linux (Fedora 44), keyring via libsecret.

Steps to reproduce

Two accounts logged in on the same host, each in its own config dir, tokens in the keyring (no oauth_token in either hosts.yml):

gh auth login                                        # user-a -> ~/.config/gh
GH_CONFIG_DIR=~/.config/gh-b gh auth login           # user-b -> ~/.config/gh-b

Each hosts.yml correctly records its own active user. The keyring holds three entries under service gh:github.com: user-a, user-b, and one with an empty username.

export GH_CONFIG_DIR=~/.config/gh-b

gh auth status
#   Logged in to github.com account user-b (keyring)
#   Active account: true                                    <- correct

gh auth token
#   <user-b's token>                                        <- correct

gh auth token --secure-storage --hostname github.com
#   <user-a's token>                                        <- wrong

gh auth token --secure-storage --hostname github.com --user user-b
#   <user-b's token>                                        <- correct

I identified each token by SHA-256 fingerprint against the keyring entries, and on the wire with GH_DEBUG=api, where the X-OAuth-Scopes response header differs between the two accounts.

Expected vs actual

expected actual
gh auth token active user of the config dir correct
gh auth token --secure-storage active user of the config dir last globally activated user
gh auth token --secure-storage --user X user X correct

Cause

tokenRun resolves the active user in the non-secure branch but not in the secure one, token.go#L72-L84:

if opts.SecureStorage {
    if opts.Username == "" {
        val, _ = authCfg.TokenFromKeyring(hostname)          // no active-user resolution
    } else {
        val, _ = authCfg.TokenFromKeyringForUser(hostname, opts.Username)
    }
} else {
    if opts.Username == "" {
        val, _ = authCfg.ActiveToken(hostname)               // resolves active user
    } else {
        val, _, _ = authCfg.TokenForUser(hostname, opts.Username)
    }
}

TokenFromKeyring reads the unkeyed keyring slot, config.go#L299:

func (c *AuthConfig) TokenFromKeyring(hostname string) (string, error) {
	return keyring.Get(keyringServiceName(hostname), "")
}

That slot is written by activateUser (config.go#L460-L472) on every gh auth login and gh auth switch. The OS keyring is a single store and isn't partitioned by GH_CONFIG_DIR, so that slot holds whichever account was activated last by any config dir, while each hosts.yml keeps its own user:. The two desync as soon as a second config dir is used.

ActiveToken gets this right: it resolves ActiveUser(hostname) from the config-scoped hosts.yml, calls TokenFromKeyringForUser, and treats the unkeyed slot only as a legacy fallback. TokenFromKeyringForUser also rejects a blank username, with a comment about the risk of returning the active token in surprising cases.

Possible fix

Mirror what ActiveToken does, keeping the unkeyed slot as the fallback:

if opts.SecureStorage {
    username := opts.Username
    if username == "" {
        username, _ = authCfg.ActiveUser(hostname)
    }
    if username == "" {
        val, _ = authCfg.TokenFromKeyring(hostname)
    } else if val, _ = authCfg.TokenFromKeyringForUser(hostname, username); val == "" {
        // Fall back to the legacy unkeyed slot, as ActiveToken does.
        val, _ = authCfg.TokenFromKeyring(hostname)
    }
}

I can put up a PR with tests if this looks like the right shape.

Related issues

#12885 reports the non-secure path returning the wrong token under GH_CONFIG_DIR. That path was fixed in #11038 and behaves correctly for me on 2.98.0; this is the sibling branch that fix didn't cover. Also worth noting that on Linux/libsecret the unkeyed entry is a real distinct entry, so the wrong result is deterministic rather than ordering-dependent as described for macOS in #12885.

Downstream effect

The gh stack extension resolves auth through go-gh, which reads only env vars and oauth_token in hosts.yml. With secure storage that finds nothing, so it shells out to gh auth token --secure-storage --hostname github.com (confirmed with a PATH shim; GH_CONFIG_DIR is inherited correctly) and gets the wrong token. The stacks endpoint 404s and the extension prints "Stacked PRs are not enabled for this repository", see github/gh-stack#394. In a GH_CONFIG_DIR setup gh auth status shows the correct account and gh auth switch isn't the fix, so that error is misleading here.

Activity

  1. added
    needs-triage
    on Sep 6, 2026
  2. cli-triage commented on Sep 6, 2026

    @cli-triage

    Thanks for the detailed report and root-cause analysis. I traced this through the current source and can confirm the code matches what's described: in pkg/cmd/auth/token/token.go, the --secure-storage branch calls authCfg.TokenFromKeyring(hostname) when no --user is given, which reads the unkeyed keyring slot (internal/config/config.go#L299-L301) — a slot that's global across GH_CONFIG_DIRs and gets overwritten on every gh auth login/switch. The non-secure branch instead resolves the active user first via ActiveUser before reading the keyring, which is config-dir-scoped (internal/config/config.go#L317-L321). So the hypothesis is well supported by the code: the secure branch is missing the same active-user resolution.

    I'm suggesting bug and gh-auth labels, plus priority-3 since this only affects a hidden flag reachable directly by advanced users or indirectly through extensions like gh-stack, and a workaround (--user) already exists. I didn't find an exact duplicate — this is related to but distinct from #12885 (fixed by #11038, which covered the non-secure path) and #14322 (a different staleness issue in the --user path after --insecure-storage refresh).

    Generated by Issue Triage (skills-driven) for #14370 · copilot · auto · 36.4 AIC · ⌖ 1.27 AIC · ⊞ 11.2K · ◷

  3. idrismmc commented on Sep 6, 2026

    @idrismmc
    Author

    One note on the priority reasoning: the --user workaround only applies if you are calling gh auth token yourself. In the extension case there is no way to reach that call site. gh stack runs gh auth token --secure-storage --hostname github.com internally and nothing in its interface lets you influence it.

    What is left for an extension user is wrapping gh on PATH to export a matching GH_TOKEN, which is what I ended up doing. That cannot be applied globally either, since gh auth login, switch and logout refuse to run while GH_TOKEN is set, so the wrapper has to scope it to a specific list of subcommands.

    Not arguing for a particular priority, just flagging that the workaround is less available than it looks for the path most people will hit this through.

  4. added
    bug
    priority-3
    gh-auth
    on Sep 10, 2026
  5. removed
    needs-triage
    on Sep 10, 2026
  6. 5h4d0wn1k commented on Sep 23, 2026

    @5h4d0wn1k

    "I independently verified this against current trunk (a3d31e2, 2026-09-22) and it is still unfixed: the --secure-storage branch at pkg/cmd/auth/token/token.go:72-84 still calls TokenFromKeyring(hostname) (the unkeyed, config-dir-global keyring slot) without resolving the active user, unlike the non-secure branch which goes through ActiveToken -> ActiveUser. PR #14430 (merged) deletes the multi-account migration but does not touch this path, so the bug is live on HEAD.

    Proposed minimal fix (mirrors how ActiveToken resolves the active user, then falls back):

    • secure + no --user -> resolve ActiveUser(hostname), read TokenFromKeyringForUser(hostname, user); fall back to the legacy unkeyed TokenFromKeyring(hostname) only on a miss.

    Risk is low: single-account installs write the same token to both the per-user and unkeyed slots, so output changes only in the desync case described here. A regression test is straightforward using NewIsolatedTestConfig with keyring.MockInit (two configs sharing a mocked keyring).

    Per CONTRIBUTING.md, external PRs are only accepted for issues carrying help wanted + explicit Acceptance Criteria. Could a maintainer add those to this issue? @cli/code-reviewers

    (Note: this analysis was prepared with AI assistance)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggh-authpriority-3stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions