From ac063326717b67144e8bae628920e9e64708bb07 Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Sat, 12 Sep 2026 02:14:27 +0900 Subject: [PATCH 1/2] test(gateway): reject fragmented health response accumulation --- .../orchestrator-gateway-stream-bound.test.ts | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/test/orchestrator-gateway-stream-bound.test.ts b/test/orchestrator-gateway-stream-bound.test.ts index 37fde5e64..fddd7cbca 100644 --- a/test/orchestrator-gateway-stream-bound.test.ts +++ b/test/orchestrator-gateway-stream-bound.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { verifyOrchestratorHealthz } from "../scripts/lib/orchestrator-gateway.mjs"; @@ -48,4 +48,46 @@ describe("contextual-orchestrator streamed health response", () => { expect(released).toBe(true); expect(arrayBufferCalled).toBe(false); }); + + it("does not retain fragmented chunks for a second concatenation allocation", async () => { + const payload = new TextEncoder().encode( + JSON.stringify({ status: "ok", service: "contextual-orchestrator" }), + ); + let offset = 0; + let released = false; + const reader = { + async read() { + if (offset >= payload.length) return { done: true, value: undefined }; + const value = payload.slice(offset, offset + 1); + offset += 1; + return { done: false, value }; + }, + async cancel() {}, + releaseLock() { + released = true; + }, + }; + const response = { + ok: true, + status: 200, + headers: { get: () => null }, + body: { getReader: () => reader }, + async arrayBuffer() { + throw new Error("streaming response must not fall back to arrayBuffer"); + }, + } as unknown as Response; + const concatSpy = vi.spyOn(Buffer, "concat"); + + try { + await expect( + verifyOrchestratorHealthz("https://orchestrator.example/healthz", { + fetchImpl: (async () => response) as typeof fetch, + }), + ).resolves.toEqual({ status: "ok", service: "contextual-orchestrator" }); + expect(concatSpy).not.toHaveBeenCalled(); + expect(released).toBe(true); + } finally { + concatSpy.mockRestore(); + } + }); }); From 73f13b3a3826c5d6f7aafc89d1ff23747f8cf8e9 Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Sat, 12 Sep 2026 02:18:50 +0900 Subject: [PATCH 2/2] fix(gateway): stream health response into fixed buffer --- scripts/lib/orchestrator-gateway.mjs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/lib/orchestrator-gateway.mjs b/scripts/lib/orchestrator-gateway.mjs index dd2f7170e..770787bdf 100644 --- a/scripts/lib/orchestrator-gateway.mjs +++ b/scripts/lib/orchestrator-gateway.mjs @@ -347,7 +347,7 @@ export async function verifyOrchestratorHealthz(healthzUrl, options = {}) { let raw; const reader = response.body?.getReader?.(); if (reader) { - const chunks = []; + const body = Buffer.allocUnsafe(HEALTH_BODY_LIMIT_BYTES); let totalBytes = 0; let completed = false; try { @@ -357,12 +357,12 @@ export async function verifyOrchestratorHealthz(healthzUrl, options = {}) { completed = true; break; } - const chunk = Buffer.from(result.value ?? new Uint8Array()); - totalBytes += chunk.length; - if (totalBytes > HEALTH_BODY_LIMIT_BYTES) { + const chunk = result.value ?? new Uint8Array(); + if (totalBytes + chunk.byteLength > HEALTH_BODY_LIMIT_BYTES) { throw new Error("contextual-orchestrator health response is too large"); } - chunks.push(chunk); + body.set(chunk, totalBytes); + totalBytes += chunk.byteLength; } } finally { if (!completed) { @@ -374,7 +374,7 @@ export async function verifyOrchestratorHealthz(healthzUrl, options = {}) { } reader.releaseLock(); } - raw = Buffer.concat(chunks, totalBytes); + raw = body.subarray(0, totalBytes); } else { raw = Buffer.from( await Promise.race([response.arrayBuffer(), timeoutPromise]),