Skip to content

Report credential storage failures during token refresh - #15178

Merged
babakks merged 3 commits into
trunkfrom
babakks/refresh-storage-errors
Dec 1, 2026
Merged

babakks merged 3 commits into
trunkfrom
babakks/refresh-storage-errors

Conversation

@babakks

@babakks babakks commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Description

TL;DR: This makes credential storage failures during a token refresh visible, without otherwise changing behaviour. When gh cannot store a renewed credential or remove a rejected one, it now prints a warning on stderr and records a new storage_error telemetry dimension, and failed removals are reported as rejected_remove_failed instead of rejected_removed. The same records are written and deleted, and the refresh returns the same credential, status and error, so exit codes and stdout are unchanged. The only runtime change is one extra keyring read, which runs only after a rejected refresh when the delete of a keyring credential reports success, to confirm the credential is really gone.

With expiring tokens, gh renews the access token when it is about to expire and writes the renewed credential back to where it was stored: the system keyring, or hosts.yml when insecure storage is used. The server rotates the refresh token on every renewal, so the old one stops working as soon as the exchange succeeds. When the server rejects a refresh token, gh removes the stored credential so later runs stop presenting it, and the user is asked to sign in again.

Two failures on this path were invisible to the user:

  • Storing the renewed credential fails. The refresh token has already been rotated, so the stored credential can no longer be refreshed. gh returned an error, but every caller of the refreshing token methods drops it, so nothing reached stderr.
  • Removing a rejected credential fails. The removal ignored keyring delete errors, so a keyring that refused the delete left the rejected credential in place, and every later run presented it again. The rejected_removed telemetry outcome was reported even then. A Secret Service delete whose prompt is dismissed also reports success without deleting anything (Secret Service: dismissed prompts are not treated as errors zalando/go-keyring#161).

This PR:

  • adds a warning for each failure, naming the storage error and pointing to gh auth login, with a hint that a sandbox may be blocking writes to the system keyring or gh's configuration directory. Warnings go through AuthConfig.Warnings(), the same path as the existing "skipped refresh" warning;
  • makes the removal report failures accurately. For a credential read from the keyring, the delete error is trusted unless the secret was not found, and a successful delete is verified by re-reading. Only a read that finds no secret confirms the removal; any other read error, including a timeout, counts as a failed removal. Keyring errors stay ignored for plaintext credentials and for the non-expiring slot, as before;
  • adds a storage_error dimension (none, error or timeout) to the auth_token_refresh telemetry event, and reports rejected_remove_failed whenever the removal fails;
  • makes internal/keyring.Delete return the package's own ErrNotFound for a missing secret, matching Get, so the removal can tell "already gone" apart from a failed delete.

How did you test this change?

Given a Linux desktop with gnome-keyring serving the Secret Service API
When I called internal/keyring.Delete for a service name with no stored secret
Then go-keyring returned its ErrNotFound, and the wrapper returned the package's ErrNotFound (errors.Is was true)

The warnings and the removal checks were not exercised by hand.

Key points

  • Behaviour is otherwise unchanged. The same records are written and deleted, and the refresh returns the same credential, status and error, so exit codes and stdout stay the same. What's new:
    • a warning on stderr, only when storing the renewed credential or removing a rejected one fails;
    • the storage_error telemetry dimension, and rejected_remove_failed reported in cases that used to show up as rejected_removed;
    • one extra keyring read. After a rejected refresh, when the credential was in the keyring and the delete reported success, gh reads the secret once more to check it is really gone. This only runs on the rejection path, but on a misbehaving keyring that read could prompt or wait for the keyring wrapper's 60 second timeout.
  • The removal still returns no error to the refresh. It is cleanup on a path that has already failed: the refresh reports the rejection either way, and the user has to sign in again whether or not the removal succeeds. A removal failure only changes telemetry and adds a warning.
  • Keyring errors are only trusted where they mean something. Keyring delete errors were ignored because they are mostly noise: the removal deletes four records when at most one exists, and on machines without a keyring every call fails. Errors are now checked only for the keyring record the credential was just read from, where neither applies. A timeout there is reported without re-reading, since the read would most likely hang too.
  • storage_error carries no error text. Exit codes or D-Bus error names could reveal which credential store is in use, so the dimension only records none, error or timeout.
  • internal/keyring.Delete is not a breaking change. The package is internal, every existing caller ignores the Delete error, and nothing compared against go-keyring's own sentinel. Success, timeouts and other errors are returned unchanged.

Notes for reviewers

Start with removeCredentialForUser and removeRejectedCredential in internal/config/refreshable.go, then the persist failure branch in refreshCredentialForUser. The table cases in refreshable_telemetry_test.go cover each outcome, its storage_error value and the warning.

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:

  • @babakks 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.

babakks and others added 2 commits November 27, 2026 11:31
Delete passed go-keyring's own not-found sentinel through, so callers
could not match a missing secret against the package's ErrNotFound.
Map it the same way Get already does. Existing callers ignore the
Delete error, so their behavior is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
When a refresh could not store the renewed credential, or could not
remove a credential whose refresh token the server rejected, the user
got no indication of why gh kept failing, because callers of the
refreshing token methods drop the returned error.

Surface both failures as warnings that name the storage error, point
to `gh auth login`, and mention sandboxes that may block writes to the
system keyring or gh's configuration directory.

Make the removal report failures accurately. For a credential read
from the keyring, trust the delete error unless the secret was not
found, and re-read after a successful delete, since a dismissed Secret
Service prompt reports success without deleting anything. Keyring
errors stay ignored for plaintext credentials and for the non-expiring
slot, where a missing record or an unavailable keyring makes them
meaningless.

Add a storage_error dimension (none, error or timeout) to the
auth_token_refresh event, without the underlying error text, and
report rejected_remove_failed whenever the removal fails.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
@babakks
babakks requested a review from a team as a code owner November 27, 2026 11:44
@babakks
babakks requested review from BagToad and tidy-dev and a balanced review from Copilot November 27, 2026 11:44

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

Post-delete verification errors and timeouts are silently reported as successful removal, while configuration-write failures can prompt unnecessary reauthentication despite a saved rotated token.

2 open issues
What changed in this PR

This PR makes credential-storage failures visible during GitHub CLI token refresh, helping users recover when renewed credentials cannot be saved or rejected credentials cannot be removed.

Changes:

  • Adds warnings for failed credential persistence and removal.
  • Checks keyring deletion results and normalizes missing-secret errors.
  • Adds storage-error telemetry and tests for failure outcomes and warnings.
File Description
internal/​keyring/​keyring.go Normalizes missing-secret delete errors to the internal ErrNotFound sentinel.
internal/​config/​refreshable.go Adds storage warnings and checks removal of rejected credentials.
internal/​config/​refreshable_telemetry.go Classifies storage errors and records them in refresh telemetry.
internal/​config/​refreshable_telemetry_test.go Covers storage failures, removal outcomes, and warning emission.

Review effort: Balanced

Comment thread internal/config/refreshable.go Resolved
Comment thread internal/config/refreshable.go Resolved
After a keyring delete reports success, the removal of a rejected credential is verified by reading the secret back. A read that failed for any reason other than a missing record was treated as a confirmed removal, so a locked or hung keyring could hide a credential that survived. Only a missing record now confirms the removal; other read errors, including timeouts, are reported as a failed removal with a warning.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
adamstallard added a commit to adamstallard/shared-skills that referenced this pull request Nov 29, 2026
GitHub records a reply in an existing review thread as a review of the
newest commit, so a reviewer's "thanks" after a push ended their wait.
Only an approval, a change request, or a review with a body or a new
comment thread now ends one. This answers design.md's open question and adds
the measurement on cli/cli#15178 to the requirement's Rationale entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bug-hunter: skipped at triage (spec only, no code)
Prose: ✓ 768567e:72b366f38a15; cold read: 9 findings, 9 fixed, 0 kept; understood: 3 items, 0 fixed, 0 kept
Spec-check: ✓ 768567e 0 errors, 4 checks; validate passed; read deferred to the round's end
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@tidy-dev tidy-dev 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.

Yay for errors!

@babakks
babakks merged commit b477146 into trunk Dec 1, 2026
25 checks passed
@babakks
babakks deleted the babakks/refresh-storage-errors branch December 1, 2026 14:45
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