Skip to content

Commit 9134cee

Browse files
committed
fix(googlechat): cancel unread auth response bodies before release
The size guard in readGoogleAuthResponseBytes inspects content-length and throws before it reaches response.body.getReader(), so an oversized auth response leaves the stream untouched and the finally block releases the dispatcher without cancelling it. This is the same shape #111290 just fixed in api.ts, applied to the auth transport's own guard site.
1 parent cc2ba80 commit 9134cee

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Exercise Google auth requests through a real guarded HTTP transport.
2+
import { createServer, type Server } from "node:http";
3+
import type { AddressInfo } from "node:net";
4+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
5+
6+
const loopback = vi.hoisted(() => ({
7+
baseUrl: "",
8+
contentLength: "",
9+
releases: [] as Array<{ bodyIsNull: boolean; bodyUsed: boolean }>,
10+
}));
11+
12+
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => {
13+
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>();
14+
return {
15+
...actual,
16+
fetchWithSsrFGuard: async (...args: Parameters<typeof actual.fetchWithSsrFGuard>) => {
17+
const [params] = args;
18+
const guarded = await actual.fetchWithSsrFGuard({
19+
...params,
20+
policy: { allowPrivateNetwork: true },
21+
url: loopback.baseUrl,
22+
});
23+
return {
24+
...guarded,
25+
release: async () => {
26+
loopback.releases.push({
27+
bodyIsNull: guarded.response.body === null,
28+
bodyUsed: guarded.response.bodyUsed,
29+
});
30+
await guarded.release();
31+
},
32+
};
33+
},
34+
};
35+
});
36+
37+
const { getGoogleAuthTransport } = await import("./google-auth.runtime.js");
38+
39+
const RESPONSE_BODY = '{"access_token":"probe"}';
40+
41+
let server: Server;
42+
43+
beforeAll(async () => {
44+
server = createServer((_req, res) => {
45+
res.writeHead(200, {
46+
"content-length": loopback.contentLength || String(RESPONSE_BODY.length),
47+
"content-type": "application/json",
48+
});
49+
res.end(RESPONSE_BODY);
50+
});
51+
await new Promise<void>((resolve) => {
52+
server.listen(0, "127.0.0.1", resolve);
53+
});
54+
const address = server.address() as AddressInfo;
55+
loopback.baseUrl = `http://127.0.0.1:${address.port}/token`;
56+
});
57+
58+
afterAll(async () => {
59+
await new Promise<void>((resolve) => {
60+
server.close(() => resolve());
61+
});
62+
});
63+
64+
beforeEach(() => {
65+
loopback.contentLength = "";
66+
loopback.releases = [];
67+
});
68+
69+
describe("google auth guarded fetch", () => {
70+
it("cancels the unread body when the size guard rejects the response", async () => {
71+
loopback.contentLength = String(1024 * 1024 + 1);
72+
const transport = await getGoogleAuthTransport();
73+
74+
await expect(transport.request({ url: loopback.baseUrl })).rejects.toThrow();
75+
76+
expect(loopback.releases).toEqual([{ bodyIsNull: false, bodyUsed: true }]);
77+
});
78+
79+
it("leaves a fully read response alone", async () => {
80+
const transport = await getGoogleAuthTransport();
81+
82+
await transport.request({ url: loopback.baseUrl });
83+
84+
expect(loopback.releases).toEqual([{ bodyIsNull: false, bodyUsed: true }]);
85+
});
86+
});

‎extensions/googlechat/src/google-auth.runtime.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ function createGoogleAuthFetch(): FetchLike {
432432
statusText: response.statusText,
433433
});
434434
} finally {
435+
// The size guard can reject before the stream is touched, leaving an
436+
// unread body. Start cancellation before release; awaiting it can
437+
// deadlock when debug capture tees the stream.
438+
if (!response.bodyUsed) {
439+
void response.body?.cancel().catch(() => undefined);
440+
}
435441
await release();
436442
}
437443
};

0 commit comments

Comments
 (0)