Skip to content

Auth module: Align Session.SignOut with the cross-SDK sign-out spec on an ended session, a missing session under the global scope and a refresh that fails before the request - #175

Merged
QuintinWillison merged 3 commits into
mainfrom
auth-session-spec-alignment
Nov 4, 2026

Conversation

@QuintinWillison

@QuintinWillison QuintinWillison commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

🤖 Claude's Summary

Aligns Session.SignOut with the cross-SDK sign-out spec, sign_out.md as merged in supabase/sdk#302, in the places the first implementation differed:

  • A sign-out of a session that has already ended returns success without a request, under the global and local scopes. The others scope keeps returning ErrSignedOut, having no credential for the one effect it asks for.
  • Under the global scope a 403 session_not_found is a failure that keeps the session. The server answers it before the logout handler runs, so nothing was revoked, and WithLocalScope then ends the session.
  • Under the others scope a 403 user_not_found is success.
  • A refresh that fails before the request sends the access token while it is still unexpired, as any request would, and otherwise sends nothing. Under the local scope a refresh answered refresh_token_not_found, session_not_found or session_expired is success, the session being gone already, and any other failure of an expired token is reported. Under the global scope a rejected refresh of an expired token ends the session as signed out and returns the server's rejection, under the others scope it ends the session as expired and returns ErrSessionExpired, and a transient failure keeps the session and returns the error under both.

…ec on an ended session, a missing session under the global scope and a refresh that fails before the request.

Co-Authored-By: Claude <noreply@anthropic.com>
@QuintinWillison QuintinWillison self-assigned this Nov 3, 2026
@QuintinWillison QuintinWillison added the auth Relates to the auth module. label Nov 3, 2026
@QuintinWillison
QuintinWillison marked this pull request as ready for review November 3, 2026 12:01

@cmelchior cmelchior 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.

The bullet point list above looks correct to me.

I got Claude to review the implementation, and it found some issues that also looked correct to me, so I got it to report them as well.

It is really great to have Go as a guinea pig for the spec, as it really illustrates why having a more comprehensible shared test suite would be very valuable

Comment thread auth/session.go Outdated
if !errors.As(err, &ended) {
return nil, err
}
if ended.Status == session.SignedOut && ended.Cause != nil {

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.

Repeat sign-out returns the old rejection. Say the token has expired and the refresh gets 400 refresh_token_not_found. The first SignOut() ends the session with EndedError{SignedOut, Cause: rejection} and returns the rejection. A retry, or a second goroutine calling SignOut(), then hits the s.status != Live branch in FreshToken, which returns the stored EndedError with Cause still set. That lands here and returns the rejection again. The doc says an already-ended session returns (&SignOutResult{}, nil). Could we add a test for signing out twice after a rejected refresh?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 35acf1d. SignOutToken returns the rejection itself and reserves *EndedError for a session that had already ended, so the global path reports the rejection once and success after that. The subtest now signs out a second time.

Comment thread auth/session.go Outdated
bearer := held.AccessToken()
if held.ExpiresAt().Sub(s.client.now()) <= expiryMargin {
renewed, err := s.client.refreshGrant(ctx, held.RefreshToken())
if err != nil {

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.

A transient refresh failure leaves the server session unrevoked. Say the token has 20s left, so it's inside the margin but still valid, and the refresh gets a 503 or a network error. Before this change the still-valid held.AccessToken() went to /logout?scope=local. Now we return the error without sending anything. signOutLocally has already ended the session locally, so a retried SignOut(WithLocalScope()) takes the EndedError path and returns success. The refresh token stays valid on the server and nothing can retry it. Could we fall back to the captured bearer when it hasn't expired?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 35acf1d. The flowchart's branch is Access token expired?, so an unexpired token goes straight to /logout under the spec and the first version read it too narrowly. revokeHeld now sends the held token while it is unexpired on any failure other than the three already-signed-out codes.

Comment thread auth/internal/session/session.go Outdated
}
})
if endingNow {
_ = s.perform(ctx, clearing)

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.

Store-clear errors are dropped. _ = s.perform(ctx, clearing) discards the error here, and _, err = s.renew(...) discards the renew writeErr too. If the store's Delete fails, the session ends in memory but the store still holds it. SignOut returns only the rejection or ErrSessionExpired, and a later SessionFromStore restores the dead document. The SignOut doc says a store that can't be cleared is reported as the wrapped error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 35acf1d for the call that performs the clear: the failure is returned beside the rejection, as the local path already does. A clear performed by a joined refresh still reaches no one, since only the starter writes the store.

Comment thread auth/internal/session/session.go Outdated
return token, err
}

_, err = s.renew(ctx, ending)

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.

The result depends on concurrent calls. renew can join a refresh that another caller started, and that refresh ends the session with the starter's ending status, not ours. Example: a Token() refresh is in flight with ending=Expired, and a bare SignOut joins it. The server rejects the refresh and the session ends as Expired. signOutGlobally sees Status=Expired, returns success instead of the rejection, and the other sessions are never revoked. The reverse also happens: WithOthersScope joins a flight started by a global sign-out and returns ErrSignedOut instead of ErrSessionExpired.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 35acf1d. The flight carries the ending status, raised to signed out when a sign-out joins, so the read gets ErrSignedOut and the sign-out gets the rejection. A new subtest covers the join.

Comment thread auth/internal/session/session.go Outdated
})
return token, err
}
if !s.definitive(err) {

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.

A transient failure blocks sign-out while the token is still valid. With 30s left on the token and a 503 from /token, this returns the 503 and no logout is sent. Before this change, SignOut went through AccessToken(), got the unexpired token back, and the logout succeeded. So a /token outage now stops global and others-scope sign-outs even though the bearer would have worked. Maybe fall back to the current token when !definitive(err) and it hasn't expired?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 35acf1d, with the same correction as in revokeHeld: the sign-out path now takes its token exactly as AccessToken does, and others is AccessToken followed by the request.

Comment thread auth/internal/session/session.go Outdated
err = s.endedErrorLocked()
case s.state.expiresAt.Sub(s.now()) > s.expiryMargin:
token = s.state.accessToken
case s.rejected:

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.

A remembered rejection ends a still-valid session. Say s.rejected is true and the token has 50s left. Then SignOut(WithOthersScope()) ends the current session as Expired and returns ErrSessionExpired without sending the logout. That contradicts "this one stays live". Token() would keep this session live until it actually expires, and the valid token could still have revoked the other sessions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 35acf1d. A remembered rejection ends the session only once the token has expired, as Token already did, so an unexpired token is sent. A new subtest covers it.

Comment thread auth/internal/session/session.go Outdated
if !s.definitive(err) {
return "", err
}
// renew ended the session only if the token had expired. A rejection

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.

nit/design: Two functions now decide whether to end the session. renew ends it on a rejection only when the token has expired, and FreshToken checks definitive(err) again and ends it itself. The *EndedError case only matches because Unwrap exposes the *Error cause. If renew's expiry rule or EndedError.Unwrap changes, this has to be updated by hand. Passing an "end on any rejection" flag to renew would let us drop the second lock block and the extra perform.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 35acf1d. renew alone ends the session on a rejection, and the second lock block and its perform are gone.

Comment thread auth/internal/session/session.go Outdated
// as it was, and a rejection, remembered or answered now, ends the session
// with status ending and is returned as the [*EndedError] carrying it as
// Cause. An already ended session returns its [*EndedError].
func (s *Session) FreshToken(ctx context.Context, ending Status) (string, error) {

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.

nit/design: FreshToken repeats most of Token's structure: the lock, the status/margin/rejected switch, renew, then reading the state again under the lock. The two copies have already drifted. Token handles ctx.Err() and this doesn't, and revokeHeld uses <= expiryMargin where this uses >. Could the two share one helper?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 35acf1d. Token and SignOutToken share one body, differing in the ending status and in whether an ended session's error replaces the rejection. revokeHeld stays separate because the spec makes its refresh a detached one.

Comment thread auth/session.go
}
scope := signout.Resolve(options...).Scope()
if scope == "local" {
switch scope {

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.

nit: "local", "others" and "global" appear as string literals in SignOut, signOutGlobally, revokeOthers, revokeHeld and nothingLeftToRevoke. Could these be constants shared with the signout package? A typo like nothingLeftToRevoke(err, "lcoal") would compile and silently turn session_not_found into a failure.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 35acf1d: one definition in auth/internal/scopes, read by signout and auth.

QuintinWillison and others added 2 commits November 3, 2026 17:21
…expired token when its refresh fails, and report a rejected refresh once.

Co-Authored-By: Claude <noreply@anthropic.com>
… to workaround boundaries.

Co-Authored-By: Claude <noreply@anthropic.com>
@QuintinWillison

Copy link
Copy Markdown
Member Author

Thanks for the review input, @cmelchior - Pushed 35acf1d for all nine threads. The sign-out path now takes its token the way AccessToken does, which answers the six correctness points with one rule.

@QuintinWillison QuintinWillison added the sdk-spec-alignment A canonical capability specification has changed in supabase/sdk, to which we need to conform. label Nov 4, 2026
@QuintinWillison
QuintinWillison removed this pull request from stack #177 November 4, 2026 20:55
@QuintinWillison
QuintinWillison merged commit 256554a into main Nov 4, 2026
11 checks passed
@QuintinWillison
QuintinWillison deleted the auth-session-spec-alignment branch November 4, 2026 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Relates to the auth module. sdk-spec-alignment A canonical capability specification has changed in supabase/sdk, to which we need to conform.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants