Skip to content

Commit 0f137ae

Browse files
NIOcursoragent
andcommitted
fix(plugins): bound hosted catalog feed reads on non-streaming responses
Co-authored-by: Cursor <[email protected]>
1 parent 94da0a9 commit 0f137ae

2 files changed

Lines changed: 91 additions & 79 deletions

File tree

‎src/plugins/official-external-plugin-catalog.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,83 @@ describe("official external plugin catalog", () => {
15011501
}
15021502
});
15031503

1504+
it("rejects non-streaming hosted feeds before buffering underreported bodies", async () => {
1505+
const oversized = "x".repeat(8192);
1506+
const response = new Response(oversized, {
1507+
status: 200,
1508+
headers: { "content-length": "1" },
1509+
});
1510+
Object.defineProperty(response, "body", { value: null });
1511+
const arrayBuffer = vi.fn(response.arrayBuffer.bind(response));
1512+
Object.defineProperty(response, "arrayBuffer", { value: arrayBuffer });
1513+
1514+
const result = await loadHostedOfficialExternalPluginCatalogEntries({
1515+
snapshotStore: null,
1516+
maxBytes: 4096,
1517+
fetchImpl: vi.fn(async () => response),
1518+
});
1519+
1520+
expect(result.source).toBe("bundled-fallback");
1521+
if (result.source === "bundled-fallback") {
1522+
expect(result.error).toContain("streaming response body unavailable");
1523+
}
1524+
expect(arrayBuffer).not.toHaveBeenCalled();
1525+
});
1526+
1527+
it("rejects non-streaming hosted feeds without content-length", async () => {
1528+
const body = JSON.stringify({
1529+
schemaVersion: 1,
1530+
id: "openclaw-official-external-plugins",
1531+
generatedAt: "2026-06-22T00:00:00.000Z",
1532+
sequence: 1,
1533+
entries: [],
1534+
});
1535+
const response = new Response(body, { status: 200 });
1536+
Object.defineProperty(response, "body", { value: null });
1537+
1538+
const result = await loadHostedOfficialExternalPluginCatalogEntries({
1539+
snapshotStore: null,
1540+
fetchImpl: vi.fn(async () => response),
1541+
});
1542+
1543+
expect(result.source).toBe("bundled-fallback");
1544+
if (result.source === "bundled-fallback") {
1545+
expect(result.error).toContain("streaming response body unavailable");
1546+
}
1547+
});
1548+
1549+
it("rejects oversized streaming hosted feeds incrementally", async () => {
1550+
const chunk = new Uint8Array(512 * 1024).fill("x".charCodeAt(0));
1551+
let emitted = 0;
1552+
const body = new ReadableStream<Uint8Array>({
1553+
pull(controller) {
1554+
if (emitted >= 3) {
1555+
controller.close();
1556+
return;
1557+
}
1558+
emitted += 1;
1559+
controller.enqueue(chunk);
1560+
},
1561+
});
1562+
1563+
const result = await loadHostedOfficialExternalPluginCatalogEntries({
1564+
snapshotStore: null,
1565+
maxBytes: 1024 * 1024,
1566+
fetchImpl: vi.fn(
1567+
async () =>
1568+
new Response(body, {
1569+
status: 200,
1570+
headers: { "content-type": "application/json" },
1571+
}),
1572+
),
1573+
});
1574+
1575+
expect(result.source).toBe("bundled-fallback");
1576+
if (result.source === "bundled-fallback") {
1577+
expect(result.error).toContain("exceeds 1048576 bytes");
1578+
}
1579+
});
1580+
15041581
it("prefers feed install candidates before legacy install metadata", () => {
15051582
expect(
15061583
resolveOfficialExternalPluginInstall({

‎src/plugins/official-external-plugin-catalog.ts‎

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import officialExternalPluginCatalog from "../../scripts/lib/official-external-p
77
import officialExternalProviderCatalog from "../../scripts/lib/official-external-provider-catalog.json" with { type: "json" };
88
import { MANIFEST_KEY } from "../compat/legacy-names.js";
99
import { normalizeClawHubSha256Integrity } from "../infra/clawhub.js";
10+
import { readResponseWithLimit } from "../infra/http-body.js";
1011
import { isRecord } from "../utils.js";
1112
import type {
1213
PluginManifestChannelConfig,
@@ -508,91 +509,25 @@ function parseHostedCatalogContentLength(raw: string | null, maxBytes: number):
508509
}
509510
}
510511

511-
function hasStreamingResponseBody(
512-
response: Response,
513-
): response is Response & { body: ReadableStream<Uint8Array> } {
514-
return Boolean(
515-
response.body && typeof (response.body as { getReader?: unknown }).getReader === "function",
516-
);
517-
}
518-
519-
async function readHostedCatalogChunkWithTimeout(
520-
reader: ReadableStreamDefaultReader<Uint8Array>,
521-
chunkTimeoutMs: number,
522-
): Promise<Awaited<ReturnType<typeof reader.read>>> {
523-
let timeoutId: ReturnType<typeof setTimeout> | undefined;
524-
let timedOut = false;
525-
return await new Promise((resolve, reject) => {
526-
const clear = () => {
527-
if (timeoutId !== undefined) {
528-
clearTimeout(timeoutId);
529-
timeoutId = undefined;
530-
}
531-
};
532-
timeoutId = setTimeout(() => {
533-
timedOut = true;
534-
clear();
535-
void reader.cancel().catch(() => undefined);
536-
reject(new Error(`hosted catalog feed read timed out after ${chunkTimeoutMs}ms`));
537-
}, chunkTimeoutMs);
538-
void reader.read().then(
539-
(result) => {
540-
clear();
541-
if (!timedOut) {
542-
resolve(result);
543-
}
544-
},
545-
(err: unknown) => {
546-
clear();
547-
if (!timedOut) {
548-
reject(err instanceof Error ? err : new Error(String(err)));
549-
}
550-
},
551-
);
552-
});
553-
}
554-
555512
async function readHostedCatalogResponseText(params: {
556513
response: Response;
557514
maxBytes: number;
558515
chunkTimeoutMs: number;
559516
}): Promise<string> {
560517
parseHostedCatalogContentLength(params.response.headers.get("content-length"), params.maxBytes);
561-
if (!hasStreamingResponseBody(params.response)) {
562-
const text = await params.response.text();
563-
if (new TextEncoder().encode(text).byteLength > params.maxBytes) {
564-
throw new Error(`hosted catalog feed exceeds ${params.maxBytes} bytes`);
565-
}
566-
return text;
567-
}
568-
const reader = params.response.body.getReader();
569-
const chunks: Uint8Array[] = [];
570-
let totalBytes = 0;
571-
try {
572-
while (true) {
573-
const chunk = await readHostedCatalogChunkWithTimeout(reader, params.chunkTimeoutMs);
574-
if (chunk.done) {
575-
break;
576-
}
577-
totalBytes += chunk.value.byteLength;
578-
if (totalBytes > params.maxBytes) {
579-
throw new Error(`hosted catalog feed exceeds ${params.maxBytes} bytes`);
580-
}
581-
chunks.push(chunk.value);
582-
}
583-
} catch (err) {
584-
await reader.cancel().catch(() => undefined);
585-
throw err;
586-
} finally {
587-
reader.releaseLock();
588-
}
589-
const body = new Uint8Array(totalBytes);
590-
let offset = 0;
591-
for (const chunk of chunks) {
592-
body.set(chunk, offset);
593-
offset += chunk.byteLength;
594-
}
595-
return new TextDecoder().decode(body);
518+
const streamless = !params.response.body || typeof params.response.body.getReader !== "function";
519+
// Hosted remote feeds are untrusted input, so fail closed when Fetch cannot
520+
// provide a streaming body instead of trusting Content-Length before read.
521+
if (streamless) {
522+
throw new Error("hosted catalog feed streaming response body unavailable");
523+
}
524+
const buffer = await readResponseWithLimit(params.response, params.maxBytes, {
525+
chunkTimeoutMs: params.chunkTimeoutMs,
526+
onOverflow: ({ maxBytes }) => new Error(`hosted catalog feed exceeds ${maxBytes} bytes`),
527+
onIdleTimeout: ({ chunkTimeoutMs }) =>
528+
new Error(`hosted catalog feed read timed out after ${chunkTimeoutMs}ms`),
529+
});
530+
return new TextDecoder().decode(buffer);
596531
}
597532

598533
function bundledOfficialExternalPluginCatalogEntries(): OfficialExternalPluginCatalogEntry[] {

0 commit comments

Comments
 (0)