Skip to content

fix(mcp): bound body-less MCP HTTP text responses at 1 MiB - #99293

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 body-less MCP HTTP responses (e.g. OAuth error ForeignResponse objects from the MCP SDK, or responses whose body was consumed) could exhaust process memory. The ensureGlobalFetchResponse function called response.text() without verifying the declared Content-Length, allowing an oversized body-less response to allocate unbounded memory.

Why This Change Was Made

Single-chokepoint fix at ensureGlobalFetchResponse — both callers in buildManagedMcpResponse converge through this function. The change makes the body-less path fail-safe:

  • Content-Length present and valid (decimal integer, <= 1 MiB) → text() is called, response body preserved.
  • Content-Length missing, malformed (non-decimal), or oversized → text() is never called, returning a null-body Response. No allocation, no OOM.
  • Post-read guard → even when Content-Length is valid, byte length is verified after text() to catch lying headers.
  • Streaming responses (body != null) are untouched — they already avoid buffering.

Cap is 1 MiB, matching the merged CONVEX_BROKER_RESPONSE_MAX_BYTES precedent in #98619. Content-Length validation uses /^\d+$/ to reject scientific-notation (1e3) and hex (0x40) values that Number() would accept but are invalid per RFC 7230.

User Impact

MCP servers returning oversized or unverifiable body-less responses now receive a null-body Response instead of causing the gateway process to allocate unbounded memory. Legitimate MCP OAuth error responses (which declare a valid Content-Length) are unaffected.

Real behavior proof

Behavior addressed: buildMcpHttpFetch (production entry point) → ensureGlobalFetchResponse rejects/drops body-less text reads when Content-Length is missing, malformed, or oversized. text() is never called in those cases, preventing the unbounded allocation.

Real environment tested: Linux x64, Node 22.22.0, standalone .mts script driving the real buildMcpHttpFetch production function.

Evidence after fix:

[case 1] body-less response with valid Content-Length — text preserved
  ok: returns status 400
  ok: body is readable
  ok: text content preserved
[case 2] oversized Content-Length (1048577 bytes) → null body, text() skipped
  ok: returns a Response (does not throw)
  ok: body is null (text dropped)
  ok: text() was NOT called
  info: text() called 0 times (expected 0)
[case 3] missing Content-Length → null body, text() skipped
  ok: returns a Response (does not throw)
  ok: body is null (text skipped)
  ok: text() was NOT called
  info: text() called 0 times (expected 0)
[case 4] malformed Content-Length ("abc") → null body, text() skipped
  ok: returns a Response (does not throw)
  ok: body is null (text skipped)
  ok: text() was NOT called
  info: text() called 0 times (expected 0)
[case 5] Content-Length exactly at cap (1048576 bytes) → accepted
  ok: returns a Response
  ok: body is readable
  ok: text() was called
  info: text() called 1 times, body byteLength=1048576
[case 6] 204 No Content response passes through unchanged
  ok: returns status 204
  ok: body is null
[case 7] streaming response (body != null) passes through untouched
  ok: returns status 200
  ok: body is readable
  ok: text content preserved

=== Summary ===
cap: 1048576 bytes (~1 MiB)
ALL PROOF ASSERTIONS: 20 passed, 0 failed

Negative control (pre-fix main code, same script):

[case 2] oversized Content-Length (1048577 bytes) → null body, text() skipped
  FAIL: body is null (text dropped)
  FAIL: text() was NOT called
  info: text() called 1 times (expected 0)
[case 3] missing Content-Length → null body, text() skipped
  FAIL: body is null (text skipped)
  FAIL: text() was NOT called
  info: text() called 1 times (expected 0)
[case 4] malformed Content-Length ("abc") → null body, text() skipped
  FAIL: body is null (text skipped)
  FAIL: text() was NOT called
  info: text() called 1 times (expected 0)

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

Without the fix, text() is called unconditionally on oversized/missing/malformed Content-Length responses (6 FAIL, exit=1). With the fix, text() is never called in those cases and the response is dropped safely.

What was not tested: Full undici network round-trip (mock fetch used; the bounds guards operate on the Response object post-fetch, independent of transport). Streaming response path is verified untouched.

Evidence

Vitest: 16/16 passed (10 existing + 6 new)

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

New tests:

  • drops body-less text when Content-Length exceeds the 1 MiB cap — verifies text() not called
  • drops body-less text when Content-Length is missing — verifies text() not called
  • drops body-less text when Content-Length is malformed (abc/1e3/0x40) — parameterized, verifies text() not called
  • accepts body-less responses with valid Content-Length within the 1 MiB cap — verifies normal path works
  • Updated existing ForeignResponse OAuth test to include Content-Length header

@openclaw-barnacle openclaw-barnacle Bot added the agents Agent runtime and tooling label Jul 3, 2026
@clawsweeper

clawsweeper Bot commented Jul 3, 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 is a duplicate landing path for the same body-less MCP HTTP response policy already covered by the open, proof-positive, mergeable candidate at #98143, which has broader coverage and stronger loopback proof.

Root-cause cluster
Relationship: duplicate
Canonical: #98143
Summary: This PR duplicates the same MCP body-less HTTP response fail-closed policy already represented by a stronger open canonical candidate.

Members:

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

Canonical path: Use #98143 as the single canonical landing path and keep #97521 as the maintainer policy tracker until the fail-closed behavior is accepted or changed.

So I’m closing this here and keeping the remaining discussion on #98143 and #97521.

Review details

Best possible solution:

Use #98143 as the single canonical landing path and keep #97521 as the maintainer policy tracker until the fail-closed behavior is accepted or changed.

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

Yes. Current main reaches await response.text() for a body-less non-204/205/304 MCP response, and the existing compatibility test exercises that text-only foreign-response shape.

Is this the best way to solve the issue?

No for this branch as the landing path. The implementation direction is plausible, but the best maintainer path is to consolidate on the already open proof-positive canonical PR with broader tests and loopback proof.

Security review:

Security review cleared: No supply-chain or new security-boundary regression was found; the diff is targeted availability hardening, with compatibility and residual allocation tradeoffs captured as merge risk.

AGENTS.md: found and applied where relevant.

What I checked:

  • Current main affected fallback: Current main still normalizes body-less non-204/205/304 MCP responses by awaiting response.text() before constructing a global Response, which is the fallback both PRs target. (src/agents/mcp-http-fetch.ts:68, 8d535fb039c5)
  • This PR implementation: The current PR gates body-less text reads on a decimal Content-Length at or below 1 MiB and returns a null-body Response for missing, malformed, or oversized declared lengths. (src/agents/mcp-http-fetch.ts:68, 5e75538c39e8)
  • Canonical candidate remains viable: The related PR is open, mergeable, labeled proof sufficient and ready for maintainer look, and implements the same fail-closed body-less MCP response policy. (8bb30cd79ae1)
  • Canonical candidate has broader proof and tests: The related PR includes real http.createServer loopback proof through production buildMcpHttpFetch and test coverage for missing, malformed, oversized, and declared-small/returned-large body-less responses. (src/agents/mcp-http-fetch.test.ts:313, 8bb30cd79ae1)
  • Policy tracker remains open: The open policy issue tracks the same MCP body-less foreign-response compatibility/security decision, so maintainer review should happen against one canonical PR plus the policy issue.
  • MCP SDK contract inspected: The pinned MCP SDK v1.29.0 FetchLike returns a Response, and parseErrorResponse reads Response text, which explains why the body-less text fallback is SDK-facing compatibility behavior rather than dead code. (v1.29.0)

Likely related people:

  • LiuwqGit: Authored the merged MCP OAuth repair that added/preserved body-less foreign-response text handling and the compatibility test this PR changes. (role: introduced behavior; confidence: high; commits: 7a34986ef064, 7efaf4cf9ae4; files: src/agents/mcp-http-fetch.ts, src/agents/mcp-http-fetch.test.ts, src/agents/mcp-oauth.ts)
  • vincentkoc: Committed or merged the MCP OAuth repair history and later refactors on the MCP fetch helper surface. (role: merger and recent area contributor; confidence: high; commits: 7a34986ef064, 0c651fd082b2, 4292f0fe7f2e; files: src/agents/mcp-http-fetch.ts, src/agents/mcp-http-fetch.test.ts)
  • pgondhi987: Authored earlier MCP HTTP redirect guard work around the same guarded fetch and response-normalization boundary. (role: adjacent owner; confidence: medium; commits: 3c6259ebb70c; files: src/agents/mcp-http-fetch.ts, src/agents/mcp-transport.ts, src/infra/net/fetch-guard.ts)
  • Pick-cat: Has prior merged bounded-read/provider HTTP helper work and authored the open canonical candidate, so this is prior merged area history rather than mere authorship of this PR. (role: recent area contributor; confidence: medium; commits: eb5fb2aa69f4; files: src/agents/provider-http-errors.ts, src/agents/mcp-http-fetch.ts, src/agents/mcp-http-fetch.test.ts)

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

@Pick-cat

Pick-cat commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 3, 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. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 3, 2026
@Pick-cat
Pick-cat force-pushed the fix/mcp-http-fetch-bound-response branch from 2c98842 to 5e75538 Compare July 3, 2026 02:39
@Pick-cat Pick-cat changed the title fix(mcp): bound HTTP response body reads at 1 MiB fix(mcp): bound body-less MCP HTTP text responses at 1 MiB Jul 3, 2026
@Pick-cat

Pick-cat commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 3, 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 proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed 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. labels Jul 3, 2026
@clawsweeper clawsweeper Bot closed this Jul 3, 2026
@clawsweeper

clawsweeper Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

🦞✅
ClawSweeper autoclose is complete.

Reason: structured ClawSweeper close marker: close-required (sha=5e75538c39e82de171ad6f43f9349f37c0f3f8b6)

Closed:

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

Labels

agents Agent runtime and tooling merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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.

1 participant