@@ -124,6 +124,11 @@ function zlibBufferOnData(chunk) {
124124 else
125125 this . buffers . push ( chunk ) ;
126126 this . nread += chunk . length ;
127+ if ( this . nread > this . _maxOutputLength ) {
128+ this . close ( ) ;
129+ this . removeAllListeners ( 'end' ) ;
130+ this . cb ( new ERR_BUFFER_TOO_LARGE ( this . _maxOutputLength ) ) ;
131+ }
127132}
128133
129134function zlibBufferOnError ( err ) {
@@ -134,9 +139,7 @@ function zlibBufferOnError(err) {
134139function zlibBufferOnEnd ( ) {
135140 let buf ;
136141 let err ;
137- if ( this . nread >= kMaxLength ) {
138- err = new ERR_BUFFER_TOO_LARGE ( ) ;
139- } else if ( this . nread === 0 ) {
142+ if ( this . nread === 0 ) {
140143 buf = Buffer . alloc ( 0 ) ;
141144 } else {
142145 const bufs = this . buffers ;
@@ -231,6 +234,7 @@ const checkRangesOrGetDefault = hideStackFrames(
231234// The base class for all Zlib-style streams.
232235function ZlibBase ( opts , mode , handle , { flush, finishFlush, fullFlush } ) {
233236 let chunkSize = Z_DEFAULT_CHUNK ;
237+ let maxOutputLength = kMaxLength ;
234238 // The ZlibBase class is not exported to user land, the mode should only be
235239 // passed in by us.
236240 assert ( typeof mode === 'number' ) ;
@@ -253,6 +257,10 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
253257 opts . finishFlush , 'options.finishFlush' ,
254258 Z_NO_FLUSH , Z_BLOCK , finishFlush ) ;
255259
260+ maxOutputLength = checkRangesOrGetDefault (
261+ opts . maxOutputLength , 'options.maxOutputLength' ,
262+ 1 , kMaxLength , kMaxLength ) ;
263+
256264 if ( opts . encoding || opts . objectMode || opts . writableObjectMode ) {
257265 opts = { ...opts } ;
258266 opts . encoding = null ;
@@ -276,6 +284,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
276284 this . _finishFlushFlag = finishFlush ;
277285 this . _defaultFullFlushFlag = fullFlush ;
278286 this . _info = opts && opts . info ;
287+ this . _maxOutputLength = maxOutputLength ;
279288}
280289ObjectSetPrototypeOf ( ZlibBase . prototype , Transform . prototype ) ;
281290ObjectSetPrototypeOf ( ZlibBase , Transform ) ;
@@ -450,6 +459,12 @@ function processChunkSync(self, chunk, flushFlag) {
450459 else
451460 buffers . push ( out ) ;
452461 nread += out . byteLength ;
462+
463+ if ( nread > self . _maxOutputLength ) {
464+ _close ( self ) ;
465+ throw new ERR_BUFFER_TOO_LARGE ( self . _maxOutputLength ) ;
466+ }
467+
453468 } else {
454469 assert ( have === 0 , 'have should not go down' ) ;
455470 }
@@ -476,10 +491,6 @@ function processChunkSync(self, chunk, flushFlag) {
476491 self . bytesWritten = inputRead ;
477492 _close ( self ) ;
478493
479- if ( nread >= kMaxLength ) {
480- throw new ERR_BUFFER_TOO_LARGE ( ) ;
481- }
482-
483494 if ( nread === 0 )
484495 return Buffer . alloc ( 0 ) ;
485496
0 commit comments