|
1 | 1 | // Qa Lab tests cover credential lease plugin behavior. |
| 2 | +import { createServer } from "node:http"; |
2 | 3 | import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime"; |
3 | 4 | import { afterEach, describe, expect, it, vi } from "vitest"; |
4 | 5 | import { |
@@ -39,6 +40,74 @@ function fetchInit(fetchImpl: FetchMock, index = 0): RequestInit { |
39 | 40 | return init; |
40 | 41 | } |
41 | 42 |
|
| 43 | +async function startStreamingFailureBroker(params: { |
| 44 | + chunkBytes?: number; |
| 45 | + intervalMs?: number; |
| 46 | + totalBytes?: number; |
| 47 | +}) { |
| 48 | + const chunkBytes = params.chunkBytes ?? 64 * 1024; |
| 49 | + const intervalMs = params.intervalMs ?? 1; |
| 50 | + const totalBytes = params.totalBytes ?? 4 * 1024 * 1024; |
| 51 | + let bytesWritten = 0; |
| 52 | + let requestCount = 0; |
| 53 | + let resolveClose: () => void = () => {}; |
| 54 | + const closePromise = new Promise<void>((resolve) => { |
| 55 | + resolveClose = resolve; |
| 56 | + }); |
| 57 | + |
| 58 | + const server = createServer((_req, res) => { |
| 59 | + requestCount += 1; |
| 60 | + res.writeHead(500, { "content-type": "text/plain" }); |
| 61 | + const interval = setInterval(() => { |
| 62 | + if (bytesWritten >= totalBytes || res.destroyed) { |
| 63 | + clearInterval(interval); |
| 64 | + if (!res.destroyed) { |
| 65 | + res.end(); |
| 66 | + } |
| 67 | + return; |
| 68 | + } |
| 69 | + const nextBytes = Math.min(chunkBytes, totalBytes - bytesWritten); |
| 70 | + bytesWritten += nextBytes; |
| 71 | + res.write("x".repeat(nextBytes)); |
| 72 | + }, intervalMs); |
| 73 | + res.on("close", () => { |
| 74 | + clearInterval(interval); |
| 75 | + resolveClose(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + await new Promise<void>((resolve, reject) => { |
| 80 | + server.once("error", reject); |
| 81 | + server.listen(0, "127.0.0.1", () => { |
| 82 | + server.off("error", reject); |
| 83 | + resolve(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + const address = server.address(); |
| 88 | + if (!address || typeof address === "string") { |
| 89 | + throw new Error("expected streaming broker address"); |
| 90 | + } |
| 91 | + return { |
| 92 | + closePromise, |
| 93 | + getBytesWritten: () => bytesWritten, |
| 94 | + getRequestCount: () => requestCount, |
| 95 | + totalBytes, |
| 96 | + url: `http://127.0.0.1:${address.port}`, |
| 97 | + stop: async () => { |
| 98 | + await new Promise<void>((resolve, reject) => { |
| 99 | + server.close((error) => { |
| 100 | + if (error) { |
| 101 | + reject(error); |
| 102 | + return; |
| 103 | + } |
| 104 | + resolve(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + }, |
| 108 | + }; |
| 109 | +} |
| 110 | + |
42 | 111 | describe("credential lease runtime", () => { |
43 | 112 | afterEach(() => { |
44 | 113 | vi.restoreAllMocks(); |
@@ -107,6 +176,60 @@ describe("credential lease runtime", () => { |
107 | 176 | expect(headers.authorization).toBe("Bearer maintainer-secret"); |
108 | 177 | }); |
109 | 178 |
|
| 179 | + it("bounds oversized convex broker failure bodies before parsing", async () => { |
| 180 | + const fetchImpl = vi.fn<typeof fetch>().mockResolvedValueOnce( |
| 181 | + new Response("x".repeat(1_048_577), { |
| 182 | + status: 500, |
| 183 | + headers: { "content-type": "text/plain" }, |
| 184 | + }), |
| 185 | + ); |
| 186 | + |
| 187 | + await expect( |
| 188 | + acquireQaCredentialLease({ |
| 189 | + kind: "telegram", |
| 190 | + source: "convex", |
| 191 | + role: "maintainer", |
| 192 | + env: { |
| 193 | + OPENCLAW_QA_CONVEX_SITE_URL: "https://qa-cred.example.convex.site", |
| 194 | + OPENCLAW_QA_CONVEX_SECRET_MAINTAINER: "maintainer-secret", |
| 195 | + }, |
| 196 | + fetchImpl, |
| 197 | + resolveEnvPayload: () => ({ groupId: "-1", driverToken: "unused", sutToken: "unused" }), |
| 198 | + parsePayload: (payload) => |
| 199 | + payload as { groupId: string; driverToken: string; sutToken: string }, |
| 200 | + }), |
| 201 | + ).rejects.toThrow("Convex credential broker: text response exceeds 1048576 bytes"); |
| 202 | + |
| 203 | + expect(fetchImpl).toHaveBeenCalledTimes(1); |
| 204 | + }); |
| 205 | + |
| 206 | + it("cancels a streaming convex broker failure body after the response cap", async () => { |
| 207 | + const broker = await startStreamingFailureBroker({}); |
| 208 | + try { |
| 209 | + await expect( |
| 210 | + acquireQaCredentialLease({ |
| 211 | + kind: "telegram", |
| 212 | + source: "convex", |
| 213 | + role: "maintainer", |
| 214 | + env: { |
| 215 | + OPENCLAW_QA_CONVEX_SITE_URL: broker.url, |
| 216 | + OPENCLAW_QA_CONVEX_SECRET_MAINTAINER: "maintainer-secret", |
| 217 | + OPENCLAW_QA_ALLOW_INSECURE_HTTP: "1", |
| 218 | + }, |
| 219 | + resolveEnvPayload: () => ({ groupId: "-1", driverToken: "unused", sutToken: "unused" }), |
| 220 | + parsePayload: (payload) => |
| 221 | + payload as { groupId: string; driverToken: string; sutToken: string }, |
| 222 | + }), |
| 223 | + ).rejects.toThrow("Convex credential broker: text response exceeds 1048576 bytes"); |
| 224 | + |
| 225 | + await broker.closePromise; |
| 226 | + expect(broker.getRequestCount()).toBe(1); |
| 227 | + expect(broker.getBytesWritten()).toBeLessThan(broker.totalBytes); |
| 228 | + } finally { |
| 229 | + await broker.stop(); |
| 230 | + } |
| 231 | + }); |
| 232 | + |
110 | 233 | it("hydrates chunked convex credential payloads after acquire", async () => { |
111 | 234 | const serialized = JSON.stringify({ |
112 | 235 | groupId: "-100123", |
|
0 commit comments