fix: report actual error when auth status token check fails for non-401 - #14054
Open
MumuTW wants to merge 1 commit into
Open
fix: report actual error when auth status token check fails for non-401#14054MumuTW wants to merge 1 commit into
MumuTW wants to merge 1 commit into
Conversation
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
force-pushed
the
fix/auth-status-non401-error-details
branch
from
August 3, 2026 17:26
17bfbf3 to
d12cd2b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
gh auth statuscurrently 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:
internal/ghcmd/cmd.goalready special-caseserrors.As(err, &api.HTTPError) && httpErr.StatusCode == 401for the "Try authenticating with" hint (Printgh auth refreshfor 401 returns #13068).Real-world trigger: an agent daemon sharing the same token exhausted the account's API quota, and
gh auth statuskept 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.goauthEntrygains aStatusCodefield (exposed in--jsonoutput asstatusCode, omitted when unset).buildEntryrecords the status code fromapi.HTTPErrorwhen the scopes request fails.String()now renders:401- unchanged: "The token in<source>is invalid." plus re-auth/logout hints.<source>:<actual error>" with no re-auth hint.pkg/cmd/auth/status/status_test.go401(the real status for bad credentials) instead of400.403rate-limit response and asserts the new message and the absence of the re-auth hint.statusCodefield.Known limitations (deliberately out of scope)
GH_TOKEN/GH_ENTERPRISE_TOKENare 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.X-GitHub-SSOheader) keep their existing handling; that flow is managed separately ininternal/ghcmdvia the SSO URL mechanism.Test plan
go test ./pkg/cmd/auth/status/...- all pass.go build ./...- clean.Related
gh auth statusreports rate limit / network failures as "token is invalid" with misleading re-auth advice #14053 (reported rate-limit misreporting; triaged as a duplicate of gh auth status should distinguish transport failures from invalid credentials #12891)