Skip to content

fix(openai): bound OAuth token response read at first body read in postTokenForm to prevent OOM - #97754

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

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

Conversation

@cxbAsDev

@cxbAsDev cxbAsDev commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

The original fix (#97716) applied bounded response readers downstream in exchangeAuthorizationCode and refreshAccessToken, but those operate on a Response already re-wrapped after postTokenForm consumed the entire body with await response.arrayBuffer(). The actual OOM vector was the unbounded first read inside postTokenForm.

This PR moves the 16 MiB bounded read (readResponseWithLimit) into postTokenForm — before the body is buffered — so oversized OAuth token responses are cancelled mid-stream rather than fully buffered. Downstream bounded readers are retained for defense-in-depth.

Evidence

Environment: Linux, Node 22, OpenClaw main @ 0ce10d7

Terminal output — all 12 tests pass, including 2 oversized-response regression tests:

$ pnpm test extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts --run --reporter=verbose

 ✓ OpenAI Codex OAuth flow > cancels provider login before opening the OAuth flow 91ms
 ✓ OpenAI Codex OAuth flow > does not open the OAuth flow after cancellation during setup 41ms
 ✓ OpenAI Codex OAuth flow > waits for Node OAuth runtime before creating an authorization flow 5ms
 ✓ OpenAI Codex OAuth flow > builds callback redirect URIs from the configured loopback host 1ms
 ✓ OpenAI Codex OAuth flow > rejects non-loopback callback bind hosts 1ms
 ✓ OpenAI Codex OAuth flow > times out token exchange requests 5ms
 ✓ OpenAI Codex OAuth flow > cancels token exchange requests with the caller signal 1ms
 ✓ OpenAI Codex OAuth flow > rejects unsafe token exchange lifetimes 16ms
 ✓ OpenAI Codex OAuth flow > times out token refresh requests 1ms
 ✓ OpenAI Codex OAuth flow > rejects non-positive token refresh lifetimes 1ms
 ✓ OpenAI Codex OAuth flow > rejects oversized token exchange responses 37ms
 ✓ OpenAI Codex OAuth flow > rejects oversized token refresh responses 36ms

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

The oversized tests create 18 MiB ReadableStream responses (cap is 16 MiB) and verify the bounded reader in postTokenForm cancels the stream and the error propagates through exchangeAuthorizationCode / refreshAccessToken as a formatted token failure.

Risk

Low. The 16 MiB cap matches the codebase convention. The cap is applied at the first body read (inside postTokenForm), and downstream bounded readers provide defense-in-depth on the re-wrapped response.

🤖 Generated with Claude Code

@clawsweeper

clawsweeper Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 29, 2026, 10:33 AM ET / 14:33 UTC.

Summary
The PR bounds the OpenAI OAuth token response at the first postTokenForm body read and adds oversized token exchange/refresh regression tests.

PR surface: Source +23, Tests +59. Total +82 across 2 files.

Reproducibility: yes. from source inspection: current main and v2026.6.10 read the OAuth token response with response.arrayBuffer() in postTokenForm before returning a rewrapped response to downstream parsing. I did not run a live token endpoint reproduction in this read-only review.

Review metrics: 1 noteworthy metric.

  • OAuth token read placement: 1 first-read cap added; 2 downstream bounded reads retained. Placement matters because current main buffers the token response in postTokenForm before later response parsing can enforce a cap.

Root-cause cluster
Relationship: canonical
Canonical: #97754
Summary: This PR is the current narrow canonical follow-up for the OpenAI OAuth token-response first-read OOM issue; the predecessor is closed and the broader overlapping PR is not a safe superseding target yet.

Members:

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

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🦞 diamond lobster
Result: blocked until real behavior proof from a real setup is added.

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

Rank-up moves:

  • [P1] Add redacted terminal/log/live output for normal OAuth token exchange and refresh.
  • [P1] Add redacted terminal/log/live output for oversized OAuth token exchange and refresh rejection.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body reports mocked test output only, not redacted after-fix real behavior proof for normal and oversized OAuth exchange/refresh; after adding proof to the PR body, a fresh ClawSweeper review should run automatically or can be requested with @clawsweeper re-review. 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] The PR body still has mocked regression-test output only; it lacks redacted live output, terminal proof, or logs for normal and oversized OpenAI OAuth exchange and refresh through the changed postTokenForm path.
  • [P1] The new 16 MiB cap intentionally turns oversized OAuth token responses from unbounded buffering into auth-provider failures; that is likely the right safety behavior, but it changes credential-flow behavior and needs maintainer acceptance.

Maintainer options:

  1. Prove the OAuth token paths (recommended)
    Ask the contributor to add redacted terminal, live output, or logs showing normal and oversized token exchange and refresh through the changed postTokenForm path.
  2. Accept source-level evidence
    Maintainers can deliberately accept the source-level and mocked-test evidence while owning the missing real setup proof for the auth-provider cap.
  3. Coordinate with the broader sweep
    If maintainers want one OpenAI-wide response-cap landing path, pause this PR until the broader overlapping PR is clean, proof-positive, and clearly owns the OAuth first-read fix.

Next step before merge

  • [P1] No automated repair is needed; the remaining action is contributor proof plus maintainer acceptance of the auth-provider cap behavior.

Security
Cleared: The diff adds no dependency, workflow, permission, package, credential-storage, or supply-chain surface; it hardens an existing auth-provider response read.

Review details

Best possible solution:

Land the narrow first-read cap after changed-path real behavior proof covers normal and oversized OAuth exchange and refresh, while keeping broader OpenAI response-size sweeps separate unless maintainers choose one unified landing path.

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

Yes from source inspection: current main and v2026.6.10 read the OAuth token response with response.arrayBuffer() in postTokenForm before returning a rewrapped response to downstream parsing. I did not run a live token endpoint reproduction in this read-only review.

Is this the best way to solve the issue?

Yes on code shape: applying readResponseWithLimit at the first body read is the narrow provider-local fix and reuses the established shared helper. It is not merge-ready until changed-path real behavior proof is added or maintainers explicitly accept the proof gap.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: The PR targets focused OpenAI OAuth availability/auth-provider hardening with limited provider-local blast radius.
  • merge-risk: 🚨 auth-provider: The diff changes the OpenAI OAuth token exchange and refresh paths that acquire and refresh provider credentials.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦞 diamond lobster.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body reports mocked test output only, not redacted after-fix real behavior proof for normal and oversized OAuth exchange/refresh; after adding proof to the PR body, a fresh ClawSweeper review should run automatically or can be requested with @clawsweeper re-review. 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 +23, Tests +59. Total +82 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 29 6 +23
Tests 1 59 0 +59
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 88 6 +82

What I checked:

Likely related people:

  • cxbAsDev: Current blame and file history for the OpenAI OAuth plugin runtime point to commit 615558f on main; this person also authored prior merged bounded-response work. (role: recent area contributor; confidence: high; commits: 615558f6fb3f, 14367e456f09; files: extensions/openai/openai-chatgpt-oauth-flow.runtime.ts, src/infra/provider-usage.fetch.claude.ts)
  • vincentkoc: Live PR metadata shows this person merged recent bounded provider-response PRs that set adjacent precedent for 16 MiB OAuth/provider response caps. (role: recent merger of related provider hardening; confidence: medium; commits: 14367e456f09, db255d677fcc, 936455b4e18d; files: src/infra/provider-usage.fetch.claude.ts, extensions/openai/embedding-batch.ts, extensions/openai/realtime-provider-shared.ts)
  • hugenshen: Authored merged OpenAI bounded success-response work for embedding batch and realtime paths that shares the same helper and cap policy. (role: adjacent OpenAI provider hardening contributor; confidence: medium; commits: db255d677fcc; files: extensions/openai/embedding-batch.ts, extensions/openai/realtime-provider-shared.ts)
  • Wynne668: Recently touched src/agents/provider-http-errors.ts, the shared provider HTTP helper surface used by the PR's downstream JSON reader. (role: recent shared helper contributor; confidence: low; commits: ac5af250154d; files: src/agents/provider-http-errors.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: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. 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 Jun 29, 2026
@cxbAsDev

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

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: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. 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.

1 participant