Repository navigation
fix(oidc): revoke refresh tokens when a code is replayed - #118
Merged
Merged
Conversation
PiquelChips
force-pushed
the
fix/code-replay-revocation
branch
from
October 14, 2026 09:51
ad44e6e to
2c6acff
Compare
Redeeming an already used authorization code only returned invalid_grant; the refresh family created from that code stayed valid. RFC 6749 4.1.2 and RFC 9700 ask the server to revoke the tokens issued from a replayed code. Each refresh family now records the code it was redeemed from (migration 0002). Redeeming a consumed code with its correct secret revokes that family, commits the revocation with an authorization_code.replay audit event, and still answers invalid_grant. A wrong secret revokes nothing. Cleanup keeps redeemed codes a day so late replays are still detected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cleanup deleted redeemed codes a day after use, which cleared the family link while refresh tokens from that code could still live for the rest of the browser session. A replay after day one then revoked nothing. A redeemed code is now purged only after its first day and once no unrevoked family linked to it has a live session. A partial index on consumed_at keeps the cleanup scan cheap. The architecture doc again states that the schema replaces the prototype and needs a fresh database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PiquelChips
force-pushed
the
fix/code-replay-revocation
branch
from
October 14, 2026 12:00
2c6acff to
bb41711
Compare
The replay lookup filtered on consumed_at, which its own snapshot saw as NULL while a first redemption was still open. If the code had expired, or its client dropped the redirect URI, the consuming update skipped the row without waiting, the lookup locked nothing, and the replay returned Invalid while the first redemption committed an unrevoked family. The lookup now matches the code by ID, digest and client only, so FOR SHARE waits and consumption is read from the committed row. Replay detection is also bound to the presenting client, so a leaked code presented by another client is just invalid and revokes nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Redeeming an authorization code a second time only returned
invalid_grant. The refresh tokens issued from the first redemption stayed valid, but RFC 6749 §4.1.2 and RFC 9700 say the server should revoke them.How
0002adds a nullablerefresh_token_families.authorization_code_id, withON DELETE SET NULLand an index, so purging codes still works.InsertRefreshFamily.authorization_code_idis required, so every new family records the code it came from.consume_code. When the one-useUPDATEmatches nothing,consume_codelooks for a consumed code with the same ID, secret digest and presenting client. If it finds one, it revokes that code's families and returns the newCodeRedemptionError::Replay. A guessed code ID with the wrong secret, or a leaked code presented by another client, getsInvalidand revokes nothing (RFC 6749 §4.1.3).consume_code, the replay lookup and revocation,insert_family, code cleanup) arequery!/query_as!/query_scalar!macros like the rest of the crate, and the.sqlxcache is regenerated.exchange_codecommits the revocation with anauthorization_code.replayaudit event, logsauth.code_replayand still answersinvalid_grant. This reuses the same commit helper as refresh-token replay (commit_rejection).authorization_codes.consumed_at(in0002) narrows the scan to redeemed codes, and the live-family check uses the newauthorization_code_idindex. The scan still rechecks every retained code on each pass, so it grows with redeemed codes still within their session's lifetime, not only with purgeable ones.Concurrency
UPDATEonly waits on the row lock while the code still looks redeemable in its snapshot. If the code has expired, or the client's policy or redirect URIs changed, theUPDATEskips the row without waiting. So the replay lookup selects the code by ID, digest and client only, and reportsconsumed_at IS NOT NULLin its select list rather than filtering on it. That way the row always qualifies,FOR SHAREwaits for the open redemption, and PostgreSQL returns the committed row. The same lock stops cleanup (SKIP LOCKED) from purging the code mid-replay.KEY SHAREon users, which never conflicts with theNO KEY UPDATElocks the account paths take.Testing
oidc::tests::code_replay_revokes_its_refresh_family: a guessed secret for the code ID is rejected and the refresh token still rotates. A real replay is rejected withinvalid_grant, and the rotated refresh token then fails.tokens::tests::code_replay_waits_for_first_redemption_and_revokes_its_family: the replay waits on an uncommitted first redemption, then returnsReplayand revokes the family that redemption committed. It runs twice: once while the code is still redeemable (theUPDATEwaits), and once after the client drops the redirect URI (only the lookup waits). The second run fails against the oldconsumed_atfilter.tokens::tests::code_replay_by_another_client_revokes_nothing: a redeemed code presented by another client isInvalid, and its family still rotates.connection::tests::cleanup_keeps_redeemed_codes_while_their_session_is_live: a code redeemed two days ago survives cleanup while its family's session is live, and is purged (unlinking the family) once the session is revoked..sqlxcache matches the schema; CI runs the full suite.just lint-rustandjust fmt-check-rustpass.0002applies on top of a populated0001database.EXPLAINshows the cleanup query using theconsumed_atand active-code partial indexes.0002changed. Previews have no OIDC client configured, so I couldn't run the replay over HTTPS there; the Rust tests above go through the full token endpoint router instead.Note
Written by Claude Opus 5.5 in Claude Code and reviewed by GPT 6.1 Sol and Claude Opus 5.5 (via T3 Code)
🤖 Generated with Claude Code