Repository navigation
Revoke CLI OAuth tokens on logout - #15100
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Scripted logout now revokes tokens shared elsewhere and fails without removing local credentials when revocation is unavailable, leaving a breaking compatibility change unresolved without explicit maintainer agreement.
1 open issue
What changed in this PR
This PR adds targeted OAuth token revocation to gh auth logout, addressing the logout portion of #13111 without changing login or token replacement flows.
Changes:
- Revoke the selected GitHub CLI OAuth token before removing local credentials; retain credentials if revocation fails or cannot be confirmed.
- Add
--no-revokeand warnings for tokens removed only locally. - Coordinate revocation and cleanup under the refresh lock, with a dedicated HTTP client that disables traffic logging.
| File | Description |
|---|---|
pkg/cmdutil/factory.go |
Exposes the OAuth app HTTP client dependency. |
pkg/cmd/factory/default.go |
Constructs the client without user authentication or traffic logging. |
pkg/cmd/factory/default_test.go |
Tests client headers and disabled logging. |
pkg/cmd/auth/logout/logout.go |
Adds revocation, local-only opt-out, warnings, and recovery guidance. |
pkg/cmd/auth/logout/logout_test.go |
Covers token types, failures, account selection, and local-only logout. |
internal/gh/gh.go |
Defines the credential-aware logout interface. |
internal/config/refreshable_test.go |
Tests locked credential selection and failure retention. |
internal/config/config.go |
Coordinates credential-dependent cleanup under the refresh lock. |
internal/authflow/revoke.go |
Implements targeted revocation with timeout and sanitized errors. |
internal/authflow/revoke_test.go |
Tests request construction, host routing, and response handling. |
Review effort: Balanced
babakks
left a comment
There was a problem hiding this comment.
Thanks for tackling this, @BagToad! 🙏
Before we merge, I'd like to confirm and document the refreshable-token behavior, particularly whether revocation also invalidates the refresh token and what happens when the access token has already expired. I'd also like to clarify the tradeoff behind requiring the refresh lock for every default logout. Extracting the revocation branch into a helper would make the main logout flow easier to follow.
| } | ||
| body, err := json.Marshal(struct { | ||
| AccessToken string `json:"access_token"` | ||
| }{AccessToken: token}) |
There was a problem hiding this comment.
For refreshable credentials, we're passing the stored access token here rather than the refresh token. Does this endpoint also invalidate the associated refresh token, preventing it from issuing another access token? What happens if the access token has already expired, given that logout deliberately does not refresh it first? Could we clarify these guarantees in the PR description?
There was a problem hiding this comment.
I tested this on GitHub.com and confirmed that revoking the access token also invalidated its paired refresh token. Refresh worked before revocation and returned bad_refresh_token afterward.
I haven't tested a naturally expired access token yet. That test is running, but GitHub's eight-hour lifetime means the check won't happen until about 8:45 PM Central today. I doubt it'll behave differently from an already-invalid token, but that still needs verification.
There was a problem hiding this comment.
I was wrong about the expired-token case. After the full eight hours on GitHub.com, access returned 401 and the targeted DELETE returned 404. The refresh token still issued a working access/refresh pair.
With the current implementation, gh auth logout would return an error and keep both credentials rather than revoke the refresh token. I revoked the new pair afterward and confirmed its refresh token was rejected.
There was a problem hiding this comment.
Fixed in bdcd69067. I ran 22 ghmock cases with synthetic credentials across 45 CLI executions.
The expired-token flow completed DELETE 404, refresh 200, then DELETE 204 and removed the local account. I also verified that a failed retry preserves the renewed pair and that a later logout revokes it without another refresh. Regression checks covered normal OAuth logout, other token types, --no-revoke, account selection, environment authentication, GHES, gateways, and lock failures.

Related to #13111 and #9233.
Description
gh auth logoutremoves local authentication without invalidating the saved token. This change revokes the selected GitHub CLI OAuth token first.Other token types are removed locally with a not-revoked warning.
--no-revokeskips revocation and still prints that warning. Failed or unconfirmed revocation keeps credentials and returns exit 1.How did you test this change?
I ran real before/after builds against synthetic
ghmockAPI responses with an in-memory keyring. No live credentials were used for those runs.Before:
After a mocked 204:
I checked that only the selected token was submitted and the other account remained. The baseline made no API request.
--no-revokereturned exit 0 with the generic warning and no API request.I separately exercised the targeted endpoint on GitHub.com with an expiring access/refresh pair. Refresh worked before deletion. After deletion, access returned 401 and the refresh token was rejected with
bad_refresh_token.The access token was still valid when deleted. Revocation after natural access-token expiry has not been verified yet.
Key points
gho_prefix does not prove its issuer, and 404 does not confirm revocation.Notes for reviewers
Start with the
logoutflow, then the targeted request and locked credential cleanup.#13111 includes the logout revocation request. #9233 discusses token replacement and compatibility; this PR does not change replacement flows.
Local validation
I used Go 1.27.4 and the repository-pinned golangci-lint v2.15.0 with isolated configuration.
Authorship and follow-up
Who wrote this:
Who answers review comments: