Repository navigation
Stop presenting a rejected refresh token - #328
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dbf9903. Configure here.
yummybomb
left a comment
There was a problem hiding this comment.
no blockers, approving. go test -race ./pkg/auth passes on 52db437.
non-blocking (5m -> 1m refresh window):
pkg/auth/storage.go:42— shorteningrefreshBeforefrom 5m to 1m drops the minimum remaining token lifetime at command start from 5m to 1m. becausecmd/root.go:150callsGetAuthenticatedClientonce with a staticAuthorizationheader, multi-request commands likekernel audit-logs downloadare more likely to hit a 401 on a later chunk (retryableAuditLogsChunkErrordoes not retry 401s, and the deferred cleanup deletes the partial file). this can already happen onmainwhen a run outlives the token, so it is fine as a follow-up, but worth refreshing per request in middleware instead of relying on the startup buffer.
|
@yummybomb thanks. The refresh window is back to 5 minutes in 17aa56f ( |

Summary
Rejected refresh tokens are cleared. When the auth server answers a refresh with
400 invalid_grant, the token is revoked or expired for good. Today the CLI keeps it, so every later command presents it again and fails the same way.RefreshTokensnow returnserrRefreshTokenRejectedfor exactly that response. While still holding the credentials lock,refreshStoredTokensremoves the refresh token from the saved credentials. It does this only when the saved token is still the one that was rejected, so credentials another process saved in the meantime are left alone. With an expired access token and no refresh token,GetAuthenticatedClientandrefreshStoredTokensnow returnsession ended, please run 'kernel login' to sign in againwithout contacting the server. Previously the CLI sent the expired access token to the API. This also covers a command that was waiting on the lock while another cleared the token.kernel loginalready accepts expired credentials without--force.Other failures still keep the refresh token and are retried by the next command: network errors, 5xx responses,
401, and other400codes.The early-refresh window stays at 5 minutes. The CLI sets the access token once per command, so a shorter window would let long multi-request commands (for example
kernel audit-logs download) start with less token lifetime left. Refreshing per request is a separate follow-up.Tests
TestRejectedRefreshTokenIsNotPresentedAgain: aninvalid_grantclears the refresh token and keeps the rest of the credentials. Three later commands returnerrSessionEnded(with "kernel login" in the message), and the server sees exactly one request.TestConcurrentCommandsStopAfterRejectionandTestRefreshAfterTheTokenWasClearedEndsTheSession: commands waiting on the lock stop instead of using the expired access token.TestFailedRefreshKeepsTheRefreshToken: 500, 503,400 invalid_requestand401 invalid_clientkeep the token, and the next command retries.TestForgetRefreshTokenKeepsReplacedCredentials: a rejection doesn't clear credentials that were already replaced.TestIsExpiredRefreshesFiveMinutesEarly.go vet ./...passes (and on Windows forpkg/authandcmd), as dogo test ./...andgo test -race ./pkg/auth.Note
Medium Risk
Changes authentication and persisted credential handling; mistakes could log users out incorrectly or leave bad tokens in use, but scope is limited to CLI OAuth refresh paths and is well covered by new tests.
Overview
When OAuth refresh fails with
400 invalid_grant,RefreshTokensnow surfaceserrRefreshTokenRejected. While holding the credentials lock,refreshStoredTokenscallsforgetRefreshTokento wipe only that refresh token from storage (other fields and credentials replaced by another process stay intact). Transient failures (5xx, other 4xx, network) still leave the refresh token in place for the next attempt.If the access token is expired and there is no refresh token,
GetAuthenticatedClientandrefreshStoredTokensreturnerrSessionEndedwith a prompt to runkernel login, instead of sending an expired access token or hitting the token endpoint again.A new
pkg/auth/client_test.gocovers rejection clearing, concurrent CLI processes on the credentials lock, retry vs non-retry failures, andforgetRefreshTokenbehavior.Reviewed by Cursor Bugbot for commit 17aa56f. Bugbot is set up for automated code reviews on this repo. Configure here.