Skip to content

fix(openai): bound Codex OAuth JSON and error response reads to prevent OOM - #98113

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

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

Conversation

@solodmd

@solodmd solodmd commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

postTokenForm in extensions/openai/openai-chatgpt-oauth-flow.runtime.ts
reads the OpenAI Codex OAuth token endpoint response body with an unbounded
await response.arrayBuffer(). A compromised or hijacked OAuth endpoint can
stream an arbitrarily large body and force the runtime to buffer the entire
payload — an OOM/DoS vector.

Changes

  • extensions/openai/openai-chatgpt-oauth-flow.runtime.ts — import
    readResponseWithLimit from openclaw/plugin-sdk/response-limit-runtime;
    replace await response.arrayBuffer() with readResponseWithLimit at 16
    MiB cap inside postTokenForm, before release(). Also replace
    response.json() and response.text() in exchangeAuthorizationCode and
    refreshAccessToken with readProviderJsonResponse and
    readResponseTextLimited for defense-in-depth. Use a small diagnostic cap
    (4 KB) for failed-exchange and failed-refresh error text reads.
  • extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts — 5 new tests
    covering oversized exchange, oversized refresh, malformed JSON, and error
    body bounding for both exchange and refresh paths.

Evidence

Real behavior proof

The proof script exercises the exact bounded read that postTokenForm
performs on real loopback HTTP wire — readResponseWithLimit at 16 MiB cap
before release() — plus the readProviderJsonResponse call that
exchangeAuthorizationCode and refreshAccessToken use on the bounded clone,
each with its own distinct label:

$ node --import tsx proof-openai-oauth-bound.mts
=== Test 1: postTokenForm bounded read rejects oversized body ===
  chunks sent: 17
  readResponseWithLimit threw: OpenAI Codex token response exceeds 16777216 bytes
  PASS: oversized body rejected before release()

=== Test 2: Valid JSON body passes through bounded read ===
  parsed access_token: tok_abc
  PASS: valid JSON within limit

=== Test 3: exchange path rejects oversized response ===
  error: openai-codex-token-exchange: JSON response exceeds 16777216 bytes
  PASS: exchange path rejects oversized body

=== Test 4: refresh path rejects oversized response ===
  error: openai-codex-token-refresh: JSON response exceeds 16777216 bytes
  PASS: refresh path rejects oversized body

=== Test 5: exchange path rejects malformed JSON ===
  error: openai-codex-token-exchange: malformed JSON response
  PASS: malformed JSON rejected at exchange path

PASS: all OpenAI Codex OAuth path-level bounding paths verified

Summary of what each level proves:
  postTokenForm:  readResponseWithLimit at 16 MiB, before release()
  exchange:       readProviderJsonResponse (label: openai-codex-token-exchange)
  refresh:        readProviderJsonResponse (label: openai-codex-token-refresh)

Each path uses its own readProviderJsonResponse label, visible in the error
message when the oversized body is rejected.

A loopback HTTP server streams an oversized body (18 MiB over 16 MiB cap)
and valid/malformed JSON responses. readResponseWithLimit detects overflow
at 16 MiB and cancels the stream; readProviderJsonResponse validates JSON
parsing on the bounded clone.

Tests

  • 5 new Vitest cases: oversized token exchange (postTokenForm level),
    oversized token refresh, malformed JSON rejection, capped error body on
    failed exchange, and capped error body on failed refresh.
  • 10 existing Vitest cases unchanged — token exchange, token refresh,
    timeout, cancellation, unsafe lifetime rejection all pass.
  • pnpm test extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts →
    15 passed (15)

@clawsweeper

clawsweeper Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 30, 2026, 7:08 AM ET / 11:08 UTC.

Summary
The PR bounds OpenAI Codex OAuth token response, success JSON, and error-text reads and adds regression tests for oversized and malformed token responses.

PR surface: Source +33, Tests +107. Total +140 across 2 files.

Reproducibility: yes. Current main and v2026.6.10 both show postTokenForm using raw response.arrayBuffer() before release(), so an oversized token endpoint body can be buffered before downstream parsing sees it.

Review metrics: none identified.

Merge readiness
Overall: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

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

Next step before merge

  • No ClawSweeper repair job is needed because the patch has no actionable defect; the remaining path is normal maintainer review and CI gating.

Security
Cleared: The diff reduces OAuth token-response memory exposure and adds no dependency, workflow, credential, package, or supply-chain surface.

Review details

Best possible solution:

Land the bounded OpenAI OAuth response-read change after ordinary maintainer and CI gates, keeping the cap at the token fetch boundary and retaining the focused regression coverage.

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

Yes. Current main and v2026.6.10 both show postTokenForm using raw response.arrayBuffer() before release(), so an oversized token endpoint body can be buffered before downstream parsing sees it.

Is this the best way to solve the issue?

Yes. Capping the guarded token response before release() is the right owner-boundary fix; capping only response.json() or diagnostic text would leave the original body-buffering path exposed.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This is a focused OpenAI OAuth OOM/DoS hardening bugfix with limited bundled-plugin blast radius.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes after-fix terminal proof from a loopback HTTP script that exercises oversized token-body rejection, bounded exchange/refresh JSON parsing, malformed JSON handling, and the focused test result.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes after-fix terminal proof from a loopback HTTP script that exercises oversized token-body rejection, bounded exchange/refresh JSON parsing, malformed JSON handling, and the focused test result.
Evidence reviewed

PR surface:

Source +33, Tests +107. Total +140 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 39 6 +33
Tests 1 107 0 +107
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 146 6 +140

What I checked:

Likely related people:

  • steipete: GitHub path history shows PR refactor: unify OpenAI provider identity #88451 authored and merged by steipete carried the current OpenAI OAuth flow file and its postTokenForm shape. (role: OpenAI OAuth area refactor contributor; confidence: high; commits: 4c33aaa86c16; files: extensions/openai/openai-chatgpt-oauth-flow.runtime.ts)
  • Alix-007: GitHub history shows Alix-007 authored the merged provider JSON response bounding helper that this PR reuses for token success bodies. (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 used by this patch. (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 readResponseWithLimit and Response rewrap pattern this PR mirrors. (role: sibling OAuth hardening contributor; confidence: medium; commits: a6aaba76ac66; files: extensions/google/oauth.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: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jun 30, 2026
@solodmd
solodmd force-pushed the fix/openai-bound-oauth-json-response-reads branch from f68614e to 11c3526 Compare June 30, 2026 10:16
@solodmd

solodmd commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 30, 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.

@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Jun 30, 2026
@solodmd
solodmd force-pushed the fix/openai-bound-oauth-json-response-reads branch from 11c3526 to 259deee Compare June 30, 2026 10:29
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jun 30, 2026
@solodmd

solodmd commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 30, 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.

@solodmd
solodmd force-pushed the fix/openai-bound-oauth-json-response-reads branch 2 times, most recently from 9072d9c to cf4f4c7 Compare June 30, 2026 10:51
exchangeAuthorizationCode and refreshAccessToken called response.json()
and response.text() without size limits on the token endpoint response.
Replace with readProviderJsonResponse (16 MiB capped JSON parse) and
readResponseTextLimited (16 MiB capped text read).

Co-Authored-By: Claude <[email protected]>
@solodmd
solodmd force-pushed the fix/openai-bound-oauth-json-response-reads branch from cf4f4c7 to d59938a Compare June 30, 2026 10:52
@solodmd

solodmd commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 30, 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.

@solodmd

solodmd commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jun 30, 2026
@clawsweeper clawsweeper Bot added status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Jun 30, 2026
@steipete steipete self-assigned this Jun 30, 2026
@steipete

steipete commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Maintainer landing validation is complete for d59938a5e77620318c2e4c0cb9fa790448dc0671.

  • Best-fix review: the production patch caps the original SSRF-guarded OAuth token response before release, then uses bounded diagnostic and JSON readers. Capping only downstream parsing would leave the initial unbounded allocation intact.
  • Dependency contract: direct sibling Codex inspection at db887d03e1f907467e33271572dffb73bceecd6b confirmed the authorization-code form POST and success/error reads in codex-rs/login/src/server.rs:732-805, plus the JSON refresh POST and success/error reads in codex-rs/login/src/auth/manager.rs:1332-1371.
  • Security: Codex Security diff scan 4795a481-373c-4c5e-9be2-e8bce8c3b086 completed with zero findings and complete coverage of both changed files.
  • Focused proof: node scripts/run-vitest.mjs extensions/openai/openai-chatgpt-oauth-flow.runtime.test.ts passed 19/19; git diff --check passed.
  • Remote gate: Blacksmith/Testbox tbx_01kweafn7y2wmkh8nt53st42mn passed the exact-base reconstructed pnpm check:changed gate, including extension production/test typechecks, lint, architecture/import-cycle, dependency, package, and database-first guards (Actions run 28502161445).
  • Fresh review: autoreview reported no accepted/actionable findings (correctness confidence 0.96).

A maintainer-local test-only patch adds exact oversized/malformed/bodyless/cancellation/release/diagnostic-cap assertions. It is intentionally not on the contributor branch yet and is ready for the manager-controlled serialized prepare step. No real OAuth credentials were used or exposed; the contributor's loopback proof and hostile-stream tests cover the response boundary.

@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 adds a second-stage 16 MiB reader after the shared response was already materialized, leaving duplicate policy and a larger memory ceiling.

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

@steipete steipete closed this Jul 6, 2026
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: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. 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.

2 participants