jcode icon indicating copy to clipboard operation
jcode copied to clipboard

OpenAI OAuth refresh lacks an explicit end-to-end HTTP deadline; preserve rotation commit safety

Open mg-mg-mg opened this issue 1 month ago • 1 comments

Source-level follow-up

While reviewing cancellation safety for #1756, a separate authentication wait boundary was found in upstream a61c38ee9:

  • crates/jcode-base/src/auth/oauth.rs, refresh_openai_tokens_inner: refresh awaits HTTP send, then error text or success JSON, before synchronously persisting the rotated credentials for a stored account.
  • crates/jcode-provider-core/src/lib.rs, shared_http_client: configures a 15-second connection timeout and transport keepalive, but does not configure an overall request or body-read timeout.
  • crates/jcode-provider-openai-runtime/src/openai_stream_runtime.rs, force_refresh_openai_token: subsequently commits the returned credentials to the in-memory credential lock.

A connection timeout bounds establishment, not a server that accepts the connection and never returns headers or finishes the response body. Thus the refresh phase does not have an explicit end-to-end deadline in this path. This is source inspection, not attribution of any specific incident or a live-account reproduction.

Why this should be separate from stream cancellation

Blindly canceling the entire refresh future is unsafe: a remote rotation may have succeeded while local persistence and in-memory commit are unfinished. Likewise, spawning an unbounded detached refresh task hides rather than resolves the waiting problem.

The focused #1756 stream fix should preserve the existing inline rotation/persistence transaction. This follow-up should separately design bounded network waiting together with correct rotation, persistence, account-scoped coordination, and error/recovery semantics. Do not impose a timeout that simply drops the post-rotation persistence or commit phase.

Acceptance and synthetic verification

  1. Define and document a finite policy for refresh network header/body waiting. Verify it against loopback servers that accept a request but withhold headers, or provide an incomplete body.
  2. Preserve existing account-scoped refresh coordination and rotated-token persistence semantics. A waiting caller's cancellation must not corrupt another account's or another transaction's credentials.
  3. Use gated synthetic transaction tests for cancellation before rotation, after a synthetic successful rotation, during persistence, and before in-memory commit. Distinguish safely canceled work from an already-started shared transaction.
  4. Cover normal success, error responses, malformed JSON, persistence failure, concurrent refresh, and the case where transport failure leaves remote rotation outcome uncertain. Do not automatically reset credentials or repeatedly reuse a known-invalid refresh token.
  5. Surface bounded, privacy-safe failure states without logging tokens, authorization headers, account identifiers, response credential bodies, or local credential paths.
  6. Use dummy credentials and isolated test storage only. No live authentication endpoint calls, customer transcripts, or real credential stores are required for regression tests.

No timeout or cancellation-policy change is claimed here. This report contains no incident logs, session identifiers, user prompts, credential values, private filesystem paths, or account information.

mg-mg-mg avatar Oct 07 '26 19:10 mg-mg-mg

Thanks. This needs a design decision (refresh deadline policy that preserves rotation and persistence safety), so I've flagged it for the maintainer. #1758 keeps the existing transaction intact, as you suggested.

--- — Jcode agent (automated triage), on behalf of @1jehuang

1jehuang avatar Oct 08 '26 02:10 1jehuang