Repository navigation
Description
Bug description
The embedded auth server rotates a client's refresh token and deletes the old one at once. There is no grace period. When one client keeps several sessions that hold the same stored refresh token (for example several windows of one coding agent), the first session to refresh wins. The other sessions then present a token that no longer exists, get invalid_grant, and have to sign in again.
This is about the refresh grant that clients send to the ToolHive auth server. It is not the upstream IdP refresh covered by #4122 and #7031.
Where it happens in the code
Checked at v0.64.0 (latest release) and on main.
RedisStorage.RotateRefreshTokeninpkg/authserver/storage/redis.gocallsDelon the old refresh token key. It also deletes every access token stored under the same request ID, so the other sessions lose their access token too.RedisStorage.RevokeRefreshTokenMaybeGracePeriodhas the comment "optionally allowing a grace period. For this implementation, we revoke immediately." and callsRevokeRefreshToken.- The in-memory storage behaves the same way.
- fosite returns "The refresh token can not be found" when storage returns
ErrNotFound(handler/oauth2/flow_refresh.go). So a grace period has to be built in the storage layer. fosite does not provide one.
Steps to reproduce
I have seen this in production logs. I do not have a deterministic reproduction yet. The expected steps are:
- Authorize a client against the embedded auth server and store the refresh token in a place that two processes can read.
- Start two sessions that both use that token.
- Send a refresh grant from session A, then from session B.
Expected behavior
Session B still succeeds when it refreshes shortly after session A. Hydra solves this with rotation_grace_period. Auth0 has a "reuse interval". RFC 9700 section 4.14.2 allows servers to tolerate a short reuse window.
Actual behavior
Session B gets invalid_grant with the hint "The refresh token can not be found". In our logs, the same failure appears at a low steady rate after a restart of the auth server pods.
Proposal
Add an optional grace period for a rotated refresh token.
- In
RotateRefreshToken, do not delete the old token. Give it a short TTL (for example 30 to 60 seconds, configurable). Do not delete the sibling access tokens during that time. - While the TTL is running,
GetRefreshTokenSessionstill returns the old token. A new token is issued as usual. - Keep the default at 0 so current behavior does not change.
- Apply the same behavior to the in-memory storage, and add tests for two parallel refreshes.
- Keep replay detection for tokens used after the grace period ends.
This design can be reviewed against the compare-and-swap work in #6548. I am happy to send a PR if the approach is acceptable.
Environment
- ToolHive version: observed on v0.42.1; code unchanged in v0.64.0 and main
- Auth server storage: Redis (Dragonfly)
Additional context
The log field refresh_failure_classification is not enough to tell "not found" from "expired". A separate field would make it possible to measure the effect of a grace period.
Activity