From a2900c69f66b7e9c12b4016c9a8b0f23d7833a71 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Thu, 29 Oct 2026 15:52:08 +0000 Subject: [PATCH 1/3] fix(gaxios): enforce maxContentLength on headerless responses in Bun --- core/packages/gaxios/src/gaxios.ts | 31 ++++++++++++++++++------- core/packages/gaxios/test/test.getch.ts | 23 ++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/core/packages/gaxios/src/gaxios.ts b/core/packages/gaxios/src/gaxios.ts index cb51f4ece1c5..4fc3bb9dcf09 100644 --- a/core/packages/gaxios/src/gaxios.ts +++ b/core/packages/gaxios/src/gaxios.ts @@ -728,6 +728,8 @@ export class Gaxios implements FetchCompliance { proxy?: string; cert?: string; key?: string; + size?: number; + maxContentLength?: number; tls?: {cert?: string; key?: string}; }) : undefined; @@ -807,6 +809,7 @@ export class Gaxios implements FetchCompliance { } throw err; } + const maxSize = rawInit?.size ?? rawInit?.maxContentLength ?? 0; if ( res?.body && typeof Readable.fromWeb === 'function' && @@ -820,11 +823,23 @@ export class Gaxios implements FetchCompliance { const origArrayBuffer = res.arrayBuffer.bind(res); const origBlob = res.blob.bind(res); const readBuffer = async () => { + nodeStream ||= Readable.fromWeb(rawBody); const chunks: Buffer[] = []; - for await (const chunk of nodeStream!) { - chunks.push( - Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as Uint8Array), - ); + let totalBytes = 0; + for await (const chunk of nodeStream) { + const buf = Buffer.isBuffer(chunk) + ? chunk + : Buffer.from(chunk as Uint8Array); + totalBytes += buf.byteLength; + if (maxSize > 0 && totalBytes > maxSize) { + throw Object.assign( + new Error( + `content size at ${String((input as {url?: unknown})?.url ?? input)} over limit: ${maxSize}`, + ), + {type: 'max-size'}, + ); + } + chunks.push(buf); } return Buffer.concat(chunks); }; @@ -837,15 +852,15 @@ export class Gaxios implements FetchCompliance { enumerable: true, }); res.text = async () => { - if (!nodeStream) return origText(); + if (!nodeStream && maxSize <= 0) return origText(); return (await readBuffer()).toString('utf8'); }; res.json = async () => { - if (!nodeStream) return origJson(); + if (!nodeStream && maxSize <= 0) return origJson(); return JSON.parse(await res.text()); }; res.arrayBuffer = async () => { - if (!nodeStream) return origArrayBuffer(); + if (!nodeStream && maxSize <= 0) return origArrayBuffer(); const buf = await readBuffer(); return buf.buffer.slice( buf.byteOffset, @@ -853,7 +868,7 @@ export class Gaxios implements FetchCompliance { ); }; res.blob = async () => { - if (!nodeStream) return origBlob(); + if (!nodeStream && maxSize <= 0) return origBlob(); const buf = await readBuffer(); const contentType = res.headers?.get?.('content-type') ?? ''; return new Blob([buf], contentType ? {type: contentType} : undefined); diff --git a/core/packages/gaxios/test/test.getch.ts b/core/packages/gaxios/test/test.getch.ts index 031fb10ee683..092cb854b613 100644 --- a/core/packages/gaxios/test/test.getch.ts +++ b/core/packages/gaxios/test/test.getch.ts @@ -343,6 +343,23 @@ describe('🥁 configuration options', () => { scope.done(); }); + it('should enforce maxContentLength on responses without a Content-Length header', async () => { + const body = {hello: 'world'}; + for (const responseType of [ + 'json', + 'text', + 'arraybuffer', + 'blob', + ] as const) { + const scope = nock(url).get('/').reply(200, body); + await assert.rejects( + request({url, maxContentLength: 5, responseType}), + (err: Error) => err instanceof GaxiosError && /limit/.test(err.message), + ); + scope.done(); + } + }); + it('should support redirects by default', async () => { const body = {hello: '🌎'}; const scopes = [ @@ -658,6 +675,12 @@ describe('🥁 configuration options', () => { const jsonRes = await request({url}); assert.deepStrictEqual(jsonRes.data, responseBody); + await assert.rejects( + request({url, maxContentLength: 2}), + (err: Error) => + err instanceof GaxiosError && /over limit: 2/.test(err.message), + ); + globalThis.fetch = (async () => { throw {code: 'ECONNRESET', message: 'socket hang up'}; }) as typeof fetch; From 0cd43b0bd3282f3d765b31698038219231879cc4 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Fri, 30 Oct 2026 21:01:30 +0000 Subject: [PATCH 2/3] perf(gaxios): avoid per-chunk Buffer.from copy in readBuffer --- core/packages/gaxios/src/gaxios.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/core/packages/gaxios/src/gaxios.ts b/core/packages/gaxios/src/gaxios.ts index 4fc3bb9dcf09..99cb3e4ee256 100644 --- a/core/packages/gaxios/src/gaxios.ts +++ b/core/packages/gaxios/src/gaxios.ts @@ -824,13 +824,11 @@ export class Gaxios implements FetchCompliance { const origBlob = res.blob.bind(res); const readBuffer = async () => { nodeStream ||= Readable.fromWeb(rawBody); - const chunks: Buffer[] = []; + const chunks: Uint8Array[] = []; let totalBytes = 0; for await (const chunk of nodeStream) { - const buf = Buffer.isBuffer(chunk) - ? chunk - : Buffer.from(chunk as Uint8Array); - totalBytes += buf.byteLength; + const bytes = chunk as Uint8Array; + totalBytes += bytes.byteLength; if (maxSize > 0 && totalBytes > maxSize) { throw Object.assign( new Error( @@ -839,9 +837,9 @@ export class Gaxios implements FetchCompliance { {type: 'max-size'}, ); } - chunks.push(buf); + chunks.push(bytes); } - return Buffer.concat(chunks); + return Buffer.concat(chunks, totalBytes); }; Object.defineProperty(res, 'body', { get() { From eb5273f3ca6bfc1eee1af703c8ff15c9bb188e1f Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Fri, 30 Oct 2026 21:01:38 +0000 Subject: [PATCH 3/3] test(gaxios): assert stream byte-limit error message on headerless nock reply --- core/packages/gaxios/test/test.getch.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/packages/gaxios/test/test.getch.ts b/core/packages/gaxios/test/test.getch.ts index 092cb854b613..277c5f1b3a9b 100644 --- a/core/packages/gaxios/test/test.getch.ts +++ b/core/packages/gaxios/test/test.getch.ts @@ -351,10 +351,15 @@ describe('🥁 configuration options', () => { 'arraybuffer', 'blob', ] as const) { + // nock's .reply(200, body) omits Content-Length unless .replyContentLength() + // or an explicit 'content-length' header is specified, so this exercises + // the stream byte-counting limit ('over limit: 5') rather than the upfront + // Content-Length header check ("Response's `Content-Length` is over the limit."). const scope = nock(url).get('/').reply(200, body); await assert.rejects( request({url, maxContentLength: 5, responseType}), - (err: Error) => err instanceof GaxiosError && /limit/.test(err.message), + (err: Error) => + err instanceof GaxiosError && /over limit: 5/.test(err.message), ); scope.done(); }