Skip to content

fix(infra): enforce maxBytes in readResponseBodySnippet non-streaming fallback - #101278

Closed
lsr911 wants to merge 2 commits into
openclaw:mainfrom
lsr911:fix/response-body-snippet-bound
Closed

lsr911 wants to merge 2 commits into
openclaw:mainfrom
lsr911:fix/response-body-snippet-bound

Conversation

@lsr911

@lsr911 lsr911 commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

readResponseBodySnippet in src/infra/http-error-body.ts has a non-streaming fallback path that previously called response.text() which would buffer the entire body before maxBytes could be applied — creating an OOM risk for large error responses on non-streaming transports.

Why This Change Was Made

Replace the whole-body fallback with a fail-closed approach: when the response body has no getReader (non-streaming or mocked responses), return `` immediately instead of calling response.text(). The streaming path continues to enforce `maxBytes` via the bounded reader loop.

This follows the ClawSweeper recommendation: "use a bounded stream source or fail closed before calling whole-body fallbacks."

Evidence

Real behavior proof (8/8 PASS)

node --import tsx test/_proof_response_body_snippet_bound.mts

PASS  non-streaming fail-closed: returns empty string :: len=0
PASS  non-streaming small: also returns empty (fail-closed) :: got=""
PASS  streaming: snippet ≤ maxChars :: len=200 max=500
PASS  streaming: byte length ≤ maxBytes :: bytes=200 max=200
PASS  streaming small: passes through :: got="world"
PASS  streaming multibyte: bytes ≤ maxBytes :: bytes=15 max=15
PASS  streaming multibyte: chars ≤ maxChars :: chars=5 max=100
PASS  streaming multibyte: no replacement char :: snippet="啊啊啊啊啊"

[proof] 8 PASS, 0 FAIL

Code change

The non-streaming fallback no longer calls response.text():

if (!body || typeof body.getReader !== "function") {
    // Fail closed: return empty rather than risk OOM on large bodies.
    return "";
}

Issue

Fixes #101269


🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle Bot added size: S triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jul 7, 2026
@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 as superseded: the same root issue is now covered by #101270, which updates the shared helper and canonical Vitest coverage with positive proof, while this PR is conflicting and narrower.

Canonical path: #101269 is still open and describes the same no-reader readResponseBodySnippet memory-bound problem; it should remain open until a canonical fix PR merges.

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

Review details

Best possible solution:

Use #101270 as the landing path, or ask for one consolidated branch that changes both readResponseBodySnippet and shared readResponsePrefix with canonical Vitest coverage.

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

Yes. Source inspection gives a high-confidence path: pass a no-reader Response-like object with a large text() result to readResponseBodySnippet, and current main buffers the full text before applying maxBytes.

Is this the best way to solve the issue?

No. The fail-closed direction is right, but this branch is not the best landing path because #101270 covers the shared helper and canonical tests with positive proof while this branch is conflicting and narrower.

Security review:

Security review cleared: No dependency, workflow, credential, permission, or supply-chain regression was found in this diff; the intended behavior reduces an availability risk.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • Pandah97: Merged PR metadata for fix(openai): bound Codex OAuth token response body reads with readResponseWithLimit #99479 ties this account to the shared bounded HTTP body reader area, and the same account authored the stronger sibling PR for this root cause. (role: introduced current shared helper behavior and authored canonical sibling fix; confidence: high; commits: a89fe705b8b8, 71496688e878, f928ff327774; files: src/infra/http-body.ts, src/infra/http-error-body.ts, src/infra/http-error-body.test.ts)
  • Pick-cat: This account authored the merged PR that added current byte-limit returned-snippet behavior and tests in the same readResponseBodySnippet fallback area. (role: recent adjacent contributor; confidence: high; commits: 7c9c2ef0c369; files: src/infra/http-error-body.ts, src/infra/http-error-body.test.ts)
  • zenglingbiao: This account authored the merged adjacent web-shared fail-closed fallback change that provides the closest implementation precedent. (role: adjacent fail-closed fallback contributor; confidence: medium; commits: db25e9ec5218; files: src/agents/tools/web-shared.ts, src/agents/tools/web-shared.test.ts)
  • steipete: This account merged the shared bounded-reader PR and the adjacent fail-closed fallback PR, making it a likely routing owner for the final landing choice. (role: recent reviewer and merger; confidence: medium; commits: a89fe705b8b8, db25e9ec5218; files: src/infra/http-body.ts, src/agents/tools/web-shared.ts)

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

@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jul 7, 2026
… fallback

The non-streaming fallback called response.text() without any size
bound, buffering the entire body before slicing to maxChars.  Apply
TextEncoder → subarray(maxBytes) → TextDecoder → strip U+FFFD
artifacts → slice(maxChars) so the fallback path respects maxBytes
like the streaming path already does.

Trim trailing replacement characters so a subarray cut that lands
mid-codepoint does not leak a bare U+FFFD into the snippet.

Closes openclaw#101269

Co-Authored-By: Claude <[email protected]>
@lsr911
lsr911 force-pushed the fix/response-body-snippet-bound branch from e86e1a8 to 3d2f218 Compare July 7, 2026 02:52
@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
…back

Per ClawSweeper review: the previous fix still awaited response.text()
before applying maxBytes, so a large non-streaming response body was
still fully materialized in memory first.  Replace the whole-body
fallback with a fail-closed return of "" — when no body reader is
available, return empty rather than risk OOM.

Co-Authored-By: Claude <[email protected]>
@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. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 7, 2026
@lsr911

lsr911 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

@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.

@lsr911

lsr911 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Closing: ClawSweeper judged this superseded by another PR covering the same root issue. Not worth maintaining a conflicting branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

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

1 participant