Skip to content

Commit 6bc661c

Browse files
authored
fix(msteams): bound Bot Framework attachmentInfo JSON response reads (#99125)
* fix(msteams): bound Bot Framework attachmentInfo JSON response reads * test(msteams): prove attachmentInfo stream cancellation
1 parent e88f5ce commit 6bc661c

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎extensions/msteams/src/attachments/bot-framework.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,61 @@ describe("downloadMSTeamsBotFrameworkAttachment", () => {
514514
{ status: 500 },
515515
]);
516516
});
517+
518+
it("bounds an unbounded attachmentInfo JSON body and cancels the stream", async () => {
519+
const state = { canceled: false, enqueued: 0 };
520+
const chunkBytes = 1024 * 1024;
521+
const stream = new ReadableStream<Uint8Array>({
522+
pull(controller) {
523+
if (state.enqueued >= 64) {
524+
controller.close();
525+
return;
526+
}
527+
state.enqueued += 1;
528+
controller.enqueue(new Uint8Array(chunkBytes).fill(0x61));
529+
},
530+
cancel() {
531+
state.canceled = true;
532+
},
533+
});
534+
const jsonSpy = vi.spyOn(Response.prototype, "json").mockImplementation(async () => {
535+
throw new Error("raw response.json() should not be used");
536+
});
537+
const fetchFn: typeof fetch = vi.fn(async () => {
538+
return new Response(stream, {
539+
status: 200,
540+
headers: { "content-type": "application/json" },
541+
});
542+
});
543+
544+
try {
545+
const warn = vi.fn();
546+
const media = await downloadMSTeamsBotFrameworkAttachment({
547+
serviceUrl: "https://smba.trafficmanager.net/amer",
548+
attachmentId: "att-1",
549+
tokenProvider: buildTokenProvider(),
550+
maxBytes: 10_000_000,
551+
fetchFn,
552+
fetchFnSupportsDispatcher: true,
553+
resolveFn: resolvePublicHost,
554+
logger: { warn },
555+
});
556+
557+
expect(media).toBeUndefined();
558+
expect(jsonSpy).not.toHaveBeenCalled();
559+
// Enforced well before the 64 MiB test ceiling; an unbounded reader would keep pulling.
560+
expect(state.enqueued).toBeLessThan(32);
561+
expect(state.canceled).toBe(true);
562+
expect(warn).toHaveBeenCalledWith(
563+
"msteams botFramework attachmentInfo parse failed",
564+
expect.objectContaining({
565+
error: expect.stringMatching(/JSON response exceeds 16777216 bytes/),
566+
}),
567+
);
568+
} finally {
569+
jsonSpy.mockRestore();
570+
}
571+
});
517572
});
518573
});
519574

‎extensions/msteams/src/attachments/bot-framework.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Msteams plugin module implements bot framework behavior.
22
import { parseMediaContentLength } from "openclaw/plugin-sdk/media-runtime";
3+
import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
34
import { getMSTeamsRuntime } from "../runtime.js";
45
import { ensureUserAgentHeader } from "../user-agent.js";
56
import {
@@ -112,7 +113,10 @@ async function fetchBotFrameworkAttachmentInfo(params: {
112113
return undefined;
113114
}
114115
try {
115-
return (await response.json()) as BotFrameworkAttachmentInfo;
116+
return await readProviderJsonResponse<BotFrameworkAttachmentInfo>(
117+
response,
118+
"msteams botFramework attachmentInfo",
119+
);
116120
} catch (err) {
117121
params.logger?.warn?.("msteams botFramework attachmentInfo parse failed", {
118122
error: err instanceof Error ? err.message : String(err),

0 commit comments

Comments
 (0)