You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
First command after the OAuth token expires fails with 401: the token is refreshed, but the request is sent with the old one #577
Version: cf 1.0.0-beta.29 (also seen in 1.0.0-beta.5), Node 26.12.1, macOS. Signed in with cf auth login (OAuth, default profile).
What happens
When the stored OAuth token has expired, the next command refreshes it (the config file gets a new token and a new expiration_time), but the API request itself still goes out with the old token and fails:
Running the same command again straight away works, with no new login.
Expected: the request is sent (or retried once) with the refreshed token, so the command succeeds.
Steps to reproduce
Sign in with cf auth login.
Wait until the token expires (about an hour), or set expiration_time in ~/Library/Preferences/cloudflare/config/default.json to a time in the past to make it happen at once.
Run any API command, e.g. cf cache purge -z <zone> --force --body '{"hosts":["<host>"]}'. It fails with the 401 above, and expiration_time in the config is now an hour ahead.
Run it again: it succeeds.
Step 2's shortcut shows it isn't only about a truly expired token: the old token was still within its lifetime, yet it was rejected after the refresh. So the refresh seems to revoke the old token while the pending request still uses it.
Impact: scripts that run cf once in a while (a deploy that purges the cache, say) fail on the first call after each hour, with a message that suggests logging in again.
cf already sends the refreshed token. With expiration_time forced into the past, both main and beta.29 refresh first and then send the request with the new token. I checked this against a fake OAuth/API server and a real account, using cache purge, zone-scoped reads, and zone ID or zone name. I couldn't reproduce the 401 from a single cf process.
A refresh revokes the previous access token immediately. Right after a refresh, the old token gets 403 and the new one gets 200.
So the 401 most likely comes from something still holding the old token after a refresh:
another cf process refreshing at the same moment (scripts or parallel jobs), whose refresh revokes the token this process already loaded, or
a token expiring partway through a long-running command.
When the API rejects an OAuth token (401/403), cf rereads the stored credentials, refreshing them if they have expired. If a different token is now stored, it retries the request once with it. Environment and --temporary-account tokens are never replaced, and streamed bodies are not replayed. This applies to generated commands, requestApi, and fetchRawBytes. It includes tests and a patch changeset, and pnpm check passes.
Two processes refreshing at the same moment can still both use the same refresh token. Fully preventing that would need a lock around refresh in @cloudflare/workers-auth.
I'm not on VOUCHED.td, so a PR from me would be auto-closed. Could a maintainer vouch me, or pick up the branch directly?
Activity
sawirricardo commentedon Nov 11, 2026
Follow-up after digging in:
cf already sends the refreshed token. With
expiration_timeforced into the past, bothmainand beta.29 refresh first and then send the request with the new token. I checked this against a fake OAuth/API server and a real account, usingcache purge, zone-scoped reads, and zone ID or zone name. I couldn't reproduce the 401 from a single cf process.A refresh revokes the previous access token immediately. Right after a refresh, the old token gets 403 and the new one gets 200.
So the 401 most likely comes from something still holding the old token after a refresh:
Proposed fix: main...sawirricardo:cf:fix/oauth-token-retry-577
When the API rejects an OAuth token (401/403), cf rereads the stored credentials, refreshing them if they have expired. If a different token is now stored, it retries the request once with it. Environment and
--temporary-accounttokens are never replaced, and streamed bodies are not replayed. This applies to generated commands,requestApi, andfetchRawBytes. It includes tests and a patch changeset, andpnpm checkpasses.Two processes refreshing at the same moment can still both use the same refresh token. Fully preventing that would need a lock around refresh in
@cloudflare/workers-auth.I'm not on
VOUCHED.td, so a PR from me would be auto-closed. Could a maintainer vouch me, or pick up the branch directly?