Skip to content
This repository was archived by the owner on Aug 7, 2026. It is now read-only.

fix: classify refresh token rejections from current gotrue - #131

Merged
Tr00d merged 8 commits into
masterfrom
fix-refresh-token
Jul 7, 2026
Merged

Tr00d merged 8 commits into
masterfrom
fix-refresh-token

Conversation

@Tr00d

@Tr00d Tr00d commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Classification fix

Current gotrue (since supabase/auth#2216) validates the refresh token format before the database lookup, so rejections come from two paths with different bodies:

  • Malformed token: {"error_code":"validation_failed","msg":"Refresh token is not valid"}
  • Well-formed but unknown/revoked token: {"error_code":"refresh_token_not_found","msg":"Invalid Refresh Token: Refresh Token Not Found"}

FailureHint.DetectReason only matched the legacy "Invalid Refresh Token" message, so format rejections were classified as Reason.Unknown — and since RefreshSession()/RefreshToken() only destroy the session on InvalidRefreshToken, the client kept a dead session as CurrentSession. Both paths are now classified as InvalidRefreshToken, including a match on the stable refresh_token_not_found error code since the msg wording has already drifted between server versions.

RefreshToken(accessToken, refreshToken) is also now exposed on IGotrueClient. It has been public on Client since #72 and the same shape exists on IGotrueStatelessClient, but consumers of the umbrella supabase-csharp package only see IGotrueClient, so it was unreachable for them without downcasting. Note: source-breaking for hand-written implementations of the interface (dynamic mocks are unaffected).

Test suite improvement

New GotrueTests/TokenRefresh/ suite in two tiers, with shared fixtures in GotrueTests/Support/:

  • RefreshContractTests (WireMock): pins the POST /token?grant_type=refresh_token exchange — request shape, response-to-session mapping, and classification of both rejection bodies.
  • RefreshBehaviorTests (live Supabase CLI stack, gotrue v2.191.0): token rotation, expired-session refresh, and rejected tokens destroying the session. This tier is what surfaced the classification gap: the repo's docker-compose stack pins gotrue:v2.155.6, which predates format validation and always returns the legacy message. Endpoint and key are overridable via SUPABASE_CLI_AUTH_URL / SUPABASE_CLI_ANON_KEY.

Legacy tests fully superseded by the new suite were removed (AnonKeyClientFailureTests bogus/expired token, AnonKeyClientTests token-refreshed event); the persistence assertion unique to the latter moved into the new suite.

Updating the docker-compose stack to a current gotrue/postgres is deliberately left for a separate PR.

Workflow update

The behavior tier needs a Supabase CLI stack on 127.0.0.1:54321, but CI only started the legacy docker-compose stack (:9999), so those tests failed with connection refused on the runner. The workflow now installs the Supabase CLI and runs supabase start alongside the existing compose stack, backed by a minimal supabase/config.toml added to the repo (api, db, and auth only — everything else disabled for startup speed). The CLI's default URL and demo anon key are exactly what the test fixtures expect, so no secrets or environment wiring is required.

Tr00d added 7 commits July 7, 2026 10:24
Since the v2 refresh token algorithm (supabase/auth#2216), gotrue
validates the token format before the database lookup and rejects
malformed tokens with "Refresh token is not valid" (validation_failed)
instead of "Invalid Refresh Token" (refresh_token_not_found).
DetectReason only matched the lookup-path message, so these rejections
were classified Unknown and RefreshSession kept the dead session
instead of destroying it.

Match both paths, including the stable refresh_token_not_found
error code.
The overload already exists on Client but was missing from the
interface, so consumers working against IGotrueClient could not
refresh a persisted session without downcasting.
Split refresh coverage into two layers: RefreshContractTests pins the
HTTP contract against a WireMock server (request shape, session
mapping, error classification for both rejection paths), and
RefreshBehaviorTests runs against the local Supabase CLI stack
(rotation, expired sessions, malformed and unknown tokens destroying
the session). Shared fixtures live in GotrueTests/Support: a client
factory per tier and a WireMock wrapper with fluent request
assertions.
The bogus and expired token tests in AnonKeyClientFailureTests and the
token-refreshed event test in AnonKeyClientTests are covered by the
new suites; the persistence assertion unique to the latter moved into
RefreshBehaviorTests.
@Tr00d Tr00d changed the title fix: destroy session when current gotrue rejects a refresh token fix: classify refresh token rejections from current gotrue Jul 7, 2026
@Tr00d
Tr00d marked this pull request as ready for review July 7, 2026 09:23
@Tr00d
Tr00d merged commit 2f1f6ea into master Jul 7, 2026
1 check passed
@Tr00d
Tr00d deleted the fix-refresh-token branch July 7, 2026 09:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants