Skip to content

Commit 2812e93

Browse files
Jvr2022isaacs
authored andcommitted
add maxDecompressionRatio guard against explosive decompression
EDIT(@isaacs): add to readme, remove dead code, full test coverage
1 parent 9ecd4d2 commit 2812e93

6 files changed

Lines changed: 251 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ The following options are supported:
570570
defaults to 1024. Anything deeper than the limit will raise a
571571
warning and skip the entry. Set to `Infinity` to remove the
572572
limitation.
573+
- `maxDecompressionRatio` Defaults to 1000. The maximum ratio of
574+
decommpressed bytes to compressed bytes, in a compressed
575+
archive. Set to `Infinity` to allow explosive decompression.
573576

574577
The following options are mostly internal, but can be modified in some
575578
advanced use cases, such as re-using caches between runs.
@@ -629,6 +632,9 @@ The following options are supported:
629632
complete until the entry data is consumed.
630633
- `onwarn` A function that will get called with `(code, message, data)` for
631634
any warnings encountered. (See "Warnings and Errors")
635+
- `maxDecompressionRatio` Defaults to 1000. The maximum ratio of
636+
decommpressed bytes to compressed bytes, in a compressed
637+
archive. Set to `Infinity` to allow explosive decompression.
632638

633639
### tar.u(options, fileList, callback) [alias: tar.update]
634640

@@ -904,6 +910,9 @@ Most unpack errors will cause a `warn` event to be emitted. If the
904910
defaults to 1024. Anything deeper than the limit will raise a
905911
warning and skip the entry. Set to `Infinity` to remove the
906912
limitation.
913+
- `maxDecompressionRatio` Defaults to 1000. The maximum ratio of
914+
decommpressed bytes to compressed bytes, in a compressed
915+
archive. Set to `Infinity` to allow explosive decompression.
907916

908917
### class UnpackSync
909918

‎src/options.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ export interface TarOptions {
153153
*/
154154
onwarn?: (code: string, message: string, data: WarnData) => unknown
155155

156+
/**
157+
* The maximum allowed ratio of decompressed bytes to compressed bytes when
158+
* reading gzip, brotli, or zstd-compressed archives.
159+
*
160+
* This defaults to `1000` and aborts extraction or parsing when exceeded.
161+
* Set to `Infinity` to disable the limit.
162+
*/
163+
maxDecompressionRatio?: number
164+
156165
//////////////////////////
157166
// extraction options
158167

‎src/parse.ts‎

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ const SAW_VALID_ENTRY = Symbol('sawValidEntry')
6161
const SAW_NULL_BLOCK = Symbol('sawNullBlock')
6262
const SAW_EOF = Symbol('sawEOF')
6363
const 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

6569
const 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 {

‎test/extract.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { Test } from 'tap'
99
import t from 'tap'
1010
import { fileURLToPath } from 'url'
1111
import { promisify } from 'util'
12+
import zlib from 'zlib'
1213
import { extract as x } from '../dist/esm/extract.js'
1314
import { Unpack, UnpackSync } from '../dist/esm/unpack.js'
1415
import { makeTar } from './fixtures/make-tar.js'
@@ -20,6 +21,22 @@ const __dirname = path.dirname(__filename)
2021
const extractdir = path.resolve(__dirname, 'fixtures/extract')
2122
const tars = path.resolve(__dirname, 'fixtures/tars')
2223

24+
const makeCompressedBomb = () => {
25+
const payload = Buffer.alloc(8 * 1024 * 1024)
26+
return zlib.gzipSync(
27+
makeTar([
28+
{
29+
path: 'bomb',
30+
size: payload.length,
31+
type: 'File',
32+
},
33+
payload,
34+
'',
35+
'',
36+
]),
37+
)
38+
}
39+
2340
const tnock = (t: Test, host: string, opts?: nock.Options) => {
2441
nock.disableNetConnect()
2542
const server = nock(host, opts)
@@ -408,6 +425,45 @@ t.test('sync gzip error edge case test', async t => {
408425
t.end()
409426
})
410427

428+
t.test('max decompression ratio', async t => {
429+
const file = path.resolve(extractdir, 'bomb.tgz')
430+
const bomb = makeCompressedBomb()
431+
432+
await mkdirp(extractdir)
433+
fs.writeFileSync(file, bomb)
434+
t.teardown(() => rimraf(file))
435+
436+
t.test('file extraction aborts by default', async t => {
437+
const cwd = t.testdir({})
438+
t.throws(
439+
() => x({ sync: true, file, cwd }),
440+
{
441+
tarCode: 'TAR_ABORT',
442+
recoverable: false,
443+
message: /max decompression ratio exceeded/,
444+
},
445+
'sync throws',
446+
)
447+
await t.rejects(x({ file, cwd }), {
448+
tarCode: 'TAR_ABORT',
449+
recoverable: false,
450+
message: /max decompression ratio exceeded/,
451+
})
452+
})
453+
454+
t.test('file extraction can disable the limit explicitly', async t => {
455+
const cwd = t.testdir({})
456+
await x({
457+
file,
458+
cwd,
459+
maxDecompressionRatio: Infinity,
460+
})
461+
t.equal(fs.statSync(path.resolve(cwd, 'bomb')).size, 8 * 1024 * 1024)
462+
})
463+
464+
t.end()
465+
})
466+
411467
t.test('brotli', async t => {
412468
const file = path.resolve(__dirname, 'fixtures/example.tbr')
413469
const dir = path.resolve(__dirname, 'brotli')

‎test/parse.js‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,67 @@ t.test('tarmageddon', t => {
10231023

10241024
t.end()
10251025
})
1026+
1027+
t.test('max decompression ratio', async t => {
1028+
const makeCompressedBomb = () => {
1029+
const payload = Buffer.alloc(8 * 1024 * 1024)
1030+
return zlib.gzipSync(
1031+
makeTar([
1032+
{
1033+
path: 'bomb',
1034+
size: payload.length,
1035+
type: 'File',
1036+
},
1037+
payload,
1038+
'',
1039+
'',
1040+
]),
1041+
)
1042+
}
1043+
const bomb = makeCompressedBomb()
1044+
1045+
t.test('parsing aborts by default', async t => {
1046+
const p = new Parser({})
1047+
t.throws(
1048+
() => p.end(bomb),
1049+
{
1050+
tarCode: 'TAR_ABORT',
1051+
recoverable: false,
1052+
message: /max decompression ratio exceeded/,
1053+
},
1054+
'parsing aborts',
1055+
)
1056+
// no-op to abort again
1057+
p.abort(new Error('hello'))
1058+
})
1059+
1060+
t.test('aborting only happens one time', t => {
1061+
const p = new Parser({})
1062+
t.throws(() => p.abort(new Error('hello')), {
1063+
message: 'hello',
1064+
tarCode: 'TAR_ABORT',
1065+
recoverable: false,
1066+
})
1067+
// this is ignored
1068+
p.end(bomb)
1069+
t.end()
1070+
})
1071+
1072+
t.test('parsing can disable the limit explicitly', async () => {
1073+
let bytesRead = 0
1074+
await new Promise(res =>
1075+
new Parser({
1076+
maxDecompressionRatio: Infinity,
1077+
onReadEntry(e) {
1078+
e.on('data', c => (bytesRead += c.byteLength))
1079+
},
1080+
})
1081+
.on('end', res)
1082+
.end(bomb)
1083+
.resume(),
1084+
)
1085+
t.equal(bytesRead, 8 * 1024 * 1024)
1086+
})
1087+
1088+
t.end()
1089+
})

0 commit comments

Comments
 (0)