|
| 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 | +}); |
0 commit comments