Skip to content

fix(server): answer invalid_grant for a grant revoked during refresh-token issuance - #985

Merged
osanderson merged 4 commits into
mainfrom
fix/race-invalid-grant
Nov 25, 2026
Merged

osanderson merged 4 commits into
mainfrom
fix/race-invalid-grant

Conversation

@osanderson

Copy link
Copy Markdown
Collaborator

A token request whose grant is revoked while the server stores its refresh token now answers invalid_grant (400) instead of server_error (500). Real store failures stay server_error.

Why

When a reused authorization code races the exchange that redeemed it, the reuse revokes the grant. That can happen after the winning exchange has stored its refresh token. The server then re-checks the grant and withdraws the token, so nothing is issued. But the client got a 500, "failed to issue refresh token": a store outage, for what is a refused grant. In concurrent exchanges of one code this was the usual outcome (#966 found it).

fix(server)

The issuance failure goes through a new helper, refreshTokenIssueFailure. It answers invalid_grant ("the grant has been revoked") only when the grant was found revoked meanwhile and its refresh token was withdrawn. Either of the server's two checks can find this:

  • The revocation re-check (confirmGrantNotRevoked): it now marks only an invalid_grant it found. A revocation store that can't answer is still server_error.
  • The registry check (confirmGrantRecorded): it finds the grant's registry entry gone.

Two cases stay server_error:

  • The token couldn't be withdrawn. It may still be live, so withdrawRefreshToken now marks that failure with its own sentinel, and the helper excludes it.
  • The registry or the revocation store failed.

Where it applies: every path that issues a refresh token uses the helper, namely both branches of the authorization code exchange, the CIBA token request, and Server.IssueRefreshToken.

Audit and docs: audit events record the new code, and tests check this. GETTING_STARTED.md and UPGRADING.md (under v0.86.0) say a client or monitoring may now see 400 where it saw 500. No exported API changes.

test(sqlstore)

TestServerCodeReuseRacingIssuanceIsInvalidGrant is no longer skipped. The concurrent-exchange race test now expects losers to answer invalid_grant.

CI also runs sqlstore's tests against the released fapigo its go.mod requires, which still answers server_error. So the old shapes are accepted only when go.mod has no replace of fapigo. Against this tree, the tests are strict.

Tests

  • Root tests: deterministic tests for each check and each issuing path, using the existing fakes and a hook that revokes after the token is stored. Each fails without the fix. Three more tests check that a store failure, an unanswerable revocation check, and a failed withdrawal still answer server_error.
  • Updated tests: two existing tests that asserted the old 500 now assert invalid_grant.
  • Races: the race tests pass 10 runs each on SQLite and PostgreSQL, and against the released fapigo.
  • Mutants: nine new, plus three existing ones re-anchored. All twelve are killed.
  • Suites: the full race suite, sqlstore and every example module pass, and lint is clean.

This changes an error code on the wire, so conformance is running.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

osanderson and others added 3 commits November 25, 2026 23:09
When a grant was revoked while its refresh token was being stored,
typically a reused authorization code racing the exchange that
redeemed it, the server withdrew the token but answered server_error
(HTTP 500) "failed to issue refresh token". Nothing was issued, but
the client saw a store outage for what is a refused grant.

Both checks that run after the token is stored now mark that outcome
with errGrantRevokedWhileIssued: confirmGrantNotRevoked when the
revocation check finds the grant revoked, and confirmGrantRecorded
when the grant registry no longer lists it. A new
refreshTokenIssueFailure maps it to invalid_grant (HTTP 400) "the
grant has been revoked" for the authorization code exchange, the CIBA
token request and IssueRefreshToken alike, so their audit events
record invalid_grant too. Everything else stays server_error: a
revocation store or registry that can't answer, and a revoked grant
whose token then can't be withdrawn (withdrawRefreshToken now marks
that failure), since a live token may remain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…race

A code exchange whose grant a racing reuse revokes while it stores its
refresh token now answers invalid_grant rather than server_error. The
race tests accept that answer, and TestServerCodeReuseRacingIssuanceIsInvalidGrant
is no longer skipped. The server_error shapes FAPIgo answered before
are still accepted, but only when the tests are built against a
released FAPIgo rather than the tree beside this module (go.mod's
replace dropped, as CI's run against the required release does), since
that release may predate the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers the mapping of a grant revoked while its refresh token was
issued to invalid_grant, server_error for a store failure or a token
that couldn't be withdrawn, and each token path's use of it. Updates
the anchors of three existing mutants on the code it changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@osanderson
osanderson force-pushed the fix/race-invalid-grant branch from 80118ad to a0575f7 Compare November 25, 2026 15:14
…token

The server now answers a grant revoked while its refresh token is
issued with invalid_grant, so a code exchange whose grant was revoked
before redemption is refused with the same code whether the check
before issuance or the re-check after it refuses it. The test of the
check before issuance asked for a refresh token, so it no longer told
the two apart. This adds the same exchange without offline_access,
where nothing after redemption re-checks the grant, and runs the
check's mutant against it too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

@osanderson
osanderson merged commit ea03c80 into main Nov 25, 2026
32 checks passed
@osanderson
osanderson deleted the fix/race-invalid-grant branch November 25, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant