@@ -122,6 +122,11 @@ function zlibBufferOnData(chunk) {
122122 else
123123 this . buffers . push ( chunk ) ;
124124 this . nread += chunk . length ;
125+ if ( this . nread > this . _maxOutputLength ) {
126+ this . close ( ) ;
127+ this . removeAllListeners ( 'end' ) ;
128+ this . cb ( new ERR_BUFFER_TOO_LARGE ( this . _maxOutputLength ) ) ;
129+ }
125130}
126131
127132function zlibBufferOnError ( err ) {
@@ -132,9 +137,7 @@ function zlibBufferOnError(err) {
132137function zlibBufferOnEnd ( ) {
133138 let buf ;
134139 let err ;
135- if ( this . nread >= kMaxLength ) {
136- err = new ERR_BUFFER_TOO_LARGE ( ) ;
137- } else if ( this . nread === 0 ) {
140+ if ( this . nread === 0 ) {
138141 buf = Buffer . alloc ( 0 ) ;
139142 } else {
140143 const bufs = this . buffers ;
@@ -230,6 +233,7 @@ const checkRangesOrGetDefault = hideStackFrames(
230233// The base class for all Zlib-style streams.
231234function ZlibBase ( opts , mode , handle , { flush, finishFlush, fullFlush } ) {
232235 let chunkSize = Z_DEFAULT_CHUNK ;
236+ let maxOutputLength = kMaxLength ;
233237 // The ZlibBase class is not exported to user land, the mode should only be
234238 // passed in by us.
235239 assert ( typeof mode === 'number' ) ;
@@ -252,6 +256,10 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
252256 opts . finishFlush , 'options.finishFlush' ,
253257 Z_NO_FLUSH , Z_BLOCK , finishFlush ) ;
254258
259+ maxOutputLength = checkRangesOrGetDefault (
260+ opts . maxOutputLength , 'options.maxOutputLength' ,
261+ 1 , kMaxLength , kMaxLength ) ;
262+
255263 if ( opts . encoding || opts . objectMode || opts . writableObjectMode ) {
256264 opts = { ...opts } ;
257265 opts . encoding = null ;
@@ -276,6 +284,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
276284 this . _defaultFullFlushFlag = fullFlush ;
277285 this . once ( 'end' , _close . bind ( null , this ) ) ;
278286 this . _info = opts && opts . info ;
287+ this . _maxOutputLength = maxOutputLength ;
279288}
280289ObjectSetPrototypeOf ( ZlibBase . prototype , Transform . prototype ) ;
281290ObjectSetPrototypeOf ( ZlibBase , Transform ) ;
@@ -445,6 +454,12 @@ function processChunkSync(self, chunk, flushFlag) {
445454 else
446455 buffers . push ( out ) ;
447456 nread += out . byteLength ;
457+
458+ if ( nread > self . _maxOutputLength ) {
459+ _close ( self ) ;
460+ throw new ERR_BUFFER_TOO_LARGE ( self . _maxOutputLength ) ;
461+ }
462+
448463 } else {
449464 assert ( have === 0 , 'have should not go down' ) ;
450465 }
@@ -471,10 +486,6 @@ function processChunkSync(self, chunk, flushFlag) {
471486 self . bytesWritten = inputRead ;
472487 _close ( self ) ;
473488
474- if ( nread >= kMaxLength ) {
475- throw new ERR_BUFFER_TOO_LARGE ( ) ;
476- }
477-
478489 if ( nread === 0 )
479490 return Buffer . alloc ( 0 ) ;
480491
0 commit comments