Skip to content

Commit b353dc0

Browse files
committed
fix(googlechat): bound api error body reads
1 parent 75a7b2c commit b353dc0

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

extensions/googlechat/src/api.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import crypto from "node:crypto";
33
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
44
import { parseMediaContentLength } from "openclaw/plugin-sdk/media-runtime";
5-
import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
5+
import {
6+
readProviderJsonResponse,
7+
readResponseTextLimited,
8+
} from "openclaw/plugin-sdk/provider-http";
69
import { readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime";
710
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
811
import type { ResolvedGoogleChatAccount } from "./accounts.js";
@@ -54,7 +57,7 @@ async function withGoogleChatResponse<T>(params: {
5457
});
5558
try {
5659
if (!response.ok) {
57-
const text = await response.text().catch(() => "");
60+
const text = await readResponseTextLimited(response).catch(() => "");
5861
throw new Error(`${errorPrefix} ${response.status}: ${text || response.statusText}`);
5962
}
6063
return await handleResponse(response);

extensions/googlechat/src/targets.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,5 +658,47 @@ describe("verifyGoogleChatRequest", () => {
658658
expect(canceled).toBe(true);
659659
expect(bytesPulled).toBeLessThan(TOTAL_CHUNKS * ONE_MIB);
660660
});
661+
662+
it("caps non-OK sendMessage error bodies before formatting the API error", async () => {
663+
const ONE_MIB = 1024 * 1024;
664+
const TOTAL_CHUNKS = 32;
665+
const chunk = new TextEncoder().encode("x".repeat(ONE_MIB));
666+
667+
let bytesPulled = 0;
668+
let canceled = false;
669+
const oversizedError = new Response(
670+
new ReadableStream<Uint8Array>({
671+
pull(controller) {
672+
if (bytesPulled >= TOTAL_CHUNKS * ONE_MIB) {
673+
controller.close();
674+
return;
675+
}
676+
bytesPulled += chunk.length;
677+
controller.enqueue(chunk);
678+
},
679+
cancel() {
680+
canceled = true;
681+
},
682+
}),
683+
{ status: 500, statusText: "Internal Server Error" },
684+
);
685+
const release = vi.fn(async () => {});
686+
mocks.fetchWithSsrFGuard.mockResolvedValueOnce({
687+
response: oversizedError,
688+
release,
689+
});
690+
691+
await expect(
692+
sendGoogleChatMessage({
693+
account,
694+
space: "spaces/AAA",
695+
text: "hello",
696+
}),
697+
).rejects.toThrow(/^Google Chat API 500: x+/);
698+
699+
expect(canceled).toBe(true);
700+
expect(bytesPulled).toBeLessThan(TOTAL_CHUNKS * ONE_MIB);
701+
expect(release).toHaveBeenCalledOnce();
702+
});
661703
});
662704
});

0 commit comments

Comments
 (0)