fix(oidc): make authorization code redemption atomic (BUNYIP-199) #238
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/bunyip-199-atomic-code-redemption"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Authorization code redemption loaded the row, verified it, and marked it consumed in three separate round-trips with no locking, so two concurrent /token requests bearing the same code could both pass the consumed_at check and both mint tokens (double-spend / code-replay race).
Wrap load+consume in a SERIALIZABLE transaction with SELECT ... FOR UPDATE, exactly as rotate_refresh_token does. The row-level lock plus SERIALIZABLE isolation serialise concurrent redemptions: the loser blocks on the lock, then sees consumed_at set and loses the race. The final UPDATE is additionally guarded on consumed_at IS NULL with a zero-rows-affected check as defence-in-depth. On the lost race the token family minted from the same op_session is revoked (revoke_reason = 'reuse_detected'), mirroring refresh-token reuse detection, before returning invalid_grant.
#BUNYIP-199