Skip to content

Commit d3a59de

Browse files
committed
fix(qa-lab): bound convex broker response bodies
1 parent cb1e822 commit d3a59de

2 files changed

Lines changed: 135 additions & 1 deletion

File tree

‎extensions/qa-lab/src/live-transports/shared/credential-lease.runtime.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Qa Lab tests cover credential lease plugin behavior.
2+
import { createServer } from "node:http";
23
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
34
import { afterEach, describe, expect, it, vi } from "vitest";
45
import {
@@ -39,6 +40,74 @@ function fetchInit(fetchImpl: FetchMock, index = 0): RequestInit {
3940
return init;
4041
}
4142

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+
42111
describe("credential lease runtime", () => {
43112
afterEach(() => {
44113
vi.restoreAllMocks();
@@ -107,6 +176,60 @@ describe("credential lease runtime", () => {
107176
expect(headers.authorization).toBe("Bearer maintainer-secret");
108177
});
109178

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+
110233
it("hydrates chunked convex credential payloads after acquire", async () => {
111234
const serialized = JSON.stringify({
112235
groupId: "-100123",

‎extensions/qa-lab/src/live-transports/shared/credential-lease.runtime.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { randomUUID } from "node:crypto";
33
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
44
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
5+
import { readProviderTextResponse } from "openclaw/plugin-sdk/provider-http";
56
import { z } from "zod";
67
import {
78
isQaCredentialTruthyOptIn,
@@ -19,6 +20,7 @@ const DEFAULT_HTTP_TIMEOUT_MS = 15_000;
1920
const DEFAULT_LEASE_TTL_MS = 20 * 60 * 1_000;
2021
const DEFAULT_CHUNKED_PAYLOAD_MAX_BYTES = 64 * 1024 * 1024;
2122
const DEFAULT_CHUNKED_PAYLOAD_MAX_CHUNKS = 4096;
23+
const CONVEX_BROKER_RESPONSE_MAX_BYTES = 1 * 1024 * 1024;
2224
const CHUNKED_PAYLOAD_MAX_BYTES_ENV = "OPENCLAW_QA_CREDENTIAL_PAYLOAD_MAX_BYTES";
2325
const CHUNKED_PAYLOAD_MAX_CHUNKS_ENV = "OPENCLAW_QA_CREDENTIAL_PAYLOAD_MAX_CHUNKS";
2426
const RETRY_BACKOFF_MS = [500, 1_000, 2_000, 4_000, 5_000] as const;
@@ -282,6 +284,7 @@ async function postConvexBroker(params: {
282284
authToken: string;
283285
body: Record<string, unknown>;
284286
fetchImpl: typeof fetch;
287+
maxBytes: number;
285288
timeoutMs: number;
286289
url: string;
287290
}): Promise<unknown> {
@@ -296,7 +299,11 @@ async function postConvexBroker(params: {
296299
signal: AbortSignal.timeout(timeoutMs),
297300
});
298301

299-
const text = await response.text();
302+
// Keep ordinary broker responses small, while allowing chunk payloads to use
303+
// the larger declared payload ceiling.
304+
const text = await readProviderTextResponse(response, "Convex credential broker", {
305+
maxBytes: params.maxBytes,
306+
});
300307
const payload: unknown = (() => {
301308
if (!text.trim()) {
302309
return undefined;
@@ -341,6 +348,7 @@ async function resolveConvexCredentialPayload(params: {
341348
for (let index = 0; index < marker.chunkCount; index += 1) {
342349
const payload = await postConvexBroker({
343350
fetchImpl: params.fetchImpl,
351+
maxBytes: params.config.payloadMaxBytes,
344352
timeoutMs: params.config.httpTimeoutMs,
345353
authToken: params.config.authToken,
346354
url: params.config.payloadChunkUrl,
@@ -437,6 +445,7 @@ export async function acquireQaCredentialLease<TPayload>(
437445
try {
438446
const payload = await postConvexBroker({
439447
fetchImpl,
448+
maxBytes: CONVEX_BROKER_RESPONSE_MAX_BYTES,
440449
timeoutMs: config.httpTimeoutMs,
441450
authToken: config.authToken,
442451
url: config.acquireUrl,
@@ -452,6 +461,7 @@ export async function acquireQaCredentialLease<TPayload>(
452461
const releaseLease = async () => {
453462
const releasePayload = await postConvexBroker({
454463
fetchImpl,
464+
maxBytes: CONVEX_BROKER_RESPONSE_MAX_BYTES,
455465
timeoutMs: config.httpTimeoutMs,
456466
authToken: config.authToken,
457467
url: config.releaseUrl,
@@ -503,6 +513,7 @@ export async function acquireQaCredentialLease<TPayload>(
503513
async heartbeat() {
504514
const heartbeatPayload = await postConvexBroker({
505515
fetchImpl,
516+
maxBytes: CONVEX_BROKER_RESPONSE_MAX_BYTES,
506517
timeoutMs: config.httpTimeoutMs,
507518
authToken: config.authToken,
508519
url: config.heartbeatUrl,

0 commit comments

Comments
 (0)