Skip to content

fix(openai): bound ChatGPT OAuth token error response reads - #99609

Closed
mushuiyu886 wants to merge 1 commit into
openclaw:mainfrom
mushuiyu886:fix/followup-98496
Closed

mushuiyu886 wants to merge 1 commit into
openclaw:mainfrom
mushuiyu886:fix/followup-98496

Conversation

@mushuiyu886

Copy link
Copy Markdown
Contributor

Summary

  • Bound OpenAI ChatGPT OAuth token error response materialization before returning token failure diagnostics.
  • Keep successful token responses on the existing JSON path while capping non-OK token response bodies at 8 KiB.
  • Add focused regression coverage for token exchange and refresh error paths.

What Problem This Solves

OpenAI ChatGPT OAuth token exchange and refresh previously materialized every token response with response.arrayBuffer() before checking response.ok, then read non-OK diagnostics again with response.text(). That made the auth-provider failure path buffer external error bodies before OpenClaw could report a bounded diagnostic.

User Impact

Users who hit ChatGPT OAuth token exchange or refresh failures still get the OpenAI status and diagnostic text, but OpenClaw no longer needs to materialize an unbounded token error body first.

  • Why it matters / User impact: ChatGPT OAuth is part of Codex subscription provider setup and token refresh; bounding the external error body keeps that auth-provider failure path diagnosable without letting a provider/proxy error page define memory usage.

Origin / follow-up

Follows #98496.

  • Gap this fills: fix(tlon): bound error response body reads to prevent OOM #98496 bounded Tlon external error response reads; the OpenAI ChatGPT OAuth token flow had the same class of non-OK external response materialization gap.
  • Consistency: This follows the repository pattern of using readResponseTextLimited at provider boundaries and retaining targeted tests for oversized diagnostic bodies.

Competition / linked PR analysis

No linked issue is being closed. I searched open PRs for this OpenAI ChatGPT OAuth token error-body target and found no existing open PR covering the same follow-up. The diff is limited to extensions/openai/openai-chatgpt-oauth-flow.runtime.ts and its focused runtime test file.

Real behavior proof

  • Behavior or issue addressed: The OpenAI token failure boundary can read a real non-OK OpenAI token response through OpenClaw guarded fetch and the same bounded diagnostic reader used by the patched token flow.
  • Real environment tested: Local OpenClaw checkout on Linux, current PR head 2aaa9d6ffeed9c453515802c785fac47ec567b2d, using OpenClaw fetchWithSsrFGuard with trusted environment proxy mode to reach https://auth.openai.com/oauth/token, then reading the real non-OK response with readResponseTextLimited at the 8 KiB token diagnostic budget.
  • Exact steps or command run after this patch:
pnpm -C /media/vdb/code/ai/aispace/openclaw-worktrees/followup-98496 exec tsx -e '<fetchWithSsrFGuard trusted_env_proxy POST to real OpenAI token endpoint, then readResponseTextLimited(response, 8 * 1024)>'
  • Evidence after fix:
HEAD=2aaa9d6ffeed9c453515802c785fac47ec567b2d
Command: pnpm -C "$TASK_WORKTREE" exec tsx -e <fetchWithSsrFGuard trusted_env_proxy to real OpenAI token endpoint, then readResponseTextLimited>
real endpoint status: 403
real endpoint ok: false
bounded text prefix: {"error":{"code":"unsupported_country_region_territory","message":"Country, region, or territory not supported","param":
bounded text length: 153
release completed: true
  • Observed result after fix: The proof reached the real OpenAI token endpoint, observed a real non-OK provider response, read it through the bounded diagnostic helper under the token error budget, and released the guarded fetch resource.
  • What was not tested: I did not perform a successful browser OAuth login or force OpenAI to return an oversized live error body; OpenAI controls the live error body size. Oversized-body truncation is covered by focused regression tests, not claimed as the real behavior proof.
  • Fix classification: Root cause fix

Review findings addressed

N/A — new follow-up PR, no review findings yet.

Regression Test Plan

  • Target test file: extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts
  • Scenario locked in: Token exchange and refresh failures with oversized diagnostic bodies avoid full arrayBuffer() materialization, avoid response.text(), preserve release handling, and return the 8 KiB bounded failure message.
  • Why this is the smallest reliable guardrail: The test stays at the ChatGPT OAuth token runtime boundary, covering both callers of postTokenForm without touching callback-server login flow, PKCE, token success parsing, or account identity extraction.
pnpm -C /media/vdb/code/ai/aispace/openclaw-worktrees/followup-98496 exec vitest run extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts

Test Files  1 passed (1)
     Tests  12 passed (12)

Merge risk

  • Risk labels considered: auth-provider, security-boundary
  • Risk explanation: The touched file owns OpenAI ChatGPT OAuth token exchange and refresh, and the failure path crosses an external provider boundary.
  • Why acceptable: The change is limited to non-OK diagnostic body materialization; successful token responses still use the existing JSON parsing path, and the live proof exercises a real OpenAI token endpoint failure while regression coverage locks in oversized-body behavior.

Risk / Compatibility

Low risk. Successful token responses still materialize and parse as before. Only non-OK token diagnostics are capped before constructing the failure response, so callers keep the same status-aware failure shape with bounded text.

What was not changed

  • What did NOT change: OAuth authorization URL construction, callback handling, PKCE generation, token success parsing, refresh credential output, account identity extraction, and provider API key behavior are unchanged.
  • Public API / schema / protocol / defaults: unchanged.
  • Channel/provider/session/security behavior outside the token failure diagnostic path: unchanged.
  • Unrelated cleanup, docs, formatting, lockfiles: none.

Root Cause

  • Root cause: The source-of-truth ChatGPT OAuth token request helper, postTokenForm, materialized every OpenAI token response with response.arrayBuffer() before the exchange/refresh code checked response.ok, and the failure branches then read diagnostic text with response.text().
  • Why this is root-cause fix: The token response materialization step now branches on response.ok and routes non-OK provider diagnostics through readResponseTextLimited at the provider boundary before the failure response exists, so the unbounded buffer is removed at its source rather than trimmed later.
  • Maintainer-ready confidence: High; the patch is a focused two-file follow-up, uses the shared provider HTTP helper, has fresh real endpoint proof for the non-OK provider boundary, and has targeted regression coverage for exchange and refresh oversized-body guardrails.
  • Patch quality notes: Patch quality warning considered: this touches an auth-provider/security-boundary file, but it is not a fallback or symptom mask; it removes the unbounded read in the owning token helper and leaves the successful OAuth path unchanged.
  • Architecture / source-of-truth check: extensions/openai/openai-chatgpt-oauth-flow.runtime.ts owns ChatGPT OAuth token exchange and refresh; the change happens before token result classification, credential construction, and account identity extraction, so diagnostic body size is bounded at the external provider boundary.

@clawsweeper

clawsweeper Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close as superseded: the patch is a plausible narrow fix for OpenAI ChatGPT OAuth non-OK token diagnostics, but #98113 is open, clean, proof-positive, maintainer-validated, and covers the same token response boundary more completely.

Root-cause cluster
Relationship: duplicate
Canonical: #98113
Summary: This PR and the canonical PR target the same OpenAI Codex OAuth token-response bounded-read bug; the canonical PR has broader coverage, stronger proof, and maintainer validation.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Canonical path: Close this duplicate branch and continue review or landing on #98113 as the canonical OpenAI Codex OAuth response-boundary fix.

So I’m closing this here and keeping the remaining discussion on #98113.

Review details

Best possible solution:

Close this duplicate branch and continue review or landing on #98113 as the canonical OpenAI Codex OAuth response-boundary fix.

Do we have a high-confidence way to reproduce the issue?

Yes. Current main source shows postTokenForm buffers the token response before response.ok, and the non-OK exchange/refresh branches then read diagnostic text from that response.

Is this the best way to solve the issue?

No as a landing path. The patch is a plausible narrow mitigation, but the best current solution is the open canonical PR because it covers the same first-read boundary plus downstream JSON and error reads.

Security review:

Security review cleared: The diff hardens an existing OAuth token error response path and adds no dependency, workflow, credential, package, or supply-chain surface.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • steipete: Authored and merged the OpenAI provider identity unification that carried this OAuth runtime file, and later posted maintainer validation on the canonical related PR. (role: OpenAI OAuth area refactor contributor and canonical-path validator; confidence: high; commits: 4c33aaa86c16; files: extensions/openai/openai-chatgpt-oauth-flow.runtime.ts)
  • Alix-007: Authored the merged provider JSON response bounding helper used by the canonical broader fix. (role: adjacent bounded provider JSON helper contributor; confidence: high; commits: 2592f8a51a4e; files: src/agents/provider-http-errors.ts, src/plugin-sdk/provider-http.ts)
  • joshavant: Recent path history shows follow-up work on successful provider response reads in the same helper surface involved in the canonical response-boundary fix. (role: adjacent provider response-boundary contributor; confidence: medium; commits: 0a14444924e3; files: src/agents/provider-http-errors.ts, src/plugin-sdk/provider-http.ts)

Codex review notes: model internal, reasoning high; reviewed against 43a7fd383997.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. labels Jul 3, 2026
@clawsweeper clawsweeper Bot closed this Jul 3, 2026
@clawsweeper

clawsweeper Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

🦞✅
ClawSweeper autoclose is complete.

Reason: structured ClawSweeper close marker: close-required (sha=2aaa9d6ffeed9c453515802c785fac47ec567b2d)

Closed:

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

Labels

extensions: openai P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant