Skip to content

Auth server: no grace period for rotated client refresh tokens, concurrent sessions get invalid_grant #7126

Open

Description

@carlos-lopez-vecino

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.RotateRefreshToken in pkg/authserver/storage/redis.go calls Del on 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.RevokeRefreshTokenMaybeGracePeriod has the comment "optionally allowing a grace period. For this implementation, we revoke immediately." and calls RevokeRefreshToken.
  • 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:

  1. Authorize a client against the embedded auth server and store the refresh token in a place that two processes can read.
  2. Start two sessions that both use that token.
  3. 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, GetRefreshTokenSession still 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageIssue needs initial triage by a maintainer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions