Repository navigation
fix(auth): keep the sign-in when a token refresh fails for a passing reason - #43
Conversation
…reason A stored OAuth sign-in was deleted whenever a token refresh failed, whatever the reason. A 429, a 5xx, a timeout, a network error, or two reply processes refreshing one expired one-time refresh token together all signed the person out, and an app that starts several reply commands at once met the last one most of all. - The stored record is removed only when the identity server refuses the grant (400 invalid_grant) and the credentials file still holds the refused record, decided on the same read the removal rewrites. If another process stored a newer one meanwhile, that one is used and nothing is removed. - Another OAuth refusal (400 or 401 with an error) keeps the record and fails auth.refresh_failed, exit 2. A 429 keeps it and fails rate_limited with retry_after. Any other answer, a timeout (20 s), a network error or a 2xx without an access token keeps it and fails reply.unavailable, exit 1. - One refresh at a time across processes: credentials.json.lock, created exclusively beside the file, holds its holder's pid, host, pid namespace and the time it was taken. A lock older than 30 s, or whose holder is gone, is taken over; a live one is waited on for up to 45 s. The pid is trusted only on the same host and in the same pid namespace, since sandboxes that give each command a namespace of its own give them equal pids. A lock is only removed, when let go or taken over, under a guard file after reading it again, so no two processes hold it at once. Letting go never fails the command. Under the lock the record is read again, and a token another process already refreshed is used with no request. - Every write to the file (a refresh, login, logout, a profile rename or delete) is a read-modify-write under the lock; a rename moves the credential in one locked step. The new token is stored as soon as the answer is read, by the same temp-file rename, now retried briefly when Windows refuses it for a moment. - The error keeps what the token endpoint said under details.refresh: status and error, or a reason (timeout, network, unreadable, lock_wait). No token is ever printed. - Every token request, a refresh or a login's code exchange, sends the CLI's User-Agent, as API requests do, instead of Node's default, so the identity server's logs can tell them apart. A test runs two built CLI processes against a local fake identity server that rotates one-time refresh tokens and holds the first refresh until the second arrives: both end signed in, on one refresh.
ArtemKosolap
left a comment
There was a problem hiding this comment.
Approving. The design holds: a record is removed only on a definite refusal of the grant and only while the file still holds the refused record, every other failure keeps it and says whether a repeat makes sense, and one refresh at a time closes the race that was deleting sign-ins. The two codes a passing failure now answers (reply.unavailable, rate_limited) are the ones a caller already retries, so nothing on a consumer's side has to move.
Read: the description, src/credentials/lock.ts whole, the changes to src/auth/resolve.ts, src/credentials/file-store.ts, src/auth/oauth-flow.ts, src/auth/token.ts and src/commands/profile.ts, and the callers of resolve_credential. Not read line by line: the tests and the docs. CI is green on both systems.
Three small notes, none blocking; each can be a follow-up.
-
The branch with no
refreshfunction inresolve_credential(src/auth/resolve.ts) still removes the record unconditionally, outside the re-read. Both production callers passrefresh, so it is dead in the product today, but it is the one path left that contradicts "only a definite refusal removes the record": a caller without a refresh function, started beside one that is refreshing, would wait for the lock and then delete the record the other has just stored. Keeping the record there, or passing the sameonly_ifthe other branches pass, would close it. -
The rename in
FileCredentialStore.write(src/credentials/file-store.ts). After a successful refresh the server has already spent the old refresh token, so this rename is the one step that must not fail. The retries add up to about 0.6 s. If Windows refuses the rename for longer than that (a scanner or a backup holding the file), the new token is dropped; the next run sends the spent one, is refused withinvalid_granton an unchanged file, and removes the sign-in. A longer backoff for this write would make that window much smaller. -
Taking over a stale guard in
try_guard(src/credentials/lock.ts) is a read, a compare and an unlink with nothing serialising them. In principle a process can unlink a fresh guard that a peer created right after another peer removed the stale one, and two processes then pass "read the lock again, then unlink" together. It needs a process that died holding the guard and three others inside the same instant, and the worst outcome is two refreshes at once, which the re-read inrefresh_under_lockalready absorbs. Not worth fixing while the lock guards only this; a sentence in the header comment saying so would be enough.
One thing for after the merge, outside this repository: a program that requires a minimum version of the CLI keeps running the older one until that minimum is raised. Jason's Reply package pins one, so the fix reaches those installations when the package's minimum moves to the release that carries it.
What changed
A stored OAuth sign-in is no longer deleted when a token refresh fails for a passing reason, and reply processes
refreshing one expired sign-in together no longer sign each other out.
Only a definite refusal of the grant removes the record: 400
invalid_grant. Even then the record isremoved only if the credentials file still holds the refused record, and the check is made on the same read
that is rewritten. If another process stored a newer record in the meantime, that one is used and nothing is
removed.
Every other failure keeps the record, and the code says whether to retry:
invalid_grant, file unchangedauth.refresh_failedinvalid_client, …)auth.refresh_failedrate_limited, withretry_afterreply.unavailableThe
--jsonerror carries what the endpoint said underdetails.refresh:statusanderror, or areason(timeout,network,unreadable,lock_wait). The top-levelstatusis not set. No token isever printed.
One refresh at a time across processes, through
credentials.json.lock, created exclusively beside thefile. It holds its holder's pid, host, pid namespace (on Linux), the time it was taken and a random id.
refreshed it, that token is used and no request is sent.
its pid (another host or another pid namespace) is taken over once it is older than 30 s. A live lock is
waited on for up to 45 s.
judged, so two processes never hold it at once. Letting go is best effort and never fails a command.
Every write to the credentials file is a read-modify-write under the lock: a refresh,
auth login,auth logout, and a profile rename or delete. A rename is one locked step.Writing the file: the new token is stored as soon as the answer is read. The write stays a temp file plus
rename, the temp file now has a name of its own, and a rename that Windows refuses for a moment is retried.
A 2xx with a usable
access_tokenis always stored. Whenexpires_inorrefresh_tokenis missing orof another type, the default lifetime and the previous refresh token are kept, as before. The server has
already rotated the refresh token at that point, so dropping the answer would lose the sign-in.
Every token request sends the CLI's User-Agent: a refresh and a login's code exchange both send
reply-cli/<version>, afterREPLY_CLIENTwhen it is set, as API requests already do. Until now they went outas Node's default
node, so the identity server's logs couldn't tell them apart.Docs:
CONTRIBUTING.md("Credentials on disk", "Tests"), the README's auth section, and the reply-cli skilldescribe the new behaviour.
Why
The identity server issues this client one-time refresh tokens, and the access token lives one hour. A program
that runs several
replycommands at once, typically right after it starts, finds the token expired in all ofthem. Every command then sends the same refresh token. The server accepts the first and answers
invalid_grantto the rest, and each refused process deleted the stored record, including the new token thewinner had just saved. Any 429, 5xx, timeout or network error on a refresh deleted the sign-in the same way, and
the error didn't say which of these it was.
How it was verified
Tests first. Every new or changed test was run against the code before the change and failed. That
includes the two-process test, which ended with exit codes
[2, 0]instead of[0, 0].src/__tests__/auth/refresh-processes.test.ts): two built CLI processes runauth whoamion one expired sign-in, against a local fake identity server that rotates one-time refreshtokens and holds the first refresh until the second arrives.
REPLY_*variable stripped, so a developer's real sign-in is nevertouched.
with and without a newer record, the lock (waiting, stale takeover, another host or pid namespace, a takeover
raced by a peer, letting go when the unlink fails), and that no token string appears in any printed error.
npm run build && npm testgives 1108 passed, 2 skipped on Linux. On Windows 11 with Defenderon, it gives 1105 passed, 5 skipped (the POSIX permission tests and the Linux-only filesystem tests).
Race probes against local fakes, on Linux and on Windows:
two holders.
one refresh per round, every process exiting 0, and no lock or temp file left behind. Against an endpoint that
always answers 429 or 503, every process failed
rate_limitedorreply.unavailable, and the record was kept.bwrap --unshare-pid,unshare -p), with equal and with different pids, gave no round with two holders, and one refresh per roundthrough the CLI.
Against Reply's dev identity server, with a test account:
invalid_granteach round, and the sign-inwas deleted every time, twice after the winning process had already stored its new token.
invalid_grant, which this change reports as"details":{"refresh":{"status":400,"error":"invalid_grant"}}. Reusing it doesn't revoke the newer token,which is what the re-read relies on.
reply-cli/<version>on this change's token requests, andnodeon v0.23.0's.