Skip to content

fix(oidc): revoke refresh tokens when a code is replayed - #118

Merged
PiquelChips merged 3 commits into
mainfrom
fix/code-replay-revocation
Oct 14, 2026
Merged

PiquelChips merged 3 commits into
mainfrom
fix/code-replay-revocation

Conversation

@PiquelChips

@PiquelChips PiquelChips commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

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

  • Link families to codes. Migration 0002 adds a nullable refresh_token_families.authorization_code_id, with ON DELETE SET NULL and an index, so purging codes still works. InsertRefreshFamily.authorization_code_id is required, so every new family records the code it came from.
  • Detect replays in consume_code. When the one-use UPDATE matches nothing, consume_code looks 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 new CodeRedemptionError::Replay. A guessed code ID with the wrong secret, or a leaked code presented by another client, gets Invalid and revokes nothing (RFC 6749 §4.1.3).
  • Checked SQL. The new and changed queries (consume_code, the replay lookup and revocation, insert_family, code cleanup) are query!/query_as!/query_scalar! macros like the rest of the crate, and the .sqlx cache is regenerated.
  • Commit on the rejection path. exchange_code commits the revocation with an authorization_code.replay audit event, logs auth.code_replay and still answers invalid_grant. This reuses the same commit helper as refresh-token replay (commit_rejection).
  • Leave the browser session alone. A leaked code exposes the client's callback, not the session cookie. Revoking the session would also let anyone holding a leaked code sign the user out of every app. Access tokens already issued can't be revoked; they expire within 5 minutes.
  • Keep redeemed codes while they still protect something. Refresh tokens live as long as the browser session (up to 30 days). Cleanup now keeps a redeemed code for at least a day after redemption, and after that for as long as an unrevoked family linked to it has a live session. Once the session ends or every family is revoked, a replay has nothing left to revoke and the code is purged. The day-long minimum means replays of codes that produced no refresh family are still audited. A partial index on authorization_codes.consumed_at (in 0002) narrows the scan to redeemed codes, and the live-family check uses the new authorization_code_id index. 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

  • A replay that races the first redemption must wait for it. The replay's consuming UPDATE only 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, the UPDATE skips the row without waiting. So the replay lookup selects the code by ID, digest and client only, and reports consumed_at IS NOT NULL in its select list rather than filtering on it. That way the row always qualifies, FOR SHARE waits for the open redemption, and PostgreSQL returns the committed row. The same lock stops cleanup (SKIP LOCKED) from purging the code mid-replay.
  • Families are revoked in a separate statement. That statement gets a fresh snapshot, so it sees the family the first redemption just committed. A single CTE would miss it.
  • The replay path locks the code row, then the family row, and takes no user or session lock. Exchange and cleanup also lock the code first, so this stays within the documented user → session → family order. Audit inserts only take KEY SHARE on users, which never conflicts with the NO KEY UPDATE locks 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 with invalid_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 returns Replay and revokes the family that redemption committed. It runs twice: once while the code is still redeemable (the UPDATE waits), and once after the client drops the redirect URI (only the lookup waits). The second run fails against the old consumed_at filter.
  • tokens::tests::code_replay_by_another_client_revokes_nothing: a redeemed code presented by another client is Invalid, 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.
  • Database and OIDC replay tests pass on a scratch Postgres 18 cluster, and the committed .sqlx cache matches the schema; CI runs the full suite.
  • just lint-rust and just fmt-check-rust pass.
  • Migration checked by hand: 0002 applies on top of a populated 0001 database. EXPLAIN shows the cleanup query using the consumed_at and active-code partial indexes.
  • Preview: redeployed from scratch, because the unreleased 0002 changed. 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

PiquelChips and others added 2 commits October 14, 2026 13:59
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
PiquelChips force-pushed the fix/code-replay-revocation branch from 2c6acff to bb41711 Compare October 14, 2026 12:00
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>
@PiquelChips PiquelChips self-assigned this Oct 14, 2026
@PiquelChips PiquelChips added bug Something isn't working apps/auth-service Origin Auth at auth.sourceorigin.dev, its settings and developer console, and the operator CLI schema-change Any changes to the database schema labels Oct 14, 2026
@PiquelChips
PiquelChips merged commit 0e43ece into main Oct 14, 2026
9 checks passed
@PiquelChips
PiquelChips deleted the fix/code-replay-revocation branch October 14, 2026 12:44
@github-project-automation github-project-automation Bot moved this to Needs Triage in sourceorigin.dev Oct 14, 2026
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in sourceorigin.dev Oct 14, 2026
@PiquelChips PiquelChips added this to the 0.1 milestone Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apps/auth-service Origin Auth at auth.sourceorigin.dev, its settings and developer console, and the operator CLI bug Something isn't working schema-change Any changes to the database schema

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant