@@ -61,6 +61,10 @@ const SAW_VALID_ENTRY = Symbol('sawValidEntry')
6161const SAW_NULL_BLOCK = Symbol ( 'sawNullBlock' )
6262const SAW_EOF = Symbol ( 'sawEOF' )
6363const CLOSESTREAM = Symbol ( 'closeStream' )
64+ const MAX_DECOMPRESSION_RATIO = 1000
65+ const COMPRESSEDBYTESREAD = Symbol ( 'compressedBytesRead' )
66+ const DECOMPRESSEDBYTESREAD = Symbol ( 'decompressedBytesRead' )
67+ const CHECKDECOMPRESSIONRATIO = Symbol ( 'checkDecompressionRatio' )
6468
6569const noop = ( ) => true
6670
@@ -73,6 +77,7 @@ export class Parser extends EE implements Warner {
7377 filter : Exclude < TarOptions [ 'filter' ] , undefined >
7478 brotli ?: TarOptions [ 'brotli' ]
7579 zstd ?: TarOptions [ 'zstd' ]
80+ maxDecompressionRatio : number
7681
7782 writable : true = true
7883 readable : false = false ;
@@ -93,7 +98,9 @@ export class Parser extends EE implements Warner {
9398 [ SAW_EOF ] : boolean = false ;
9499 [ WRITING ] : boolean = false ;
95100 [ CONSUMING ] : boolean = false ;
96- [ EMITTEDEND ] : boolean = false
101+ [ EMITTEDEND ] : boolean = false ;
102+ [ COMPRESSEDBYTESREAD ] : number = 0 ;
103+ [ DECOMPRESSEDBYTESREAD ] : number = 0
97104
98105 constructor ( opt : TarOptions = { } ) {
99106 super ( )
@@ -120,6 +127,10 @@ export class Parser extends EE implements Warner {
120127 }
121128
122129 this . strict = ! ! opt . strict
130+ this . maxDecompressionRatio =
131+ typeof opt . maxDecompressionRatio === 'number' ?
132+ opt . maxDecompressionRatio
133+ : MAX_DECOMPRESSION_RATIO
123134 this . maxMetaEntrySize = opt . maxMetaEntrySize || maxMetaEntrySize
124135 this . filter = typeof opt . filter === 'function' ? opt . filter : noop
125136 // Unlike gzip, brotli doesn't have any magic bytes to identify it
@@ -374,14 +385,14 @@ export class Parser extends EE implements Warner {
374385
375386 case 'NextFileHasLongPath' :
376387 case 'OldGnuLongPath' : {
377- const ex = this [ EX ] ?? Object . create ( null )
388+ const ex : Pax = this [ EX ] ?? Object . create ( null )
378389 this [ EX ] = ex
379390 ex . path = this [ META ] . replace ( / \0 .* / , '' )
380391 break
381392 }
382393
383394 case 'NextFileHasLongLinkpath' : {
384- const ex = this [ EX ] || Object . create ( null )
395+ const ex : Pax = this [ EX ] || Object . create ( null )
385396 this [ EX ] = ex
386397 ex . linkpath = this [ META ] . replace ( / \0 .* / , '' )
387398 break
@@ -395,12 +406,31 @@ export class Parser extends EE implements Warner {
395406 }
396407
397408 abort ( error : Error ) {
409+ if ( this [ ABORTED ] ) {
410+ return
411+ }
398412 this [ ABORTED ] = true
399413 this . emit ( 'abort' , error )
400414 // always throws, even in non-strict mode
401415 this . warn ( 'TAR_ABORT' , error , { recoverable : false } )
402416 }
403417
418+ [ CHECKDECOMPRESSIONRATIO ] ( chunk : Buffer ) {
419+ this [ DECOMPRESSEDBYTESREAD ] += chunk . length
420+ const ratio = this [ DECOMPRESSEDBYTESREAD ] / this [ COMPRESSEDBYTESREAD ]
421+ if ( ratio > this . maxDecompressionRatio ) {
422+ this . abort (
423+ new Error (
424+ `max decompression ratio exceeded: ${ ratio . toFixed ( 2 ) } > ${
425+ this . maxDecompressionRatio
426+ } `,
427+ ) ,
428+ )
429+ return false
430+ }
431+ return true
432+ }
433+
404434 write (
405435 buffer : Uint8Array | string ,
406436 cb ?: ( err ?: Error | null ) => void ,
@@ -508,13 +538,22 @@ export class Parser extends EE implements Warner {
508538 this [ UNZIP ] === undefined ? new Unzip ( { } )
509539 : isZstd ? new ZstdDecompress ( { } )
510540 : new BrotliDecompress ( { } )
511- this [ UNZIP ] . on ( 'data' , chunk => this [ CONSUMECHUNK ] ( chunk ) )
512- this [ UNZIP ] . on ( 'error' , er => this . abort ( er as Error ) )
541+ this [ UNZIP ] . on ( 'data' , chunk => {
542+ if ( this [ CHECKDECOMPRESSIONRATIO ] ( chunk ) ) {
543+ this [ CONSUMECHUNK ] ( chunk )
544+ }
545+ } )
546+ this [ UNZIP ] . on ( 'error' , er => {
547+ if ( ! this [ ABORTED ] ) {
548+ this . abort ( er as Error )
549+ }
550+ } )
513551 this [ UNZIP ] . on ( 'end' , ( ) => {
514552 this [ ENDED ] = true
515553 this [ CONSUMECHUNK ] ( )
516554 } )
517555 this [ WRITING ] = true
556+ this [ COMPRESSEDBYTESREAD ] += chunk . length
518557 const ret = ! ! this [ UNZIP ] [ ended ? 'end' : 'write' ] ( chunk )
519558 this [ WRITING ] = false
520559 cb ?.( )
@@ -524,6 +563,7 @@ export class Parser extends EE implements Warner {
524563
525564 this [ WRITING ] = true
526565 if ( this [ UNZIP ] ) {
566+ this [ COMPRESSEDBYTESREAD ] += chunk . length
527567 this [ UNZIP ] . write ( chunk )
528568 } else {
529569 this [ CONSUMECHUNK ] ( chunk )
@@ -561,7 +601,7 @@ export class Parser extends EE implements Warner {
561601 ) {
562602 this [ EMITTEDEND ] = true
563603 const entry = this [ WRITEENTRY ]
564- if ( entry && entry . blockRemain ) {
604+ if ( entry ? .blockRemain ) {
565605 // truncated, likely a damaged file
566606 const have = this [ BUFFER ] ? this [ BUFFER ] . length : 0
567607 this . warn (
@@ -673,7 +713,10 @@ export class Parser extends EE implements Warner {
673713 if ( ! this [ ABORTED ] ) {
674714 if ( this [ UNZIP ] ) {
675715 /* c8 ignore start */
676- if ( chunk ) this [ UNZIP ] . write ( chunk )
716+ if ( chunk ) {
717+ this [ COMPRESSEDBYTESREAD ] += chunk . length
718+ this [ UNZIP ] . write ( chunk )
719+ }
677720 /* c8 ignore stop */
678721 this [ UNZIP ] . end ( )
679722 } else {
0 commit comments