Skip to content

fix(extensions/openai): bound ChatGPT OAuth token response reads to prevent OOM - #101075

Closed
cxbAsDev wants to merge 2 commits into
openclaw:mainfrom
cxbAsDev:fix/openai-chatgpt-oauth-json-response-bound
Closed

cxbAsDev wants to merge 2 commits into
openclaw:mainfrom
cxbAsDev:fix/openai-chatgpt-oauth-json-response-bound

Conversation

@cxbAsDev

@cxbAsDev cxbAsDev commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

postTokenForm in the OpenAI Codex OAuth flow buffered the entire token exchange / refresh response into an arrayBuffer before re-wrapping it as a Response. A malicious or misbehaving token endpoint could stream an unbounded JSON payload and force the CLI to buffer it all, leading to OOM.

Why This Change Was Made

Use the repo's existing bounded provider HTTP helpers (readProviderJsonResponse / readResponseTextLimited) to cap the successful token JSON body at 1 MiB and error diagnostic bodies at 8 KiB. This mirrors the already-landed fixes for OpenRouter OAuth (#98645), Google OAuth (#97587), and QA bus JSON responses (#99169).

User Impact

openclaw auth openai (ChatGPT OAuth) is protected against runaway token endpoint responses.

Evidence

  • Regression test added in extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts: it feeds a >1 MiB JSON payload to exchangeAuthorizationCode and asserts the result is a bounded failed response.
  • Added a real behavior proof script at scripts/proof/openai-chatgpt-oauth-json-bound.mts that serves an 8 MiB JSON body from a local HTTP server and verifies readProviderJsonResponse rejects at the 1 MiB cap before buffering everything.
  • Local verification:
    • node scripts/run-vitest.mjs extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts passes.
    • OPENCLAW_OXLINT_SKIP_PREPARE=1 node scripts/run-oxlint.mjs extensions/openai/openai-chatgpt-oauth-flow.runtime.ts extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts scripts/proof/openai-chatgpt-oauth-json-bound.mts passes.
    • node --import tsx scripts/proof/openai-chatgpt-oauth-json-bound.mts passes.

Proof output

=== Proof: OpenAI ChatGPT OAuth token JSON bound ===

Local token server listening on port 43211

Normal token response: access_token=a...

Oversized token body rejected: OpenAI Codex token exchange: JSON response exceeds 1048576 bytes
Duration: 17.4 ms
RSS delta: 11.6 MB

PASS: OAuth token JSON read is bounded; oversized bodies fail fast without OOM.

@cxbAsDev
cxbAsDev force-pushed the fix/openai-chatgpt-oauth-json-response-bound branch from 1741b01 to 61991e7 Compare July 6, 2026 17:59
@clawsweeper

clawsweeper Bot commented Jul 6, 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 same OpenAI ChatGPT OAuth token-response OOM work is already owned by #98113, which is proof-positive, broader, and has maintainer validation, while this branch still has a type-checking defect and weaker path-level proof.

Root-cause cluster
Relationship: duplicate
Canonical: #98113
Summary: This PR and the canonical PR target the same OpenAI ChatGPT OAuth token-response OOM class; the canonical PR is the viable landing path because it is open, proof-positive, broader, and maintainer-validated.

Members:

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

Canonical path: Close this branch and keep #98113 as the canonical landing path for the OpenAI ChatGPT OAuth response-boundary fix.

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

Review details

Best possible solution:

Close this branch and keep #98113 as the canonical landing path for the OpenAI ChatGPT OAuth response-boundary fix.

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

Yes. Source inspection on current main shows postTokenForm still uses an unbounded response.arrayBuffer() on the guarded token response before release; I did not run a live endpoint repro for this read-only cleanup review.

Is this the best way to solve the issue?

No as a landing path. The branch is a plausible mitigation, but #98113 is the better solution because it covers the first-read boundary plus downstream exchange/refresh JSON and error reads with stronger proof.

Security review:

Security review cleared: Cleared: the diff hardens an OAuth token response read and adds no dependency, workflow, credential, package, lockfile, or supply-chain execution surface.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • steipete: GitHub path history shows the OpenAI provider unification commit carried the current OpenAI OAuth runtime file, and the canonical related PR has maintainer validation from this person. (role: OpenAI OAuth area refactor contributor and canonical-path validator; confidence: high; commits: 4c33aaa86c16; files: extensions/openai/openai-chatgpt-oauth-flow.runtime.ts, extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts)
  • Alix-007: GitHub commit history shows this person authored the merged provider JSON response bounding helper that the OpenAI OAuth fixes reuse. (role: adjacent bounded JSON helper contributor; confidence: high; commits: 2592f8a51a4e; files: src/agents/provider-http-errors.ts)
  • joshavant: GitHub history shows follow-up work on successful provider response reads in the same helper surface involved in this response-boundary pattern. (role: adjacent provider response-boundary contributor; confidence: medium; commits: 0a14444924e3; files: src/agents/provider-http-errors.ts, src/plugin-sdk/provider-http.ts)
  • wangmiao0668000666: GitHub history shows recent Google OAuth body-cap work using the same bounded response-read pattern relevant to this OpenAI OAuth change family. (role: sibling OAuth hardening contributor; confidence: medium; commits: a6aaba76ac66; files: extensions/google/oauth.http.ts)

Codex review notes: model internal, reasoning high; reviewed against 2f7299eaa575.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. labels Jul 6, 2026
@cxbAsDev
cxbAsDev force-pushed the fix/openai-chatgpt-oauth-json-response-bound branch from 61991e7 to edc6d9a Compare July 6, 2026 19:05
@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: S and removed size: XS labels Jul 6, 2026
@cxbAsDev

cxbAsDev commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review\n\nAdded a real behavior proof script and updated the PR body with the proof output. Branch rebased onto latest upstream/main.

@steipete

steipete commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Superseded by #99479, landed as a89fe705.

The landed fix bounds the single shared upstream postTokenForm body read at 1 MiB, so both authorization-code exchange and refresh are protected before caller parsing. It preserves the existing Response bytes, status, headers, JSON parsing, and error-text behavior, and its real loopback tests prove success, overflow cancellation, and guarded-fetch release. This PR instead parses and reserializes JSON inside the transport helper, which changes response-byte semantics and moves caller policy into the wrong layer.

Thanks @cxbAsDev for addressing this class of issue. Closing in favor of the landed canonical implementation.

@steipete steipete closed this Jul 6, 2026
@cxbAsDev
cxbAsDev deleted the fix/openai-chatgpt-oauth-json-response-bound branch July 15, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: openai merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. scripts Repository scripts size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants