Skip to content

Commit 7604a19

Browse files
committed
fix(http-body): fail closed in no-reader fallback to avoid whole-body arrayBuffer
Replace the Content-Length guard approach with a simpler fail-closed strategy: when the response has no ReadableStream reader, return empty immediately instead of falling back to arrayBuffer()/text(). The streaming path (which handles all production HTTP responses in Node.js undici fetch) is unchanged. - Remove arrayBuffer() fallback from readResponsePrefix no-reader branch - Rewrite body-less path tests to use streaming Response with Blob - Add regression test asserting no-reader path never calls arrayBuffer() Ref. #101270
1 parent 1ca556d commit 7604a19

2 files changed

Lines changed: 26 additions & 46 deletions

File tree

‎src/infra/http-body.ts‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,9 @@ export async function readResponsePrefix(
201201
if (!body) {
202202
return { buffer: Buffer.alloc(0), size: 0, truncated: false };
203203
}
204-
// No ReadableStream reader available (e.g., synthetic Response).
205-
// Check Content-Length to avoid materializing an oversized body.
206-
const contentLength = response.headers?.get?.("content-length") ?? null;
207-
if (contentLength !== null) {
208-
const cl = Number.parseInt(contentLength, 10);
209-
if (!Number.isNaN(cl) && cl > maxBytes) {
210-
return { buffer: Buffer.alloc(0), size: cl, truncated: true };
211-
}
212-
}
213-
const fallback = Buffer.from(await response.arrayBuffer());
214-
if (fallback.length > maxBytes) {
215-
return {
216-
buffer: fallback.subarray(0, maxBytes),
217-
size: fallback.length,
218-
truncated: true,
219-
};
220-
}
221-
return { buffer: fallback, size: fallback.length, truncated: false };
204+
// No ReadableStream reader available — fail closed to avoid materializing
205+
// the entire response body via arrayBuffer()/text().
206+
return { buffer: Buffer.alloc(0), size: 0, truncated: true };
222207
}
223208

224209
const reader = body.getReader();

‎src/infra/http-error-body.test.ts‎

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
import { describe, expect, it } from "vitest";
22
import { readResponseBodySnippet } from "./http-error-body.js";
33

4-
function bodyLessResponse(text: string): Response {
5-
return {
6-
body: {} as ReadableStream,
7-
text: async () => text,
8-
arrayBuffer: async () => new TextEncoder().encode(text).buffer,
9-
} as unknown as Response;
4+
function streamResponseFromText(text: string): Response {
5+
return new Response(new Blob([text]).stream());
106
}
117

128
describe("readResponseBodySnippet", () => {
13-
it("returns full text when under both limits (body-less path)", async () => {
9+
it("returns full text when under both limits", async () => {
1410
const text = "short text";
15-
const result = await readResponseBodySnippet(bodyLessResponse(text), {
11+
const result = await readResponseBodySnippet(streamResponseFromText(text), {
1612
maxBytes: 1024,
1713
maxChars: 50,
1814
});
1915
expect(result).toBe(text);
2016
});
2117

22-
it("truncates by maxChars when under maxBytes (body-less path)", async () => {
18+
it("truncates by maxChars when under maxBytes", async () => {
2319
const text = "abcdefghij";
24-
const result = await readResponseBodySnippet(bodyLessResponse(text), {
20+
const result = await readResponseBodySnippet(streamResponseFromText(text), {
2521
maxBytes: 1024,
2622
maxChars: 5,
2723
});
2824
expect(result).toBe("abcde");
2925
});
3026

31-
it("truncates by maxBytes in the body-less path", async () => {
27+
it("truncates by maxBytes before maxChars", async () => {
3228
const text = "a".repeat(200);
33-
const result = await readResponseBodySnippet(bodyLessResponse(text), {
29+
const result = await readResponseBodySnippet(streamResponseFromText(text), {
3430
maxBytes: 50,
3531
maxChars: 500,
3632
});
@@ -39,9 +35,9 @@ describe("readResponseBodySnippet", () => {
3935
expect(result.length).toBeLessThan(text.length);
4036
});
4137

42-
it("enforces maxBytes before maxChars in the body-less path", async () => {
38+
it("enforces maxBytes before maxChars", async () => {
4339
const text = "a".repeat(500);
44-
const result = await readResponseBodySnippet(bodyLessResponse(text), {
40+
const result = await readResponseBodySnippet(streamResponseFromText(text), {
4541
maxBytes: 30,
4642
maxChars: 500,
4743
});
@@ -52,16 +48,16 @@ describe("readResponseBodySnippet", () => {
5248
it("does not split multi-byte UTF-8 characters at the byte boundary", async () => {
5349
// U+1F600 (😀) is 4 bytes in UTF-8: F0 9F 98 80
5450
const text = "ab😀cd";
55-
// 2 ASCII bytes (ab) + cut before the 4-byte emoji
56-
// Without stream flag, incomplete multi-byte produces U+FFFD
57-
const result = await readResponseBodySnippet(bodyLessResponse(text), {
51+
// 2 ASCII bytes (ab) + cut before the 4-byte emoji.
52+
// Without stream:true flag, incomplete multi-byte produces U+FFFD.
53+
const result = await readResponseBodySnippet(streamResponseFromText(text), {
5854
maxBytes: 3,
5955
maxChars: 100,
6056
});
6157
expect(result).toBe("ab�");
6258
});
6359

64-
it("stream path still enforces maxBytes", async () => {
60+
it("stream path enforces maxBytes", async () => {
6561
const data = new Uint8Array(500).fill(97); // 500 'a' bytes
6662
const response = new Response(new Blob([data]).stream());
6763
const result = await readResponseBodySnippet(response, {
@@ -72,7 +68,7 @@ describe("readResponseBodySnippet", () => {
7268
expect(byteLen).toBeLessThanOrEqual(100);
7369
});
7470

75-
it("stream path still enforces maxChars", async () => {
71+
it("stream path enforces maxChars", async () => {
7672
const data = new Uint8Array(500).fill(97);
7773
const response = new Response(new Blob([data]).stream());
7874
const result = await readResponseBodySnippet(response, {
@@ -82,34 +78,33 @@ describe("readResponseBodySnippet", () => {
8278
expect(result.length).toBeLessThanOrEqual(10);
8379
});
8480

85-
it("returns empty string when maxBytes is 0 (body-less path)", async () => {
86-
const result = await readResponseBodySnippet(bodyLessResponse("some text"), {
81+
it("returns empty string when maxBytes is 0", async () => {
82+
const result = await readResponseBodySnippet(streamResponseFromText("some text"), {
8783
maxBytes: 0,
8884
maxChars: 100,
8985
});
9086
expect(result).toBe("");
9187
});
9288

9389
it("returns empty string for empty response body", async () => {
94-
const result = await readResponseBodySnippet(bodyLessResponse(""), {
90+
const result = await readResponseBodySnippet(streamResponseFromText(""), {
9591
maxBytes: 1024,
9692
maxChars: 50,
9793
});
9894
expect(result).toBe("");
9995
});
10096

101-
it("avoids arrayBuffer() when Content-Length exceeds maxBytes (body-less path)", async () => {
97+
it("fails closed when body has no getReader (avoids arrayBuffer)", async () => {
10298
let arrayBufferCalled = false;
103-
const bigResponse = {
99+
const noReaderResponse = {
104100
body: {} as ReadableStream,
105-
headers: { get: () => "1000" },
106101
arrayBuffer: async () => {
107102
arrayBufferCalled = true;
108-
return new ArrayBuffer(0);
103+
return new TextEncoder().encode("large body").buffer;
109104
},
110105
} as unknown as Response;
111-
const result = await readResponseBodySnippet(bigResponse, {
112-
maxBytes: 50,
106+
const result = await readResponseBodySnippet(noReaderResponse, {
107+
maxBytes: 1024,
113108
maxChars: 100,
114109
});
115110
expect(arrayBufferCalled).toBe(false);

0 commit comments

Comments
 (0)