Skip to content

auth token --user can return stale keyring token after --insecure-storage refresh #14322

Open

Description

@cephalin

Description

When an isolated GH_CONFIG_DIR uses --insecure-storage, gh auth refresh writes the refreshed credential to that profile's hosts.yml. However, gh auth token --user <login> can return an older credential for the same login from the global OS keyring instead. Calling gh auth token without --user returns the freshly refreshed active credential.

This makes a successful scope refresh appear ineffective to callers that retrieve the token by username. In my case, only the active token had the newly requested codespace scope.

Steps to reproduce

  1. Have an existing secure keyring credential for a GitHub login.

  2. Create an isolated GH_CONFIG_DIR for the same login and authenticate it with --insecure-storage.

  3. With that login active in the isolated profile, run:

    gh auth refresh --hostname github.com --scopes codespace --insecure-storage
  4. Clear GH_TOKEN and GITHUB_TOKEN, then retrieve both credentials:

    gh auth token --hostname github.com
    gh auth token --hostname github.com --user <login>
  5. Compare hashes rather than printing the tokens, and inspect each token's scopes using gh api --include /user with the token supplied through GH_TOKEN.

Actual behavior

  • The two token hashes differ.
  • The token returned without --user is the refreshed token and includes codespace.
  • The token returned with --user is the stale keyring token and does not include codespace.
  • Both credentials belong to the same GitHub login.

Expected behavior

gh auth token --user <login> should return the credential just written for that login in the selected GH_CONFIG_DIR. An older global keyring entry should not shadow an explicitly selected profile's newer --insecure-storage credential.

Suspected cause

AuthConfig.Login(..., secureStorage=false) writes the per-user token under hosts.<host>.users.<login>.oauth_token but does not remove an existing keyring entry for that login. AuthConfig.TokenForUser checks the keyring before the profile config, so gh auth token --user returns the stale keyring value. By contrast, active-token resolution checks the profile config first.

The same precedence remains in current trunk. This is related to #12885, but that issue describes unqualified active-token lookup across multiple GH_CONFIG_DIR profiles rather than a refreshed profile token being shadowed during explicit --user lookup.

Environment

  • GitHub CLI: 2.74.2
  • OS: Windows
  • Credential setup: global Windows Credential Manager entry plus isolated GH_CONFIG_DIR using --insecure-storage

Activity

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

    @cli-triage

    Caution

    agentic threat detected
    Threat detection flagged this output in warn mode. Manual review is REQUIRED before any follow-up automation.

    Details

    Potential security threats were detected in the agent output.

    Review the workflow run logs for details.

    Thanks for the detailed report and root-cause analysis. I'm suggesting bug since this is a clear, reproducible issue with gh auth token --user returning a stale keyring credential instead of the refreshed --insecure-storage token for the same login, and priority-3 since it's a fairly narrow scenario (isolated GH_CONFIG_DIR + --insecure-storage + explicit --user lookup) with a workaround (retrieving the active token without --user). This looks related to but distinct from #12885 (which is about keychain lookups ignoring the account field for multi-account setups) rather than a duplicate.

    Generated by Issue Triage (skills-driven) for #14322 · copilot · auto · 8.86 AIC · ⌖ 4.33 AIC · ⊞ 9.7K · ◷

  3. added
    bug
    priority-3
    gh-auth
    on Sep 2, 2026
  4. removed
    needs-triage
    on Sep 2, 2026
  5. boyam01 commented on Sep 3, 2026

    @boyam01

    @cli/code-reviewers, I'd like to work on this if the scope is welcome.

    I traced the current lookup path on trunk at adda317c11b892da5e5ed3fbbf26b69a5c163cc6. An insecure refresh stores the per-user token in the selected GH_CONFIG_DIR, while normal explicit-user lookup checks the global keyring first. I would avoid deleting the keyring entry because it may still be authoritative for another config profile.

    Proposed acceptance criteria:

    1. If the selected profile and keyring both contain credentials for the same host/user, gh auth token --user <login> returns the selected profile credential.
    2. If the selected profile has no such credential, lookup still falls back to the user's keyring credential.
    3. --secure-storage --user <login> remains keyring-only.
    4. Lookup without --user, including environment-token precedence, remains unchanged.
    5. Lookup does not delete or migrate either credential.
    6. Tests cover both-source precedence plus config-only, keyring-only, not-found, and --secure-storage controls.

    TokenForUser is also used by gh auth status for inactive accounts. Should the selected-profile precedence apply to both consumers through that shared method, or only to gh auth token --user?

    Could you confirm the intended contract or add help wanted if this is suitable for an external contribution? I will not open a PR before that gate is satisfied.

    AI assistance disclosure: I used AI tooling to inspect the current code path and draft this proposed scope. I will review and stand behind any implementation, tests, and follow-up before submission.

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