My JWT notes have one entry on refresh tokens. Translated, this is all of it:
- Every time an access token is issued, issue a new refresh token too, and keep the old one.
- If an old refresh token is used again, do not try to tell the user from the hacker: revoke every active refresh token.
- The user logs in again.
RFC 9700 describes the trade: on replay, the server cannot tell who sent the old token.
The note says what to do when an old token comes back. It does not ask how often one comes back when nobody has stolen anything. So I built it and counted.
I tested Node.js 22 with PostgreSQL 18.6 in Docker. Refresh tokens were hashed; browser tests used HttpOnly cookies in Chromium 153. Access tokens lasted 15 minutes except in the theft test, where I used five seconds.
1. The case it was built for: it works, with two gaps
I let the attacker use the original refresh token, RT0, first:
attacker POST /auth/refresh RT0 200 gets RT1 and an access token user POST /auth/refresh RT0 401 reuse: every refresh token revoked attacker POST /auth/refresh RT1 401 revoked attacker GET /api/me AT 200 ...and 200 until it expires
If the user rotates first, the attacker’s next use of RT0 revokes the family instead.
Previously issued access tokens still work until expiry. When the user stayed away, the attacker refreshed 96 times before the user returned and triggered detection. A family lifetime caps that exposure.
2. Nobody stole anything: one page, two tabs
I loaded a page with an expired access token. Three parallel requests got a 401 and all tried to refresh with RT0:
| Client | page loads | logged out |
|---|---|---|
| 2 calls, each refreshes on 401 | 200 | 200 |
| 3 calls, each refreshes on 401 | 200 | 200 |
| 3 calls, one shared refresh | 200 | 0 |
The first refresh issued RT1. The second looked like a replay and revoked it.
I repeated the test with two tabs sharing the same cookie:
| Two tabs, Chromium 153 | trials | logged out |
|---|---|---|
| plain fetch | 30 | 30 |
| refresh inside navigator.locks | 30 | 0 |
With a 100 ms response delay, every gap up to 90 ms between the two refreshes logged the user out; from 110 ms on, none did. The window is one round trip, however long the network makes it.
3. A response that never arrived
I made the server rotate the token but reply after the client timed out. All 50 retries sent RT0 again and ended the session. Okta documents this failure mode as a reason for a grace period.
4. The race in the check itself
A separate read and write let both requests see RT0 as unused:
| Server | 2 refreshes at once | logged out | two live tokens |
|---|---|---|---|
| read, check, then write | 200 | 1 | 199 |
| one atomic UPDATE | 200 | 200 | 0 |
The apparent improvement was a bug: two live successors were issued for a token meant to be single-use.
Check and claim the old token in one statement:
UPDATE refresh_tokens SET used_at = now() WHERE token_hash = $1 AND used_at IS NULL AND revoked_at IS NULL RETURNING id, user_id, family_id; -- one row: this request owns the rotation -- no row: unknown, revoked or already used; find out which
At Read Committed, the second update waits for the first to commit, rechecks used_at IS NULL and matches nothing. The token is now truly single-use, and the parallel page loads from section 2 log the user out 200 times out of 200. The buggy version had been hiding the problem.
5. A grace window, built two ways
I tested a one-second grace window two ways: return the successor already issued, or issue another token:
| Inside the window | 3-call page, logged out | lost response, logged out | extra live tokens | thief replays in time |
|---|---|---|---|---|
| nothing (no window) | 200 / 200 | 50 / 50 | none | caught |
| return the successor | 0 / 200 | 0 / 50 | none | caught at its next refresh |
| issue another token | 0 / 200 | 0 / 50 | two per 3-call page | not caught in 8 rounds |
Both removed the tested logouts. Returning RT1 let a replay inside the window get that token too, but reuse was caught at the next competing refresh in my test. Issuing another independent token forked the chain and missed the thief through eight rounds.
Replaying the successor requires retaining it for the window. These results are for my implementations; I did not test Auth0’s overlap behavior.
6. Fix the client as well
Use one promise per page and the Web Locks API to serialize refreshes across tabs:
let inflight = null;
// One refresh per page, and one per origin at a time across tabs.
function refreshTokens() {
inflight ??= navigator.locks
.request("refresh-token", async () => {
const res = await fetch("/auth/refresh", { method: "POST" });
if (!res.ok) throw new Error("session ended");
return (await res.json()).access;
})
.finally(() => { inflight = null; });
return inflight;
}This gave zero logouts in 100 page loads and 30 two-tab runs. It does not cover lost responses. For coordination between server workers, see my Shopee token post.
7. Revoke what, exactly?
I logged in on three devices and made two tabs collide on the first:
| On reuse, revoke | device 1 | devices 2 and 3 |
|---|---|---|
| every token of the user | logged out | logged out |
| the family (one login's chain) | logged out | still signed in |
Revoke the family associated with the grant. That contains the logout to one login; it does not fix the collision itself.
What the notes say now
- Claim the old token with one atomic
UPDATE. Read-then-write let a single-use token be used twice 199 times in 200. - An old token coming back is usually your own client. Of the four ways it came back in this post, one was a thief.
- Give the previous token a short window, and inside it return the token you already issued. Issuing another one forked the family and hid the thief for eight rounds.
- Refresh once per page, and once per origin at a time, with a shared promise and
navigator.locks. - On reuse, revoke the family instead of every token the user has.
- Revocation does not recall access tokens, and detection waits for the real user. Keep access tokens short and cap a family’s lifetime.
The note was right about the thief, but it never asked who else sends an old refresh token. In this post it was a second tab, a page’s parallel requests and a lost response, and each time it was the real user.