tar 5.0.10 → 6.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/unpack.js CHANGED
@@ -16,14 +16,10 @@ const mkdir = require('./mkdir.js')
16
16
  const mkdirSync = mkdir.sync
17
17
  const wc = require('./winchars.js')
18
18
  const pathReservations = require('./path-reservations.js')
19
- const stripAbsolutePath = require('./strip-absolute-path.js')
20
- const normPath = require('./normalize-windows-path.js')
21
- const stripSlash = require('./strip-trailing-slashes.js')
22
19
 
23
20
  const ONENTRY = Symbol('onEntry')
24
21
  const CHECKFS = Symbol('checkFs')
25
22
  const CHECKFS2 = Symbol('checkFs2')
26
- const PRUNECACHE = Symbol('pruneCache')
27
23
  const ISREUSABLE = Symbol('isReusable')
28
24
  const MAKEFS = Symbol('makeFs')
29
25
  const FILE = Symbol('file')
@@ -45,11 +41,13 @@ const SKIP = Symbol('skip')
45
41
  const DOCHOWN = Symbol('doChown')
46
42
  const UID = Symbol('uid')
47
43
  const GID = Symbol('gid')
48
- const CHECKED_CWD = Symbol('checkedCwd')
49
44
  const crypto = require('crypto')
50
45
  const getFlag = require('./get-write-flag.js')
51
- const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
52
- const isWindows = platform === 'win32'
46
+
47
+ /* istanbul ignore next */
48
+ const neverCalled = () => {
49
+ throw new Error('sync function called cb somehow?!?')
50
+ }
53
51
 
54
52
  // Unlinks on Windows are not atomic.
55
53
  //
@@ -68,7 +66,7 @@ const isWindows = platform === 'win32'
68
66
  // See: https://github.com/npm/node-tar/issues/183
69
67
  /* istanbul ignore next */
70
68
  const unlinkFile = (path, cb) => {
71
- if (!isWindows)
69
+ if (process.platform !== 'win32')
72
70
  return fs.unlink(path, cb)
73
71
 
74
72
  const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
@@ -81,7 +79,7 @@ const unlinkFile = (path, cb) => {
81
79
 
82
80
  /* istanbul ignore next */
83
81
  const unlinkFileSync = path => {
84
- if (!isWindows)
82
+ if (process.platform !== 'win32')
85
83
  return fs.unlinkSync(path)
86
84
 
87
85
  const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
@@ -95,33 +93,6 @@ const uint32 = (a, b, c) =>
95
93
  : b === b >>> 0 ? b
96
94
  : c
97
95
 
98
- // clear the cache if it's a case-insensitive unicode-squashing match.
99
- // we can't know if the current file system is case-sensitive or supports
100
- // unicode fully, so we check for similarity on the maximally compatible
101
- // representation. Err on the side of pruning, since all it's doing is
102
- // preventing lstats, and it's not the end of the world if we get a false
103
- // positive.
104
- // Note that on windows, we always drop the entire cache whenever a
105
- // symbolic link is encountered, because 8.3 filenames are impossible
106
- // to reason about, and collisions are hazards rather than just failures.
107
- const cacheKeyNormalize = path => stripSlash(normPath(path))
108
- .normalize('NFKD')
109
- .toLowerCase()
110
-
111
- const pruneCache = (cache, abs) => {
112
- abs = cacheKeyNormalize(abs)
113
- for (const path of cache.keys()) {
114
- const pnorm = cacheKeyNormalize(path)
115
- if (pnorm === abs || pnorm.indexOf(abs + '/') === 0)
116
- cache.delete(path)
117
- }
118
- }
119
-
120
- const dropCache = cache => {
121
- for (const key of cache.keys())
122
- cache.delete(key)
123
- }
124
-
125
96
  class Unpack extends Parser {
126
97
  constructor (opt) {
127
98
  if (!opt)
@@ -134,8 +105,6 @@ class Unpack extends Parser {
134
105
 
135
106
  super(opt)
136
107
 
137
- this[CHECKED_CWD] = false
138
-
139
108
  this.reservations = pathReservations()
140
109
 
141
110
  this.transform = typeof opt.transform === 'function' ? opt.transform : null
@@ -180,7 +149,7 @@ class Unpack extends Parser {
180
149
  this.forceChown = opt.forceChown === true
181
150
 
182
151
  // turn ><?| in filenames into 0xf000-higher encoded forms
183
- this.win32 = !!opt.win32 || isWindows
152
+ this.win32 = !!opt.win32 || process.platform === 'win32'
184
153
 
185
154
  // do not unpack over files that are newer than what's in the archive
186
155
  this.newer = !!opt.newer
@@ -200,7 +169,7 @@ class Unpack extends Parser {
200
169
  // links, and removes symlink directories rather than erroring
201
170
  this.unlink = !!opt.unlink
202
171
 
203
- this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
172
+ this.cwd = path.resolve(opt.cwd || process.cwd())
204
173
  this.strip = +opt.strip || 0
205
174
  this.processUmask = process.umask()
206
175
  this.umask = typeof opt.umask === 'number' ? opt.umask : this.processUmask
@@ -230,24 +199,21 @@ class Unpack extends Parser {
230
199
 
231
200
  [CHECKPATH] (entry) {
232
201
  if (this.strip) {
233
- const parts = normPath(entry.path).split('/')
202
+ const parts = entry.path.split(/\/|\\/)
234
203
  if (parts.length < this.strip)
235
204
  return false
236
205
  entry.path = parts.slice(this.strip).join('/')
237
206
 
238
207
  if (entry.type === 'Link') {
239
- const linkparts = normPath(entry.linkpath).split('/')
208
+ const linkparts = entry.linkpath.split(/\/|\\/)
240
209
  if (linkparts.length >= this.strip)
241
210
  entry.linkpath = linkparts.slice(this.strip).join('/')
242
- else
243
- return false
244
211
  }
245
212
  }
246
213
 
247
214
  if (!this.preservePaths) {
248
- const p = normPath(entry.path)
249
- const parts = p.split('/')
250
- if (parts.includes('..') || isWindows && /^[a-z]:\.\.$/i.test(parts[0])) {
215
+ const p = entry.path
216
+ if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
251
217
  this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
252
218
  entry,
253
219
  path: p,
@@ -255,53 +221,31 @@ class Unpack extends Parser {
255
221
  return false
256
222
  }
257
223
 
258
- // strip off the root
259
- const [root, stripped] = stripAbsolutePath(p)
260
- if (root) {
261
- entry.path = stripped
262
- this.warn('TAR_ENTRY_INFO', `stripping ${root} from absolute path`, {
224
+ // absolutes on posix are also absolutes on win32
225
+ // so we only need to test this one to get both
226
+ if (path.win32.isAbsolute(p)) {
227
+ const parsed = path.win32.parse(p)
228
+ entry.path = p.substr(parsed.root.length)
229
+ const r = parsed.root
230
+ this.warn('TAR_ENTRY_INFO', `stripping ${r} from absolute path`, {
263
231
  entry,
264
232
  path: p,
265
233
  })
266
234
  }
267
235
  }
268
236
 
269
- if (path.isAbsolute(entry.path))
270
- entry.absolute = normPath(path.resolve(entry.path))
271
- else
272
- entry.absolute = normPath(path.resolve(this.cwd, entry.path))
273
-
274
- // if we somehow ended up with a path that escapes the cwd, and we are
275
- // not in preservePaths mode, then something is fishy! This should have
276
- // been prevented above, so ignore this for coverage.
277
- /* istanbul ignore if - defense in depth */
278
- if (!this.preservePaths &&
279
- entry.absolute.indexOf(this.cwd + '/') !== 0 &&
280
- entry.absolute !== this.cwd) {
281
- this.warn('TAR_ENTRY_ERROR', 'path escaped extraction target', {
282
- entry,
283
- path: normPath(entry.path),
284
- resolvedPath: entry.absolute,
285
- cwd: this.cwd,
286
- })
287
- return false
288
- }
289
-
290
- // an archive can set properties on the extraction directory, but it
291
- // may not replace the cwd with a different kind of thing entirely.
292
- if (entry.absolute === this.cwd &&
293
- entry.type !== 'Directory' &&
294
- entry.type !== 'GNUDumpDir')
295
- return false
296
-
297
237
  // only encode : chars that aren't drive letter indicators
298
238
  if (this.win32) {
299
- const { root: aRoot } = path.win32.parse(entry.absolute)
300
- entry.absolute = aRoot + wc.encode(entry.absolute.substr(aRoot.length))
301
- const { root: pRoot } = path.win32.parse(entry.path)
302
- entry.path = pRoot + wc.encode(entry.path.substr(pRoot.length))
239
+ const parsed = path.win32.parse(entry.path)
240
+ entry.path = parsed.root === '' ? wc.encode(entry.path)
241
+ : parsed.root + wc.encode(entry.path.substr(parsed.root.length))
303
242
  }
304
243
 
244
+ if (path.isAbsolute(entry.path))
245
+ entry.absolute = entry.path
246
+ else
247
+ entry.absolute = path.resolve(this.cwd, entry.path)
248
+
305
249
  return true
306
250
  }
307
251
 
@@ -345,7 +289,7 @@ class Unpack extends Parser {
345
289
  }
346
290
 
347
291
  [MKDIR] (dir, mode, cb) {
348
- mkdir(normPath(dir), {
292
+ mkdir(dir, {
349
293
  uid: this.uid,
350
294
  gid: this.gid,
351
295
  processUid: this.processUid,
@@ -386,37 +330,17 @@ class Unpack extends Parser {
386
330
  mode: mode,
387
331
  autoClose: false
388
332
  })
389
- stream.on('error', er => {
390
- if (stream.fd)
391
- fs.close(stream.fd, () => {})
392
-
393
- // flush all the data out so that we aren't left hanging
394
- // if the error wasn't actually fatal. otherwise the parse
395
- // is blocked, and we never proceed.
396
- stream.write = () => true
397
- this[ONERROR](er, entry)
398
- fullyDone()
399
- })
333
+ stream.on('error', er => this[ONERROR](er, entry))
400
334
 
401
335
  let actions = 1
402
336
  const done = er => {
403
- if (er) {
404
- /* istanbul ignore else - we should always have a fd by now */
405
- if (stream.fd)
406
- fs.close(stream.fd, () => {})
407
-
408
- this[ONERROR](er, entry)
409
- fullyDone()
410
- return
411
- }
337
+ if (er)
338
+ return this[ONERROR](er, entry)
412
339
 
413
340
  if (--actions === 0) {
414
341
  fs.close(stream.fd, er => {
415
- if (er)
416
- this[ONERROR](er, entry)
417
- else
418
- this[UNPEND]()
419
342
  fullyDone()
343
+ er ? this[ONERROR](er, entry) : this[UNPEND]()
420
344
  })
421
345
  }
422
346
  }
@@ -451,10 +375,7 @@ class Unpack extends Parser {
451
375
 
452
376
  const tx = this.transform ? this.transform(entry) || entry : entry
453
377
  if (tx !== entry) {
454
- tx.on('error', er => {
455
- this[ONERROR](er, entry)
456
- fullyDone()
457
- })
378
+ tx.on('error', er => this[ONERROR](er, entry))
458
379
  entry.pipe(tx)
459
380
  }
460
381
  tx.pipe(stream)
@@ -464,9 +385,8 @@ class Unpack extends Parser {
464
385
  const mode = entry.mode & 0o7777 || this.dmode
465
386
  this[MKDIR](entry.absolute, mode, er => {
466
387
  if (er) {
467
- this[ONERROR](er, entry)
468
388
  fullyDone()
469
- return
389
+ return this[ONERROR](er, entry)
470
390
  }
471
391
 
472
392
  let actions = 1
@@ -504,8 +424,7 @@ class Unpack extends Parser {
504
424
  }
505
425
 
506
426
  [HARDLINK] (entry, done) {
507
- const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
508
- this[LINK](entry, linkpath, 'link', done)
427
+ this[LINK](entry, path.resolve(this.cwd, entry.linkpath), 'link', done)
509
428
  }
510
429
 
511
430
  [PEND] () {
@@ -530,7 +449,7 @@ class Unpack extends Parser {
530
449
  !this.unlink &&
531
450
  st.isFile() &&
532
451
  st.nlink <= 1 &&
533
- !isWindows
452
+ process.platform !== 'win32'
534
453
  }
535
454
 
536
455
  // check if a thing is there, and if so, try to clobber it
@@ -541,116 +460,37 @@ class Unpack extends Parser {
541
460
  paths.push(entry.linkpath)
542
461
  this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
543
462
  }
544
-
545
- [PRUNECACHE] (entry) {
546
- // if we are not creating a directory, and the path is in the dirCache,
547
- // then that means we are about to delete the directory we created
548
- // previously, and it is no longer going to be a directory, and neither
549
- // is any of its children.
550
- // If a symbolic link is encountered on Windows, all bets are off.
551
- // There is no reasonable way to sanitize the cache in such a way
552
- // we will be able to avoid having filesystem collisions. If this
553
- // happens with a non-symlink entry, it'll just fail to unpack,
554
- // but a symlink to a directory, using an 8.3 shortname, can evade
555
- // detection and lead to arbitrary writes to anywhere on the system.
556
- if (isWindows && entry.type === 'SymbolicLink')
557
- dropCache(this.dirCache)
558
- else if (entry.type !== 'Directory')
559
- pruneCache(this.dirCache, entry.absolute)
560
- }
561
-
562
- [CHECKFS2] (entry, fullyDone) {
563
- this[PRUNECACHE](entry)
564
-
565
- const done = er => {
566
- this[PRUNECACHE](entry)
567
- fullyDone(er)
568
- }
569
-
570
- const checkCwd = () => {
571
- this[MKDIR](this.cwd, this.dmode, er => {
572
- if (er) {
573
- this[ONERROR](er, entry)
574
- done()
575
- return
576
- }
577
- this[CHECKED_CWD] = true
578
- start()
579
- })
580
- }
581
-
582
- const start = () => {
583
- if (entry.absolute !== this.cwd) {
584
- const parent = normPath(path.dirname(entry.absolute))
585
- if (parent !== this.cwd) {
586
- return this[MKDIR](parent, this.dmode, er => {
587
- if (er) {
588
- this[ONERROR](er, entry)
589
- done()
590
- return
591
- }
592
- afterMakeParent()
593
- })
594
- }
463
+ [CHECKFS2] (entry, done) {
464
+ this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
465
+ if (er) {
466
+ done()
467
+ return this[ONERROR](er, entry)
595
468
  }
596
- afterMakeParent()
597
- }
598
-
599
- const afterMakeParent = () => {
600
- fs.lstat(entry.absolute, (lstatEr, st) => {
469
+ fs.lstat(entry.absolute, (er, st) => {
601
470
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
602
471
  this[SKIP](entry)
603
472
  done()
604
- return
473
+ } else if (er || this[ISREUSABLE](entry, st)) {
474
+ this[MAKEFS](null, entry, done)
605
475
  }
606
- if (lstatEr || this[ISREUSABLE](entry, st))
607
- return this[MAKEFS](null, entry, done)
608
-
609
- if (st.isDirectory()) {
476
+ else if (st.isDirectory()) {
610
477
  if (entry.type === 'Directory') {
611
- const needChmod = !this.noChmod &&
612
- entry.mode &&
613
- (st.mode & 0o7777) !== entry.mode
614
- const afterChmod = er => this[MAKEFS](er, entry, done)
615
- if (!needChmod)
616
- return afterChmod()
617
- return fs.chmod(entry.absolute, entry.mode, afterChmod)
618
- }
619
- // Not a dir entry, have to remove it.
620
- // NB: the only way to end up with an entry that is the cwd
621
- // itself, in such a way that == does not detect, is a
622
- // tricky windows absolute path with UNC or 8.3 parts (and
623
- // preservePaths:true, or else it will have been stripped).
624
- // In that case, the user has opted out of path protections
625
- // explicitly, so if they blow away the cwd, c'est la vie.
626
- if (entry.absolute !== this.cwd) {
627
- return fs.rmdir(entry.absolute, er =>
628
- this[MAKEFS](er, entry, done))
629
- }
630
- }
631
-
632
- // not a dir, and not reusable
633
- // don't remove if the cwd, we want that error
634
- if (entry.absolute === this.cwd)
635
- return this[MAKEFS](null, entry, done)
636
-
637
- unlinkFile(entry.absolute, er =>
638
- this[MAKEFS](er, entry, done))
478
+ if (!entry.mode || (st.mode & 0o7777) === entry.mode)
479
+ this[MAKEFS](null, entry, done)
480
+ else
481
+ fs.chmod(entry.absolute, entry.mode,
482
+ er => this[MAKEFS](er, entry, done))
483
+ } else
484
+ fs.rmdir(entry.absolute, er => this[MAKEFS](er, entry, done))
485
+ } else
486
+ unlinkFile(entry.absolute, er => this[MAKEFS](er, entry, done))
639
487
  })
640
- }
641
-
642
- if (this[CHECKED_CWD])
643
- start()
644
- else
645
- checkCwd()
488
+ })
646
489
  }
647
490
 
648
491
  [MAKEFS] (er, entry, done) {
649
- if (er) {
650
- this[ONERROR](er, entry)
651
- done()
652
- return
653
- }
492
+ if (er)
493
+ return this[ONERROR](er, entry)
654
494
 
655
495
  switch (entry.type) {
656
496
  case 'File':
@@ -671,82 +511,53 @@ class Unpack extends Parser {
671
511
  }
672
512
 
673
513
  [LINK] (entry, linkpath, link, done) {
674
- // XXX: get the type ('symlink' or 'junction') for windows
514
+ // XXX: get the type ('file' or 'dir') for windows
675
515
  fs[link](linkpath, entry.absolute, er => {
676
516
  if (er)
677
- this[ONERROR](er, entry)
678
- else {
679
- this[UNPEND]()
680
- entry.resume()
681
- }
517
+ return this[ONERROR](er, entry)
682
518
  done()
519
+ this[UNPEND]()
520
+ entry.resume()
683
521
  })
684
522
  }
685
523
  }
686
524
 
687
- const callSync = fn => {
688
- try {
689
- return [null, fn()]
690
- } catch (er) {
691
- return [er, null]
692
- }
693
- }
694
525
  class UnpackSync extends Unpack {
695
- [MAKEFS] (er, entry) {
696
- return super[MAKEFS](er, entry, () => {})
526
+ constructor (opt) {
527
+ super(opt)
697
528
  }
698
529
 
699
530
  [CHECKFS] (entry) {
700
- this[PRUNECACHE](entry)
701
-
702
- if (!this[CHECKED_CWD]) {
703
- const er = this[MKDIR](this.cwd, this.dmode)
704
- if (er)
705
- return this[ONERROR](er, entry)
706
- this[CHECKED_CWD] = true
707
- }
708
-
709
- // don't bother to make the parent if the current entry is the cwd,
710
- // we've already checked it.
711
- if (entry.absolute !== this.cwd) {
712
- const parent = normPath(path.dirname(entry.absolute))
713
- if (parent !== this.cwd) {
714
- const mkParent = this[MKDIR](parent, this.dmode)
715
- if (mkParent)
716
- return this[ONERROR](mkParent, entry)
717
- }
718
- }
719
-
720
- const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
721
- if (st && (this.keep || this.newer && st.mtime > entry.mtime))
722
- return this[SKIP](entry)
723
-
724
- if (lstatEr || this[ISREUSABLE](entry, st))
725
- return this[MAKEFS](null, entry)
726
-
727
- if (st.isDirectory()) {
728
- if (entry.type === 'Directory') {
729
- const needChmod = !this.noChmod &&
730
- entry.mode &&
731
- (st.mode & 0o7777) !== entry.mode
732
- const [er] = needChmod ? callSync(() => {
733
- fs.chmodSync(entry.absolute, entry.mode)
734
- }) : []
735
- return this[MAKEFS](er, entry)
531
+ const er = this[MKDIR](path.dirname(entry.absolute), this.dmode, neverCalled)
532
+ if (er)
533
+ return this[ONERROR](er, entry)
534
+ try {
535
+ const st = fs.lstatSync(entry.absolute)
536
+ if (this.keep || this.newer && st.mtime > entry.mtime)
537
+ return this[SKIP](entry)
538
+ else if (this[ISREUSABLE](entry, st))
539
+ return this[MAKEFS](null, entry, neverCalled)
540
+ else {
541
+ try {
542
+ if (st.isDirectory()) {
543
+ if (entry.type === 'Directory') {
544
+ if (entry.mode && (st.mode & 0o7777) !== entry.mode)
545
+ fs.chmodSync(entry.absolute, entry.mode)
546
+ } else
547
+ fs.rmdirSync(entry.absolute)
548
+ } else
549
+ unlinkFileSync(entry.absolute)
550
+ return this[MAKEFS](null, entry, neverCalled)
551
+ } catch (er) {
552
+ return this[ONERROR](er, entry)
553
+ }
736
554
  }
737
- // not a dir entry, have to remove it
738
- const [er] = callSync(() => fs.rmdirSync(entry.absolute))
739
- this[MAKEFS](er, entry)
555
+ } catch (er) {
556
+ return this[MAKEFS](null, entry, neverCalled)
740
557
  }
741
-
742
- // not a dir, and not reusable.
743
- // don't remove if it's the cwd, since we want that error.
744
- const [er] = entry.absolute === this.cwd ? []
745
- : callSync(() => unlinkFileSync(entry.absolute))
746
- this[MAKEFS](er, entry)
747
558
  }
748
559
 
749
- [FILE] (entry, done) {
560
+ [FILE] (entry, _) {
750
561
  const mode = entry.mode & 0o7777 || this.fmode
751
562
 
752
563
  const oner = er => {
@@ -758,7 +569,6 @@ class UnpackSync extends Unpack {
758
569
  }
759
570
  if (er || closeError)
760
571
  this[ONERROR](er || closeError, entry)
761
- done()
762
572
  }
763
573
 
764
574
  let stream
@@ -819,14 +629,11 @@ class UnpackSync extends Unpack {
819
629
  })
820
630
  }
821
631
 
822
- [DIRECTORY] (entry, done) {
632
+ [DIRECTORY] (entry, _) {
823
633
  const mode = entry.mode & 0o7777 || this.dmode
824
634
  const er = this[MKDIR](entry.absolute, mode)
825
- if (er) {
826
- this[ONERROR](er, entry)
827
- done()
828
- return
829
- }
635
+ if (er)
636
+ return this[ONERROR](er, entry)
830
637
  if (entry.mtime && !this.noMtime) {
831
638
  try {
832
639
  fs.utimesSync(entry.absolute, entry.atime || new Date(), entry.mtime)
@@ -837,13 +644,12 @@ class UnpackSync extends Unpack {
837
644
  fs.chownSync(entry.absolute, this[UID](entry), this[GID](entry))
838
645
  } catch (er) {}
839
646
  }
840
- done()
841
647
  entry.resume()
842
648
  }
843
649
 
844
650
  [MKDIR] (dir, mode) {
845
651
  try {
846
- return mkdir.sync(normPath(dir), {
652
+ return mkdir.sync(dir, {
847
653
  uid: this.uid,
848
654
  gid: this.gid,
849
655
  processUid: this.processUid,
@@ -860,10 +666,9 @@ class UnpackSync extends Unpack {
860
666
  }
861
667
  }
862
668
 
863
- [LINK] (entry, linkpath, link, done) {
669
+ [LINK] (entry, linkpath, link, _) {
864
670
  try {
865
671
  fs[link + 'Sync'](linkpath, entry.absolute)
866
- done()
867
672
  entry.resume()
868
673
  } catch (er) {
869
674
  return this[ONERROR](er, entry)