diff --git a/core/packages/gaxios/src/gaxios.ts b/core/packages/gaxios/src/gaxios.ts index cb51f4ece1c5..99cb3e4ee256 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,13 +823,23 @@ export class Gaxios implements FetchCompliance { const origArrayBuffer = res.arrayBuffer.bind(res); const origBlob = res.blob.bind(res); const readBuffer = async () => { - const chunks: Buffer[] = []; - for await (const chunk of nodeStream!) { - chunks.push( - Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as Uint8Array), - ); + nodeStream ||= Readable.fromWeb(rawBody); + const chunks: Uint8Array[] = []; + let totalBytes = 0; + for await (const chunk of nodeStream) { + const bytes = chunk as Uint8Array; + totalBytes += bytes.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(bytes); } - return Buffer.concat(chunks); + return Buffer.concat(chunks, totalBytes); }; Object.defineProperty(res, 'body', { get() { @@ -837,15 +850,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 +866,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..277c5f1b3a9b 100644 --- a/core/packages/gaxios/test/test.getch.ts +++ b/core/packages/gaxios/test/test.getch.ts @@ -343,6 +343,28 @@ 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) { + // 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 && /over limit: 5/.test(err.message), + ); + scope.done(); + } + }); + it('should support redirects by default', async () => { const body = {hello: '🌎'}; const scopes = [ @@ -658,6 +680,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;