Skip to content

fix: report actual error when auth status token check fails for non-401 - #14054

Open
MumuTW wants to merge 1 commit into
cli:trunkfrom
MumuTW:fix/auth-status-non401-error-details
Open

fix: report actual error when auth status token check fails for non-401#14054
MumuTW wants to merge 1 commit into
cli:trunkfrom
MumuTW:fix/auth-status-non401-error-details

Conversation

@MumuTW

@MumuTW MumuTW commented Aug 3, 2026

Copy link
Copy Markdown

Summary

gh auth status currently renders "The token in <source> is invalid." for any token-verification failure: 401, 403 rate limit, 5xx, and network/DNS errors all produce the same misleading message plus a "To re-authenticate, run: gh auth refresh" hint that only makes sense for genuinely invalid or revoked tokens. Refreshing while rate limited does not help, and the message actively misleads users (and agents reading the output) into thinking their token is dead.

This PR records the HTTP status code of the verification request and only reports the token as invalid for 401 Unauthorized. For every other failure it surfaces the actual error instead, and the re-auth/logout hints are only shown for genuine auth failures.

This follows existing precedent in the codebase rather than introducing a new mechanism:

Real-world trigger: an agent daemon sharing the same token exhausted the account's API quota, and gh auth status kept reporting the token as invalid with a useless refresh hint - the token itself was fine and worked again after the rate limit reset. The same misleading output appears for sandboxed/network-restricted environments (DNS failures, blocked connections), as documented in #12891.

Changes

  • pkg/cmd/auth/status/status.go
    • authEntry gains a StatusCode field (exposed in --json output as statusCode, omitted when unset).
    • buildEntry records the status code from api.HTTPError when the scopes request fails.
    • String() now renders:
      • 401 - unchanged: "The token in <source> is invalid." plus re-auth/logout hints.
      • anything else - "Could not verify token in <source>: <actual error>" with no re-auth hint.
  • pkg/cmd/auth/status/status_test.go
    • "bad token" cases now stub 401 (the real status for bad credentials) instead of 400.
    • New "rate limited" case stubs a 403 rate-limit response and asserts the new message and the absence of the re-auth hint.
    • JSON case asserts the new statusCode field.

Known limitations (deliberately out of scope)

  • Env-token path: tokens from GH_TOKEN/GH_ENTERPRISE_TOKEN are validated via a GraphQL call (CurrentLoginName) whose errors do not carry an HTTP status code, so the statusCode-based discrimination does not apply there; a genuinely invalid env token reports "Could not verify token" with the underlying error rather than "invalid". Fixing this requires touching the GraphQL client's error surface.
  • SSO-enforced 403s (403 + X-GitHub-SSO header) keep their existing handling; that flow is managed separately in internal/ghcmd via the SSO URL mechanism.

Test plan

  • go test ./pkg/cmd/auth/status/... - all pass.
  • go build ./... - clean.

Related

@MumuTW
MumuTW requested a review from a team as a code owner August 3, 2026 17:16
@MumuTW
MumuTW requested a review from tidy-dev August 3, 2026 17:16
@github-actions github-actions Bot added external pull request originating outside of the CLI core team needs-triage needs to be reviewed labels Aug 3, 2026
@MumuTW MumuTW changed the title auth status: distinguish invalid tokens (401) from other verification failures fix: report actual error when auth status token check fails for non-401 Aug 3, 2026
gh auth status rendered "The token in <source> is invalid." for any
token-verification failure: 401, 403 rate limit, 5xx, and network errors
all produced the same misleading message plus a re-auth hint that only
makes sense for genuinely invalid tokens.

Record the HTTP status code of the verification request and only report
the token as invalid for 401. For other failures, surface the actual
error instead and drop the re-auth hint, since refreshing does not help
with e.g. rate limits.

Fixes cli#12891
@MumuTW
MumuTW force-pushed the fix/auth-status-non401-error-details branch from 17bfbf3 to d12cd2b Compare August 3, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team needs-triage needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh auth status should distinguish transport failures from invalid credentials

1 participant