|
| 1 | +import http from "node:http"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +vi.mock("openclaw/plugin-sdk/ssrf-runtime", async () => { |
| 5 | + const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/ssrf-runtime")>( |
| 6 | + "openclaw/plugin-sdk/ssrf-runtime", |
| 7 | + ); |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + fetchWithSsrFGuard: async (params: { |
| 11 | + url: string; |
| 12 | + init?: RequestInit; |
| 13 | + signal?: AbortSignal; |
| 14 | + }) => ({ |
| 15 | + response: await fetch(params.url, { ...params.init, signal: params.signal }), |
| 16 | + finalUrl: params.url, |
| 17 | + release: async () => {}, |
| 18 | + }), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +const { pokeUrbitChannel } = await import("./channel-ops.js"); |
| 23 | + |
| 24 | +const CHUNK = Buffer.alloc(64 * 1024, "X"); |
| 25 | + |
| 26 | +describe("tlon error body boundary", () => { |
| 27 | + let server: http.Server; |
| 28 | + |
| 29 | + afterEach(async () => { |
| 30 | + vi.restoreAllMocks(); |
| 31 | + await new Promise<void>((resolve) => { |
| 32 | + server?.close(() => resolve()); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("bounds poke error body at 16 KiB", async () => { |
| 37 | + server = http.createServer((_req, res) => { |
| 38 | + res.writeHead(500, { "Content-Type": "text/plain" }); |
| 39 | + let written = 0; |
| 40 | + function write() { |
| 41 | + if (written >= 4 * 1024 * 1024) { |
| 42 | + res.end(); |
| 43 | + return; |
| 44 | + } |
| 45 | + const ok = res.write(CHUNK); |
| 46 | + written += CHUNK.length; |
| 47 | + if (ok) { |
| 48 | + setImmediate(write); |
| 49 | + } else { |
| 50 | + res.once("drain", write); |
| 51 | + } |
| 52 | + } |
| 53 | + write(); |
| 54 | + }); |
| 55 | + const port = await new Promise<number>((resolve) => { |
| 56 | + server.listen(0, "127.0.0.1", () => { |
| 57 | + resolve((server.address() as { port: number }).port); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + const err = await pokeUrbitChannel( |
| 62 | + { |
| 63 | + baseUrl: `http://127.0.0.1:${port}`, |
| 64 | + cookie: "urbit=cookie", |
| 65 | + ship: "~zod", |
| 66 | + channelId: "test", |
| 67 | + }, |
| 68 | + { app: "test", mark: "test", json: {}, auditContext: "test" }, |
| 69 | + ).catch((e: unknown) => e); |
| 70 | + |
| 71 | + expect(err).toBeInstanceOf(Error); |
| 72 | + const msg = (err as Error).message; |
| 73 | + expect(Buffer.byteLength(msg, "utf8")).toBeLessThan(32 * 1024); |
| 74 | + expect(msg).toContain("X"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("preserves short error body when under cap", async () => { |
| 78 | + server = http.createServer((_req, res) => { |
| 79 | + res.writeHead(500, { "Content-Type": "text/plain" }); |
| 80 | + res.end("session expired"); |
| 81 | + }); |
| 82 | + const port = await new Promise<number>((resolve) => { |
| 83 | + server.listen(0, "127.0.0.1", () => { |
| 84 | + resolve((server.address() as { port: number }).port); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + const err = await pokeUrbitChannel( |
| 89 | + { |
| 90 | + baseUrl: `http://127.0.0.1:${port}`, |
| 91 | + cookie: "urbit=cookie", |
| 92 | + ship: "~zod", |
| 93 | + channelId: "test", |
| 94 | + }, |
| 95 | + { app: "test", mark: "test", json: {}, auditContext: "test" }, |
| 96 | + ).catch((e: unknown) => e); |
| 97 | + |
| 98 | + expect(err).toBeInstanceOf(Error); |
| 99 | + expect((err as Error).message).toContain("session expired"); |
| 100 | + }); |
| 101 | +}); |
0 commit comments