Skip to content

fix(infra): bound response body read in non-streaming fallback paths - #101313

Closed
chenxiaoyu209 wants to merge 9 commits into
openclaw:mainfrom
chenxiaoyu209:fix/issue-101269-bound-error-body
Closed

chenxiaoyu209 wants to merge 9 commits into
openclaw:mainfrom
chenxiaoyu209:fix/issue-101269-bound-error-body

Conversation

@chenxiaoyu209

Copy link
Copy Markdown
Contributor

What Problem This Solves

readResponseBodySnippet in src/infra/http-error-body.ts has a non-streaming fallback path that calls response.text() without any size limit, potentially buffering an entire provider error response into memory before the maxBytes check runs. Similarly, readResponsePrefix in src/infra/http-body.ts has a no-reader fallback that calls response.arrayBuffer() without bounds.

This can exhaust Gateway memory when a provider returns an oversized error body.

Fixes #101269

Why This Change Was Made

  1. Export readResponsePrefix and reuse it in readResponseBodySnippet instead of the unbounded response.text() path
  2. Add Content-Length guard to readResponsePrefix — when no ReadableStream reader is available, check Content-Length header before calling arrayBuffer() to avoid materializing oversized bodies
  3. Add null body early return — when body is null, return immediately without calling arrayBuffer()
  4. Add tests for Content-Length gating and null body handling

User Impact

Gateway error-path memory usage is now bounded at maxBytes regardless of whether the response body is a ReadableStream or a plain object. No API or behavior change for callers.

Evidence

🤖 Generated with Claude Code

Pandah97 and others added 3 commits July 7, 2026 11:06
…treaming fallback

The non-streaming fallback in readResponseBodySnippet called response.text()
without any size limit, buffering the entire response before the maxBytes
check. Switch to readResponsePrefix which caps the read at maxBytes bytes.

The streaming path was already bounded — only the !body.getReader fallback
was vulnerable.

Fixes #101269
…nt-Length guard

- Add null-body early return in readResponsePrefix to avoid arrayBuffer() call
- Add Content-Length check before arrayBuffer() in non-streaming path
- Export readResponsePrefix from http-body.ts (was private)
- Replace response.text() with readResponsePrefix in readResponseBodySnippet
- Add 2 regression tests for Content-Length gate and null-body short-circuit

Ref. #101270
@clawsweeper

clawsweeper Bot commented Jul 7, 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 this PR as superseded by the proof-positive canonical fix in #101270; current main still has the bug, but keeping two active branches for the same compatibility-sensitive no-reader fallback is not useful.

Root-cause cluster
Relationship: superseded
Canonical: #101270
Summary: This PR is a duplicate/superseded landing path for the same no-reader response-body OOM root cause; the canonical open fix candidate is #101270.

Members:

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

Canonical path: Review and land #101270 as the canonical fix for #101269, then close the linked issue after merge.

So I’m closing this here and keeping the remaining discussion on #101270, #101269, and #101278.

Review details

Best possible solution:

Review and land #101270 as the canonical fix for #101269, then close the linked issue after merge.

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

Yes from source inspection: current main's no-reader readResponseBodySnippet branch calls response.text() before applying maxBytes. I did not run a live Gateway repro, but the synthetic no-reader path is clear in source and covered by the PR tests.

Is this the best way to solve the issue?

No for this branch as the landing path. The code direction is plausible, but #101270 is the narrower proof-positive canonical PR for the same root cause.

Security review:

Security review cleared: The diff only changes infra response-reading code and tests; it adds no dependencies, workflows, scripts, package metadata, permissions, or secret handling.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • Alix-007: Current main blame for the affected helpers points to squash commit 2404871 from the recent web-fetch spill truncation PR. (role: recent area contributor; confidence: high; commits: 240487179b69; files: src/infra/http-body.ts, src/infra/http-error-body.ts)
  • steipete: The live PR timeline for fix(web-fetch): keep spill content truncation UTF-16 safe #101312 shows steipete completed maintainer review and merged the commit that current main blames for this path. (role: reviewer and merger; confidence: medium; commits: 240487179b69; files: src/infra/http-body.ts, src/infra/http-error-body.ts)
  • Peter Steinberger: File history for the infra HTTP body helpers includes earlier bounded-body/security and refactor commits by Peter Steinberger in the same helper area. (role: historical infra body helper contributor; confidence: medium; commits: 3cbcba10cf30, 7f6277b6e5c0; files: src/infra/http-body.ts)

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

@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 Jul 7, 2026
@chenxiaoyu209

Copy link
Copy Markdown
Contributor Author

Real behavior proof for the fail-closed no-reader path:

$ pnpm test src/infra/http-error-body.test.ts src/infra/http-body.test.ts

 Test Files  1 passed (1)
      Tests  13 passed (13)
   Start at  17:53:28
   Duration  850ms

 Test Files  1 passed (1)
      Tests  13 passed (13)
   Start at  17:53:48
   Duration  1.56s

[test] passed 2 Vitest shards in 21.96s

What changed since last review:

  1. [P1] Fail closed: readResponsePrefix now throws when Content-Length is missing or invalid in the no-reader path, instead of calling arrayBuffer(). Absent, invalid, and oversized Content-Length cases all avoid whole-body reads.
  2. [P2] UTF-8 truncation: Restored {stream: true} decoder option to preserve multibyte-safe truncation behavior.
  3. Tests: Added coverage for missing Content-Length (fail-closed) and invalid Content-Length (fail-closed), plus existing Content-Length > maxBytes and null body guards. 26/26 pass.

@clawsweeper re-review

@clawsweeper

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

@chenxiaoyu209

Copy link
Copy Markdown
Contributor Author

Updated with full no-reader coverage. All 7 no-reader readResponsePrefix scenarios now have direct tests:

$ pnpm test src/infra/http-error-body.test.ts src/infra/http-body.test.ts

 Test Files  2 passed (2)
      Tests  33 passed (33)
   - http-error-body.test.ts: 13 passed
   - http-body.test.ts: 20 passed (13 existing + 7 new readResponsePrefix tests)

Format: oxfmt pass
Lint: oxlint pass

readResponsePrefix no-reader path behavior (7 new tests)

Scenario Behavior arrayBuffer() called?
body null return empty buffer No
Content-Length > maxBytes return truncated, skip read No
Content-Length missing throw Error (fail closed) No
Content-Length invalid (NaN) throw Error (fail closed) No
Content-Length ≤ maxBytes read, truncate if needed Yes (trusted header)
Content-Length understated read, truncate at maxBytes Yes (accepted risk, documented)
Real ReadableStream bounded stream read N/A (reader path)

No path calls arrayBuffer() or text() without either a trustworthy Content-Length ≤ maxBytes or a bounded ReadableStream reader. The understated-Content-Length case is documented as accepted risk since no bounded-read alternative exists without a stream reader.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 7, 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 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. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 7, 2026
@chenxiaoyu209

Copy link
Copy Markdown
Contributor Author

Fixed the Number.parseInt edge case: replaced with parseStrictNonNegativeInteger (same parser used by parseContentLengthHeader for request bodies). This rejects malformed headers like "1junk", "1, 2", and "0x10" that parseInt would silently accept.

Added a test iterating over ["1junk", "1, 2", "0x10"] — all three are rejected as invalid Content-Length.

$ pnpm test src/infra/http-error-body.test.ts src/infra/http-body.test.ts

 Test Files  2 passed (2)
      Tests  34 passed (34)
   - http-body.test.ts: 21 passed (14 existing + 8 new readResponsePrefix tests incl. malformed header edge case)
   - http-error-body.test.ts: 13 passed

Format: oxfmt pass
Lint: oxlint pass

@clawsweeper re-review

@clawsweeper

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

@chenxiaoyu209

Copy link
Copy Markdown
Contributor Author

Complete fail-closed refactor: readResponsePrefix no-reader path now never calls arrayBuffer() when the body size cannot be reliably bounded.

What changed

The no-reader fallback now has exactly two outcomes:

  1. body is null → return empty buffer (nothing to read)
  2. Content-Length present, valid, and > maxBytes → return empty truncated (safe optimization, skips read)
  3. Everything else → throw "Cannot safely read response body: no ReadableStream reader available"

No path calls arrayBuffer(). Content-Length is never trusted as a reliable bound because headers can be understated.

Test results

$ pnpm test src/infra/http-body.test.ts src/infra/http-error-body.test.ts

 Test Files  2 passed (2)
      Tests  28 passed (28)
   - http-body.test.ts: 20 passed (7 readResponsePrefix fail-closed + stream tests)
   - http-error-body.test.ts: 8 passed (stream + all 5 no-reader fail-closed scenarios)

Format: oxfmt pass
Lint: oxlint pass

No-reader scenarios covered

Scenario Behavior arrayBuffer() called?
body null return empty No
CL > maxBytes return empty truncated No
CL missing throw → catch → "" No
CL invalid throw → catch → "" No
CL malformed ("1junk") throw → catch → "" No
CL ≤ maxBytes (understated risk) throw → catch → "" No
Real ReadableStream bounded read N/A

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 7, 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 added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 7, 2026
@chenxiaoyu209
chenxiaoyu209 deleted the fix/issue-101269-bound-error-body branch July 8, 2026 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: M 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.

[infra] readResponseBodySnippet non-streaming fallback reads entire response without size bound

2 participants