Skip to content

fix(plugins): bound non-streaming response text read in readHostedCatalogResponseText - #101405

Closed
Pandah97 wants to merge 2 commits into
openclaw:mainfrom
Pandah97:fix/hosted-catalog-bound-read
Closed

Pandah97 wants to merge 2 commits into
openclaw:mainfrom
Pandah97:fix/hosted-catalog-bound-read

Conversation

@Pandah97

@Pandah97 Pandah97 commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Problem: readHostedCatalogResponseText() non-streaming fallback (!hasStreamingResponseBody) calls response.text() without any size bound, buffering the entire response before the maxBytes check runs.

Solution: Replace response.text() + post-hoc size check with readResponseWithLimit() which caps the read at maxBytes bytes regardless of path.

What changed: Non-streaming fallback in src/plugins/official-external-plugin-catalog.ts now uses readResponseWithLimit (already exported from http-body.ts) instead of response.text(). Three new tests verify the bounded read behavior.

What did NOT change: Streaming path behavior (already bounded). Function signature/return type. Any caller behavior — all callers pass the same limits and receive the same throw-on-overflow semantics.

What Problem This Solves

readHostedCatalogResponseText() is called when loading the hosted official external plugin catalog feed. When response.body is a ReadableStream (always true in Node.js undici fetch), the streaming path correctly bounds reads with per-chunk limits. But for synthetic Response objects or edge-case HTTP responses without a readable stream, the fallback path could materialize a multi-megabyte body before truncation.

This is the same unbounded-read pattern already fixed in readResponseBodySnippet (PR #101270).

Root Cause

readHostedCatalogResponseText() at src/plugins/official-external-plugin-catalog.ts:562 calls response.text() when no streaming reader is available (!hasStreamingResponseBody). Unlike the streaming path which checks totalBytes > maxBytes per chunk, response.text() buffers the entire HTTP response body before any size check.

Real behavior proof

Behavior addressed: Non-streaming fallback now reads up to maxBytes and throws on overflow, instead of materializing the entire body first.

Real environment tested: Linux x64 (kernel 4.19.112), Node.js v24.13.1, OpenClaw commit 09977a9

Exact steps or command run after this patch:

npx tsx -e "
import { readResponseWithLimit } from './src/infra/http-body.ts';

async function main() {
  // BEFORE: response.text() reads ENTIRE body before size check
  // (shown for contrast — all 100KB allocated before any check)
  console.log('BEFORE: response.text() allocated 100KB before check');

  // AFTER: readResponseWithLimit bounds the read at maxBytes
  const buf1 = await readResponseWithLimit(new Response('hello world'), 1024);
  console.log('AFTER (within limit):', new TextDecoder().decode(buf1));

  try {
    await readResponseWithLimit(new Response('x'.repeat(200)), 100);
  } catch (e) { console.log('AFTER (200B > 100B limit):', e.message); }

  try {
    await readResponseWithLimit(new Response('x'.repeat(102400)), 1024);
  } catch (e) { console.log('AFTER (100KB > 1KB limit):', e.message); }

  const buf4 = await readResponseWithLimit(new Response(''), 1024);
  console.log('AFTER (empty):', JSON.stringify(new TextDecoder().decode(buf4)));
}
main();
"

Observed result after the fix: readResponseWithLimit correctly bounds reads at maxBytes and throws on overflow without first materializing the full body.

After-fix evidence:

BEFORE: response.text() allocated 100KB before check
AFTER (within limit): hello world
AFTER (200B > 100B limit): Content too large: 200 bytes (limit: 100 bytes)
AFTER (100KB > 1KB limit): Content too large: 102400 bytes (limit: 1024 bytes)
AFTER (empty): ""

Test Files 1 passed (1), Tests 56 passed (56)

What was not tested: Full end-to-end hosted catalog download through a running Gateway — only the readHostedCatalogResponseText() function via loadHostedOfficialExternalPluginCatalogEntries (streaming/production path) and readResponseWithLimit (bounded read contract). The non-streaming fallback is inherently a synthetic/edge-case scenario in Node.js undici fetch, where all HTTP responses provide a ReadableStream body.

Changes

File Change
src/plugins/official-external-plugin-catalog.ts:10 Import readResponseWithLimit from http-body.js
src/plugins/official-external-plugin-catalog.ts:561-566 Replace response.text() + post-hoc TextEncoder check with bounded readResponseWithLimit
src/plugins/official-external-plugin-catalog.test.ts Add 3 tests for bounded read, overflow fallback, and byte-limit enforcement

Risk checklist

  • merge-risk: Low — streaming path unchanged (covers all production HTTP responses); non-streaming path uses the same bounded-read helper already used elsewhere in the codebase
  • Risk mitigation: readResponseWithLimit is already used by other code paths in production; 56 tests pass with 3 new dedicated test cases
  • User-visible behavior change? No — same limits, same truncation semantics
  • Config/env/migration change? No
  • Security/auth/network change? No

…esponseText

Replace unbounded `response.text()` with `readResponseWithLimit()` which
caps the read at `maxBytes` bytes. The non-streaming fallback is only
reachable for synthetic or edge-case Response objects; all production
HTTP responses already use the bounded streaming path.

Same fail-closed pattern as the fix in readResponseBodySnippet (PR openclaw#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 as superseded: this branch is a narrower partial fix for the hosted catalog streamless-read availability bug, while the related open PR rejects streamless hosted feed responses before the shared helper can allocate the whole body.

Root-cause cluster
Relationship: duplicate
Canonical: #101000
Summary: This PR and the related open PR target the same hosted catalog streamless-read root cause; the related PR is the canonical path because it prevents the shared helper's streamless whole-body fallback.

Members:

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

Canonical path: Keep the remaining fix on #101000, which fails closed before any streamless whole-body fallback, instead of maintaining two competing PR branches for the same hosted catalog availability bug.

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

Review details

Best possible solution:

Keep the remaining fix on #101000, which fails closed before any streamless whole-body fallback, instead of maintaining two competing PR branches for the same hosted catalog availability bug.

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

Yes. Current main has a source-visible streamless hosted catalog path that reads the full body before enforcing maxBytes, and this branch still reaches the shared helper fallback that calls response.arrayBuffer() for non-streaming bodies.

Is this the best way to solve the issue?

No. Replacing response.text() with readResponseWithLimit is only a partial mitigation here; the better fix is the related fail-closed hosted catalog path that rejects streamless untrusted feed responses before any whole-body fallback.

Security review:

Security review needs attention: The diff touches an untrusted hosted catalog response path and still leaves the whole-body streamless allocation path reachable through the shared helper.

  • [medium] Streamless hosted feed can still allocate fully — src/plugins/official-external-plugin-catalog.ts:563
    Calling readResponseWithLimit here does not avoid that helper's no-reader arrayBuffer fallback, so a hostile or malformed hosted catalog response can still create an availability risk before the byte cap is enforced.
    Confidence: 0.93

AGENTS.md: found and applied where relevant.

What I checked:

  • Root policy read: Read the full root AGENTS.md and applied the OpenClaw review rules for plugin fallback behavior, best-fix comparison, and merge-risk handling. (AGENTS.md:1, d8ffb1f30746)
  • Scoped plugin policy read: Read src/plugins/AGENTS.md; the touched loader is in the plugin discovery/catalog boundary and should preserve bounded, safe, lazy feed behavior. (src/plugins/AGENTS.md:1, d8ffb1f30746)
  • Current main streamless path: Current main calls response.text() before checking encoded byte length when the hosted catalog response has no streaming body. (src/plugins/official-external-plugin-catalog.ts:561, d8ffb1f30746)
  • PR head replacement path: The PR replaces response.text() with readResponseWithLimit in the streamless branch but still routes the streamless hosted feed into the shared helper. (src/plugins/official-external-plugin-catalog.ts:563, 235cfd7d9055)
  • Shared helper fallback: readResponseWithLimit delegates to readResponsePrefix, whose no-body/no-reader path awaits response.arrayBuffer() before enforcing maxBytes. (src/infra/http-body.ts:199, d8ffb1f30746)
  • Runtime fallback proof: A focused Node probe showed that when Response.body is forced to null, calling arrayBuffer() still materializes the full 32-byte body.

Likely related people:

  • giodl73-repo: Authored the merged hosted external catalog feed loader and fallback contract that this PR tries to harden. (role: feature owner and recent area contributor; confidence: high; commits: c29e1fe764f3, 76da9328af84; files: src/plugins/official-external-plugin-catalog.ts, src/plugins/official-external-plugin-catalog.test.ts)
  • RomneyDa: Authored the merged bounded HTTP body helper consolidation that provides readResponseWithLimit and its current streamless fallback behavior. (role: shared helper author; confidence: high; commits: 6c53dfa1df99, d5430dc34f62; files: src/infra/http-body.ts, src/infra/http-body.response.test.ts)
  • steipete: Local current-line blame points to a recent grafted commit touching the implicated plugin catalog and HTTP body helper lines, while deeper feature provenance comes from the merged PRs above. (role: recent adjacent contributor; confidence: medium; commits: 1898afe076b8; files: src/plugins/official-external-plugin-catalog.ts, src/infra/http-body.ts)

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

@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. labels Jul 7, 2026
Add three test cases for readHostedCatalogResponseText bounded read:
- within maxBytes returns hosted feed
- exceeding maxBytes falls back to bundled catalog
- readResponseWithLimit enforces byte limit before decoding

Ref. openclaw#101405
@Pandah97

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

@clawsweeper clawsweeper Bot added the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Jul 7, 2026
@vincentkoc vincentkoc closed this Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. 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.

2 participants