Skip to content

First command after the OAuth token expires fails with 401: the token is refreshed, but the request is sent with the old one #577

Open

Description

@sawirricardo

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:

┌ APIError
│ [10000] Authentication error
│ 401 Unauthorized · HTTP /zones/<zone>/purge_cache
└

→ Try running: cf auth login

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

  1. Sign in with cf auth login.
  2. 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.
  3. 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.
  4. 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.

Activity

  1. sawirricardo commented on Nov 11, 2026

    @sawirricardo
    Author

    Follow-up after digging in:

    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.

    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-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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions