@@ -60,6 +60,10 @@ const SAW_VALID_ENTRY = Symbol('sawValidEntry');
6060const SAW_NULL_BLOCK = Symbol ( 'sawNullBlock' ) ;
6161const SAW_EOF = Symbol ( 'sawEOF' ) ;
6262const CLOSESTREAM = Symbol ( 'closeStream' ) ;
63+ const MAX_DECOMPRESSION_RATIO = 1000 ;
64+ const COMPRESSEDBYTESREAD = Symbol ( 'compressedBytesRead' ) ;
65+ const DECOMPRESSEDBYTESREAD = Symbol ( 'decompressedBytesRead' ) ;
66+ const CHECKDECOMPRESSIONRATIO = Symbol ( 'checkDecompressionRatio' ) ;
6367const noop = ( ) => true ;
6468class Parser extends events_1 . EventEmitter {
6569 file ;
@@ -68,6 +72,7 @@ class Parser extends events_1.EventEmitter {
6872 filter ;
6973 brotli ;
7074 zstd ;
75+ maxDecompressionRatio ;
7176 writable = true ;
7277 readable = false ;
7378 [ QUEUE ] = [ ] ;
@@ -87,6 +92,8 @@ class Parser extends events_1.EventEmitter {
8792 [ WRITING ] = false ;
8893 [ CONSUMING ] = false ;
8994 [ EMITTEDEND ] = false ;
95+ [ COMPRESSEDBYTESREAD ] = 0 ;
96+ [ DECOMPRESSEDBYTESREAD ] = 0 ;
9097 constructor ( opt = { } ) {
9198 super ( ) ;
9299 this . file = opt . file || '' ;
@@ -109,6 +116,10 @@ class Parser extends events_1.EventEmitter {
109116 } ) ;
110117 }
111118 this . strict = ! ! opt . strict ;
119+ this . maxDecompressionRatio =
120+ typeof opt . maxDecompressionRatio === 'number' ?
121+ opt . maxDecompressionRatio
122+ : MAX_DECOMPRESSION_RATIO ;
112123 this . maxMetaEntrySize = opt . maxMetaEntrySize || maxMetaEntrySize ;
113124 this . filter = typeof opt . filter === 'function' ? opt . filter : noop ;
114125 // Unlike gzip, brotli doesn't have any magic bytes to identify it
@@ -362,11 +373,23 @@ class Parser extends events_1.EventEmitter {
362373 }
363374 }
364375 abort ( error ) {
376+ if ( this [ ABORTED ] ) {
377+ return ;
378+ }
365379 this [ ABORTED ] = true ;
366380 this . emit ( 'abort' , error ) ;
367381 // always throws, even in non-strict mode
368382 this . warn ( 'TAR_ABORT' , error , { recoverable : false } ) ;
369383 }
384+ [ CHECKDECOMPRESSIONRATIO ] ( chunk ) {
385+ this [ DECOMPRESSEDBYTESREAD ] += chunk . length ;
386+ const ratio = this [ DECOMPRESSEDBYTESREAD ] / this [ COMPRESSEDBYTESREAD ] ;
387+ if ( ratio > this . maxDecompressionRatio ) {
388+ this . abort ( new Error ( `max decompression ratio exceeded: ${ ratio . toFixed ( 2 ) } > ${ this . maxDecompressionRatio } ` ) ) ;
389+ return false ;
390+ }
391+ return true ;
392+ }
370393 write ( chunk , encoding , cb ) {
371394 if ( typeof encoding === 'function' ) {
372395 cb = encoding ;
@@ -450,13 +473,22 @@ class Parser extends events_1.EventEmitter {
450473 this [ UNZIP ] === undefined ? new minizlib_1 . Unzip ( { } )
451474 : isZstd ? new minizlib_1 . ZstdDecompress ( { } )
452475 : new minizlib_1 . BrotliDecompress ( { } ) ;
453- this [ UNZIP ] . on ( 'data' , chunk => this [ CONSUMECHUNK ] ( chunk ) ) ;
454- this [ UNZIP ] . on ( 'error' , er => this . abort ( er ) ) ;
476+ this [ UNZIP ] . on ( 'data' , chunk => {
477+ if ( this [ CHECKDECOMPRESSIONRATIO ] ( chunk ) ) {
478+ this [ CONSUMECHUNK ] ( chunk ) ;
479+ }
480+ } ) ;
481+ this [ UNZIP ] . on ( 'error' , er => {
482+ if ( ! this [ ABORTED ] ) {
483+ this . abort ( er ) ;
484+ }
485+ } ) ;
455486 this [ UNZIP ] . on ( 'end' , ( ) => {
456487 this [ ENDED ] = true ;
457488 this [ CONSUMECHUNK ] ( ) ;
458489 } ) ;
459490 this [ WRITING ] = true ;
491+ this [ COMPRESSEDBYTESREAD ] += chunk . length ;
460492 const ret = ! ! this [ UNZIP ] [ ended ? 'end' : 'write' ] ( chunk ) ;
461493 this [ WRITING ] = false ;
462494 cb ?. ( ) ;
@@ -465,6 +497,7 @@ class Parser extends events_1.EventEmitter {
465497 }
466498 this [ WRITING ] = true ;
467499 if ( this [ UNZIP ] ) {
500+ this [ COMPRESSEDBYTESREAD ] += chunk . length ;
468501 this [ UNZIP ] . write ( chunk ) ;
469502 }
470503 else {
@@ -495,7 +528,7 @@ class Parser extends events_1.EventEmitter {
495528 ! this [ CONSUMING ] ) {
496529 this [ EMITTEDEND ] = true ;
497530 const entry = this [ WRITEENTRY ] ;
498- if ( entry && entry . blockRemain ) {
531+ if ( entry ? .blockRemain ) {
499532 // truncated, likely a damaged file
500533 const have = this [ BUFFER ] ? this [ BUFFER ] . length : 0 ;
501534 this . warn ( 'TAR_BAD_ARCHIVE' , `Truncated input (needed ${ entry . blockRemain } more bytes, only ${ have } available)` , { entry } ) ;
@@ -589,8 +622,10 @@ class Parser extends events_1.EventEmitter {
589622 if ( ! this [ ABORTED ] ) {
590623 if ( this [ UNZIP ] ) {
591624 /* c8 ignore start */
592- if ( chunk )
625+ if ( chunk ) {
626+ this [ COMPRESSEDBYTESREAD ] += chunk . length ;
593627 this [ UNZIP ] . write ( chunk ) ;
628+ }
594629 /* c8 ignore stop */
595630 this [ UNZIP ] . end ( ) ;
596631 }
0 commit comments