Skip to content

Commit ef8fc68

Browse files
RileyJJYobviyus
andauthored
fix(a2a): bound outbound JSON response bodies (#132273)
Route successful A2A peer JSON through OpenClaw's shared bounded response reader. This rejects oversized peer responses at the existing 16 MiB limit and prevents malformed reflected data from retaining active outbound credentials. Normal replies, schema validation, and the one-time legacy method retry remain unchanged. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 2a767ee commit ef8fc68

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎extensions/a2a/src/outbound.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,58 @@ describe("A2A outbound channel delivery", () => {
225225
"invalid A2A JSON-RPC response",
226226
);
227227
await expect(sendA2aChannelText({ cfg, to: "hermes", text: "hello" })).rejects.toThrow(
228-
SyntaxError,
228+
"peer hermes A2A response: malformed JSON response",
229229
);
230230
await expect(sendA2aChannelText({ cfg, to: "hermes", text: "hello" })).rejects.toThrow(
231231
"A2A response without a result",
232232
);
233233
expect(fetchMock).toHaveBeenCalledTimes(4);
234234
});
235+
236+
it("bounds oversized peer JSON responses before parsing", async () => {
237+
const cfg = createA2aOutboundConfig({
238+
token: "inbound-token",
239+
url: "https://hermes.example/a2a/v1",
240+
});
241+
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue(
242+
createA2aJsonResponse({
243+
jsonrpc: "2.0",
244+
result: { task: { id: "remote-task-1" } },
245+
padding: "x".repeat(16 * 1024 * 1024),
246+
}),
247+
);
248+
249+
await expect(sendA2aChannelText({ cfg, to: "hermes", text: "hello" })).rejects.toThrow(
250+
"peer hermes A2A response: JSON response exceeds 16777216 bytes",
251+
);
252+
expect(fetchMock).toHaveBeenCalledOnce();
253+
});
254+
255+
it("does not retain reflected outbound credentials in malformed responses", async () => {
256+
const outboundToken = "outbound-secret-token";
257+
const cfg = createA2aOutboundConfig({
258+
token: "inbound-token",
259+
outboundToken,
260+
url: "https://hermes.example/a2a/v1",
261+
});
262+
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue(
263+
new Response(`{"reflected":"${outboundToken}"`, {
264+
headers: { "content-type": "application/json" },
265+
}),
266+
);
267+
268+
const failure = await sendA2aChannelText({ cfg, to: "hermes", text: "hello" }).catch(
269+
(error: unknown) => error,
270+
);
271+
272+
expect(failure).toBeInstanceOf(Error);
273+
if (!(failure instanceof Error)) {
274+
throw new Error("expected malformed peer response to reject");
275+
}
276+
expect(failure.message).toBe("peer hermes A2A response: malformed JSON response");
277+
expect(failure.message).not.toContain(outboundToken);
278+
expect(String(failure.cause)).not.toContain(outboundToken);
279+
expect(failure.cause).toBeUndefined();
280+
expect(fetchMock).toHaveBeenCalledOnce();
281+
});
235282
});

‎extensions/a2a/src/outbound.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { randomUUID } from "node:crypto";
22
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
3+
import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
34
import {
45
fetchWithSsrFGuard,
56
ssrfPolicyFromHttpBaseUrlAllowedOrigin,
@@ -96,7 +97,11 @@ export async function sendA2aChannelText(
9697
);
9798
}
9899

99-
const parsed = A2aOutboundResponseSchema.safeParse(await response.json());
100+
const parsed = A2aOutboundResponseSchema.safeParse(
101+
await readProviderJsonResponse(response, `peer ${peerName} A2A response`, {
102+
requestHeaders: headers,
103+
}),
104+
);
100105
if (!parsed.success) {
101106
throw new Error(`peer ${peerName} returned an invalid A2A JSON-RPC response`);
102107
}

0 commit comments

Comments
 (0)