Skip to content

Revoke CLI OAuth tokens on logout - #15100

Merged
BagToad merged 7 commits into
trunkfrom
bagtoad/oauth-logout-revocation
Nov 24, 2026
Merged

BagToad merged 7 commits into
trunkfrom
bagtoad/oauth-logout-revocation

Conversation

@BagToad

@BagToad BagToad commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Related to #13111 and #9233.

Description

gh auth logout removes 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-revoke skips 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 ghmock API responses with an in-memory keyring. No live credentials were used for those runs.

Before:

$ gh auth logout --hostname github.com --user monalisa
✓ Logged out of github.com account monalisa
✓ Switched active account for github.com to hubot

After a mocked 204:

$ gh auth logout --hostname github.com --user monalisa
✓ Revoked GitHub CLI OAuth token for monalisa (github.com)
✓ Logged out of github.com account monalisa
✓ Switched active account for github.com to hubot

I checked that only the selected token was submitted and the other account remained. The baseline made no API request.

  • A mocked 404 returned exit 1 and left the saved credentials byte-for-byte unchanged.
  • --no-revoke returned exit 0 with the generic warning and no API request.
  • A PAT returned exit 0 with the PAT 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

  • The new default applies to scripts too. Copies of the revoked token stop working. Independently issued tokens are unaffected.
  • Only the CLI app's targeted endpoint is used. A gho_ prefix does not prove its issuer, and 404 does not confirm revocation.
  • OAuth revocation requires the refresh lock to protect token selection through local removal. Other token types keep the existing best-effort locking behavior.
  • Logout does not refresh tokens. Login, refresh, and external Git credential-helper cleanup are unchanged.

Notes for reviewers

Start with the logout flow, 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.

go fix ./internal/authflow ./internal/config ./internal/gh ./pkg/cmd/auth/logout ./pkg/cmd/factory ./pkg/cmdutil
# Passed.
go test ./...
# Passed.
go test -race ./internal/authflow ./internal/config ./pkg/cmd/auth/logout ./pkg/cmd/factory
# Passed.
make lint
# Passed, 0 issues.

Authorship and follow-up

Who wrote this:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @BagToad will read and reply directly.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

@BagToad
BagToad requested a review from a team as a code owner November 18, 2026 23:08
@BagToad
BagToad requested review from niik and a balanced review from Copilot November 18, 2026 23:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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-revoke and 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

Comment thread pkg/cmd/auth/logout/logout.go Resolved

@babakks babakks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/cmd/auth/logout/logout.go Resolved
Comment thread pkg/cmd/auth/logout/logout.go Resolved
Comment thread internal/config/config.go Resolved
}
body, err := json.Marshal(struct {
AccessToken string `json:"access_token"`
}{AccessToken: token})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread internal/authflow/revoke.go Resolved
@babakks
babakks removed the request for review from niik November 19, 2026 18:24
@BagToad
BagToad requested a review from babakks November 19, 2026 19:01

@babakks babakks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for applying the changes, @BagToad! 🙏

Looks good to me now.

@BagToad
BagToad merged commit d298dbd into trunk Nov 24, 2026
10 of 11 checks passed
@BagToad
BagToad deleted the bagtoad/oauth-logout-revocation branch November 24, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants