Skip to content

fix(mcp): bound HTTP response body reads at 16 MiB - #99285

Closed
Pick-cat wants to merge 1 commit into
openclaw:mainfrom
Pick-cat:fix/mcp-http-fetch-bound-response
Closed

Pick-cat wants to merge 1 commit into
openclaw:mainfrom
Pick-cat:fix/mcp-http-fetch-bound-response

Conversation

@Pick-cat

@Pick-cat Pick-cat commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where MCP HTTP responses with large bodies (e.g. from a misconfigured or malicious MCP server) could exhaust process memory. The ensureGlobalFetchResponse function read response.text() without any size check, so a response body of hundreds of megabytes would be buffered entirely in memory — risking OOM.

Why This Change Was Made

A single chokepoint fix at ensureGlobalFetchResponse — both callers in buildManagedMcpResponse pass through this function. The fix adds a two-layer guard:

  1. Content-Length pre-check — rejects before response.text() is called when the header is present and oversized.
  2. Post-read length guard — catches oversized bodies that arrive without a Content-Length header.

Cap is 16 MiB, matching the existing SSH sandbox output bound.

User Impact

MCP server responses larger than 16 MiB are now rejected with a clear error message instead of silently consuming unbounded memory until the process crashes.

Real behavior proof

Behavior addressed: ensureGlobalFetchResponse rejects oversized body-less responses via buildMcpHttpFetch.

Real environment tested: Linux x64, Node 22.22.0, openclaw main branch.

Evidence after fix:

[case 1] Content-Length pre-check rejects before reading body
  ok: rejects when Content-Length header exceeds cap
  ok: error message includes byte limit (16777216)
  ok: error message includes offending size
[case 2] Post-read check catches oversized body without Content-Length
  ok: rejects when actual text length exceeds cap
  ok: error message includes byte limit (16777216)
  ok: error message includes offending char count
  info: bodyLength=16777217 chars, cap=16777216 chars, elapsed=1 ms
[case 3] Within-limit response passes through unchanged
  ok: returns status 200
  ok: returns JSON body intact
[case 4] Body at exactly the cap (boundary) is accepted
  ok: accepts response at exact cap (Content-Length)

=== Summary ===
cap: 16777216 bytes (~16 MiB)
ALL PROOF ASSERTIONS: 9 passed, 0 failed

Negative control (pre-fix behavior reproduced):

[case 1] Content-Length pre-check rejects before reading body
  FAIL: rejects when Content-Length header exceeds cap
  FAIL: error message includes byte limit (16777216)
  FAIL: error message includes offending size
[case 2] Post-read check catches oversized body without Content-Length
  FAIL: rejects when actual text length exceeds cap
  FAIL: error message includes byte limit (16777216)
  FAIL: error message includes offending char count
  info: bodyLength=16777217 chars, cap=16777216 chars, elapsed=22 ms
[case 3] Within-limit response passes through unchanged
  ok: returns status 200
  ok: returns JSON body intact
[case 4] Body at exactly the cap (boundary) is accepted
  ok: accepts response at exact cap (Content-Length)

ALL PROOF ASSERTIONS: 3 passed, 6 failed
exit=1

Without the fix, oversized responses pass through silently (no rejection, potential OOM). With the fix, they are caught and rejected.

What was not tested: Real undici HTTP server round-trip (mock fetch used; the bounds guards operate on the Response object post-fetch, independent of transport).

Evidence

Vitest: 13/13 passed (10 existing + 3 new bound tests)

$ node scripts/run-vitest.mjs src/agents/mcp-http-fetch.test.ts --run
Test Files  1 passed (1)
     Tests  13 passed (13)

New tests:

  • rejects body-less responses whose Content-Length exceeds the 16 MiB cap
  • rejects body-less responses whose actual text exceeds the 16 MiB cap
  • accepts body-less responses within the 16 MiB cap

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S r: too-many-prs Auto-close: author has more than twenty active PRs. labels Jul 3, 2026
@openclaw-barnacle

Copy link
Copy Markdown

Closing this PR because the author has more than 20 active PRs in this repo. Please reduce the active PR queue and reopen or resubmit once it is back under the limit. You can close your own PRs to get back under the limit.

@openclaw-barnacle openclaw-barnacle Bot closed this Jul 3, 2026
@Pick-cat

Pick-cat commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #99293 (1 MiB cap aligned with #98619 precedent, added boundary tests).

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

Labels

agents Agent runtime and tooling r: too-many-prs Auto-close: author has more than twenty active PRs. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant