fix(e2e): harden kitchen sink probe body caps

This commit is contained in:
Vincent Koc
2026-05-29 20:12:23 +02:00
parent 75ef73d4f7
commit 18d2bc441c
2 changed files with 71 additions and 4 deletions

View File

@@ -512,9 +512,22 @@ export async function fetchJson(url, options = {}) {
}
export async function readBoundedResponseText(response, byteLimit = FETCH_BODY_MAX_BYTES) {
const contentLength = response.headers?.get?.("content-length");
if (contentLength) {
const parsedContentLength = Number(contentLength);
if (Number.isFinite(parsedContentLength) && parsedContentLength > byteLimit) {
await response.body?.cancel?.().catch(() => undefined);
throw createFetchBodyTooLargeError(byteLimit);
}
}
const reader = response.body?.getReader?.();
if (!reader) {
return await response.text();
const text = await response.text();
if (Buffer.byteLength(text, "utf8") > byteLimit) {
throw createFetchBodyTooLargeError(byteLimit);
}
return text;
}
const chunks = [];
let totalBytes = 0;
@@ -527,15 +540,19 @@ export async function readBoundedResponseText(response, byteLimit = FETCH_BODY_M
totalBytes += chunk.byteLength;
if (totalBytes > byteLimit) {
await reader.cancel().catch(() => undefined);
throw Object.assign(new Error(`fetch response body exceeded ${byteLimit} bytes`), {
code: "ETOOBIG",
});
throw createFetchBodyTooLargeError(byteLimit);
}
chunks.push(chunk);
}
return Buffer.concat(chunks, totalBytes).toString("utf8");
}
function createFetchBodyTooLargeError(byteLimit) {
return Object.assign(new Error(`fetch response body exceeded ${byteLimit} bytes`), {
code: "ETOOBIG",
});
}
function configureKitchenSink(env, port) {
const configPath = env.OPENCLAW_CONFIG_PATH;
const config = fs.existsSync(configPath) ? readJson(configPath) : {};