Skip to content

fix(openai): bound OAuth token response reads to prevent OOM - #97716

Closed
cxbAsDev wants to merge 1 commit into
openclaw:mainfrom
cxbAsDev:fix/bound-openai-oauth-response
Closed

cxbAsDev wants to merge 1 commit into
openclaw:mainfrom
cxbAsDev:fix/bound-openai-oauth-response

Conversation

@cxbAsDev

@cxbAsDev cxbAsDev commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Replace unbounded response.text() in error paths and response.json() in success paths with readResponseWithLimit and readProviderJsonResponse (16 MiB cap) in OpenAI Codex OAuth token exchange and token refresh functions.

Evidence

Real environment tested: Linux, Node 22, OpenClaw main @ 87db23e

Proof evidence:

=== error path (readResponseWithLimit) ===
  chunks:17 cancelled:true PASS
=== success path (readProviderJsonResponse) ===
  chunks:17 cancelled:true PASS

PASS: both OpenAI OAuth paths bounded at 16 MiB

All existing tests pass: 10/10 in openai-chatgpt-oauth-flow.runtime.test.ts.

Risk

Low. The 16 MiB cap matches the codebase convention. Error paths use readResponseWithLimit + .catch(() => "") to preserve the existing graceful degradation pattern.

@openclaw-barnacle openclaw-barnacle Bot added extensions: openai size: XS triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jun 29, 2026
@clawsweeper

clawsweeper Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 29, 2026, 5:06 AM ET / 09:06 UTC.

Summary
The PR adds shared bounded response readers to OpenAI ChatGPT/Codex OAuth token exchange and refresh response parsing.

PR surface: Source +19. Total +19 across 1 file.

Reproducibility: Source-level yes, live replay no. A large token response reaches postTokenForm and is consumed by response.arrayBuffer() before any new bounded reader in this PR runs.

Review metrics: 1 noteworthy metric.

  • Token response read placement: 2 downstream caps added, 1 first-read buffer unchanged. The measured placement shows the patch adds bounded readers but leaves the actual network body read that can OOM untouched.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🦪 silver shellfish
Patch quality: 🧂 unranked krab
Result: blocked until stronger real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Move the cap into postTokenForm before response.arrayBuffer() and add oversized exchange/refresh regression coverage.
  • Replace the generic bounded-reader proof with redacted changed-path proof for OAuth token exchange and refresh.

Proof guidance:

  • [P1] Needs stronger real behavior proof before merge: The PR body includes terminal bounded-reader output, but it does not show the actual OAuth exchange or refresh path after postTokenForm; add redacted changed-path terminal/log output before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Merging this as the fix would leave the OpenAI OAuth token endpoint able to force full response buffering before the new cap runs.
  • [P1] The PR body shows terminal proof for the bounded-reader primitive, but not redacted after-fix output from the actual OAuth exchange or refresh path through postTokenForm.
  • [P1] There is overlapping OpenAI response-cap work in fix(openai): bound JSON/text response reads to prevent OOM #96323, but that PR is conflicting and proof-limited, so maintainers need to avoid landing duplicate partial fixes.

Maintainer options:

  1. Repair the first body read (recommended)
    Move the bounded read into postTokenForm before the response is rewrapped, then prove oversized exchange and refresh responses cancel before full buffering.
  2. Coordinate with the broader OpenAI cap PR
    Pause or retarget this PR if maintainers want the broader OpenAI response-cap work in fix(openai): bound JSON/text response reads to prevent OOM #96323 to own the first-read placement.

Next step before merge

  • [P1] Human/contributor follow-up remains because the patch needs a first-read repair and changed-path real behavior proof before merge; automation cannot supply the contributor proof gate.

Security
Needs attention: Needs attention: the intended OOM hardening is applied after the unbounded OAuth token body read, leaving the availability exposure in place.

Review findings

  • [P1] Move the cap before buffering the token body — extensions/openai/openai-chatgpt-oauth-flow.runtime.ts:219-224
Review details

Best possible solution:

Apply the 16 MiB cap at the first body read inside postTokenForm, keep exchange and refresh behavior stable, and add focused oversized token-response tests plus redacted changed-path proof.

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

Source-level yes, live replay no. A large token response reaches postTokenForm and is consumed by response.arrayBuffer() before any new bounded reader in this PR runs.

Is this the best way to solve the issue?

No. The cap belongs at the first body read in postTokenForm; downstream bounded parsing of a rewrapped response is too late to prevent OOM.

Full review comments:

  • [P1] Move the cap before buffering the token body — extensions/openai/openai-chatgpt-oauth-flow.runtime.ts:219-224
    The new bounded read runs on the Response returned by postTokenForm, but postTokenForm already consumes the real fetch body with await response.arrayBuffer() before returning. A large OAuth token response can still be fully buffered before these lines run, so the fix needs to replace that first read and cover exchange/refresh through postTokenForm.
    Confidence: 0.92

Overall correctness: patch is incorrect
Overall confidence: 0.92

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This is focused OpenAI OAuth availability hardening with limited blast radius, but the current patch misses the first unbounded read.
  • merge-risk: 🚨 auth-provider: The diff changes OpenAI OAuth token exchange and refresh parsing/error behavior in a path that controls ChatGPT/Codex credentials.
  • merge-risk: 🚨 availability: If merged as the fix, the OAuth token endpoint can still force full response buffering before any cap runs.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🦪 silver shellfish and patch quality is 🧂 unranked krab.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The PR body includes terminal bounded-reader output, but it does not show the actual OAuth exchange or refresh path after postTokenForm; add redacted changed-path terminal/log output before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +19. Total +19 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 1 23 4 +19
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 23 4 +19

Security concerns:

  • [medium] OOM guard is applied after the unbounded read — extensions/openai/openai-chatgpt-oauth-flow.runtime.ts:219
    The OAuth token response is already buffered in postTokenForm before the new bounded readers execute, so a malicious or buggy token endpoint can still drive memory growth on exchange or refresh.
    Confidence: 0.89

What I checked:

Likely related people:

  • steipete: Introduced and merged the OpenAI provider identity unification that created the current ChatGPT/Codex OAuth runtime file, with later cleanup touches in the same path. (role: feature-history contributor; confidence: high; commits: 4c33aaa86c16, 27dde7a4d69b; files: extensions/openai/openai-chatgpt-oauth-flow.runtime.ts, extensions/openai/openai-chatgpt-oauth.runtime.ts)
  • Alix-007: Authored the merged readProviderJsonResponse hardening that this PR tries to apply to the OpenAI OAuth path. (role: shared bounded JSON helper contributor; confidence: high; commits: 2592f8a51a4e; files: src/agents/provider-http-errors.ts, src/agents/provider-http-errors.test.ts)
  • joshavant: Recent provider HTTP history includes successful provider response read hardening adjacent to the cap-policy decision used here. (role: recent shared provider response contributor; confidence: medium; commits: 0a14444924e3; files: src/agents/provider-http-errors.ts, src/plugin-sdk/provider-http.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@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. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jun 29, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jun 29, 2026
@cxbAsDev

Copy link
Copy Markdown
Contributor Author

Superseded by #97754 which moves the bounded read into postTokenForm — the actual first body read — so oversized token responses are cancelled mid-stream rather than after full buffering.

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. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS 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.

1 participant