tar 6.1.8 → 6.1.9

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.
@@ -8,8 +8,12 @@
8
8
 
9
9
  const assert = require('assert')
10
10
  const normPath = require('./normalize-windows-path.js')
11
+ const stripSlashes = require('./strip-trailing-slashes.js')
11
12
  const { join } = require('path')
12
13
 
14
+ const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
15
+ const isWindows = platform === 'win32'
16
+
13
17
  module.exports = () => {
14
18
  // path => [function or Set]
15
19
  // A Set object means a directory reservation
@@ -20,10 +24,16 @@ module.exports = () => {
20
24
  const reservations = new Map()
21
25
 
22
26
  // return a set of parent dirs for a given path
23
- const getDirs = path =>
24
- path.split('/').slice(0, -1).reduce((set, path) =>
25
- set.length ? set.concat(normPath(join(set[set.length - 1], path)))
26
- : [path], [])
27
+ // '/a/b/c/d' -> ['/', '/a', '/a/b', '/a/b/c', '/a/b/c/d']
28
+ const getDirs = path => {
29
+ const dirs = path.split('/').slice(0, -1).reduce((set, path) => {
30
+ if (set.length)
31
+ path = normPath(join(set[set.length - 1], path))
32
+ set.push(path || '/')
33
+ return set
34
+ }, [])
35
+ return dirs
36
+ }
27
37
 
28
38
  // functions currently running
29
39
  const running = new Set()
@@ -99,7 +109,18 @@ module.exports = () => {
99
109
  }
100
110
 
101
111
  const reserve = (paths, fn) => {
102
- paths = paths.map(p => normPath(join(p)).toLowerCase())
112
+ // collide on matches across case and unicode normalization
113
+ // On windows, thanks to the magic of 8.3 shortnames, it is fundamentally
114
+ // impossible to determine whether two paths refer to the same thing on
115
+ // disk, without asking the kernel for a shortname.
116
+ // So, we just pretend that every path matches every other path here,
117
+ // effectively removing all parallelization on windows.
118
+ paths = isWindows ? ['win32 parallelization disabled'] : paths.map(p => {
119
+ return stripSlashes(normPath(join(p)))
120
+ .normalize('NFKD')
121
+ .toLowerCase()
122
+ })
123
+
103
124
  const dirs = new Set(
104
125
  paths.map(path => getDirs(path)).reduce((a, b) => a.concat(b))
105
126
  )
@@ -2,13 +2,23 @@
2
2
  const { isAbsolute, parse } = require('path').win32
3
3
 
4
4
  // returns [root, stripped]
5
+ // Note that windows will think that //x/y/z/a has a "root" of //x/y, and in
6
+ // those cases, we want to sanitize it to x/y/z/a, not z/a, so we strip /
7
+ // explicitly if it's the first character.
8
+ // drive-specific relative paths on Windows get their root stripped off even
9
+ // though they are not absolute, so `c:../foo` becomes ['c:', '../foo']
5
10
  module.exports = path => {
6
11
  let r = ''
7
- while (isAbsolute(path)) {
12
+
13
+ let parsed = parse(path)
14
+ while (isAbsolute(path) || parsed.root) {
8
15
  // windows will think that //x/y/z has a "root" of //x/y/
9
- const root = path.charAt(0) === '/' ? '/' : parse(path).root
16
+ // but strip the //?/C:/ off of //?/C:/path
17
+ const root = path.charAt(0) === '/' && path.slice(0, 4) !== '//?/' ? '/'
18
+ : parsed.root
10
19
  path = path.substr(root.length)
11
20
  r += root
21
+ parsed = parse(path)
12
22
  }
13
23
  return [r, path]
14
24
  }
package/lib/unpack.js CHANGED
@@ -16,10 +16,12 @@ const wc = require('./winchars.js')
16
16
  const pathReservations = require('./path-reservations.js')
17
17
  const stripAbsolutePath = require('./strip-absolute-path.js')
18
18
  const normPath = require('./normalize-windows-path.js')
19
+ const stripSlash = require('./strip-trailing-slashes.js')
19
20
 
20
21
  const ONENTRY = Symbol('onEntry')
21
22
  const CHECKFS = Symbol('checkFs')
22
23
  const CHECKFS2 = Symbol('checkFs2')
24
+ const PRUNECACHE = Symbol('pruneCache')
23
25
  const ISREUSABLE = Symbol('isReusable')
24
26
  const MAKEFS = Symbol('makeFs')
25
27
  const FILE = Symbol('file')
@@ -43,6 +45,8 @@ const GID = Symbol('gid')
43
45
  const CHECKED_CWD = Symbol('checkedCwd')
44
46
  const crypto = require('crypto')
45
47
  const getFlag = require('./get-write-flag.js')
48
+ const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
49
+ const isWindows = platform === 'win32'
46
50
 
47
51
  // Unlinks on Windows are not atomic.
48
52
  //
@@ -61,7 +65,7 @@ const getFlag = require('./get-write-flag.js')
61
65
  // See: https://github.com/npm/node-tar/issues/183
62
66
  /* istanbul ignore next */
63
67
  const unlinkFile = (path, cb) => {
64
- if (process.platform !== 'win32')
68
+ if (!isWindows)
65
69
  return fs.unlink(path, cb)
66
70
 
67
71
  const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
@@ -74,7 +78,7 @@ const unlinkFile = (path, cb) => {
74
78
 
75
79
  /* istanbul ignore next */
76
80
  const unlinkFileSync = path => {
77
- if (process.platform !== 'win32')
81
+ if (!isWindows)
78
82
  return fs.unlinkSync(path)
79
83
 
80
84
  const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
@@ -88,17 +92,33 @@ const uint32 = (a, b, c) =>
88
92
  : b === b >>> 0 ? b
89
93
  : c
90
94
 
95
+ // clear the cache if it's a case-insensitive unicode-squashing match.
96
+ // we can't know if the current file system is case-sensitive or supports
97
+ // unicode fully, so we check for similarity on the maximally compatible
98
+ // representation. Err on the side of pruning, since all it's doing is
99
+ // preventing lstats, and it's not the end of the world if we get a false
100
+ // positive.
101
+ // Note that on windows, we always drop the entire cache whenever a
102
+ // symbolic link is encountered, because 8.3 filenames are impossible
103
+ // to reason about, and collisions are hazards rather than just failures.
104
+ const cacheKeyNormalize = path => stripSlash(normPath(path))
105
+ .normalize('NFKD')
106
+ .toLowerCase()
107
+
91
108
  const pruneCache = (cache, abs) => {
92
- // clear the cache if it's a case-insensitive match, since we can't
93
- // know if the current file system is case-sensitive or not.
94
- abs = normPath(abs).toLowerCase()
109
+ abs = cacheKeyNormalize(abs)
95
110
  for (const path of cache.keys()) {
96
- const plower = path.toLowerCase()
97
- if (plower === abs || plower.toLowerCase().indexOf(abs + '/') === 0)
111
+ const pnorm = cacheKeyNormalize(path)
112
+ if (pnorm === abs || pnorm.indexOf(abs + '/') === 0)
98
113
  cache.delete(path)
99
114
  }
100
115
  }
101
116
 
117
+ const dropCache = cache => {
118
+ for (const key of cache.keys())
119
+ cache.delete(key)
120
+ }
121
+
102
122
  class Unpack extends Parser {
103
123
  constructor (opt) {
104
124
  if (!opt)
@@ -158,7 +178,7 @@ class Unpack extends Parser {
158
178
  this.forceChown = opt.forceChown === true
159
179
 
160
180
  // turn ><?| in filenames into 0xf000-higher encoded forms
161
- this.win32 = !!opt.win32 || process.platform === 'win32'
181
+ this.win32 = !!opt.win32 || isWindows
162
182
 
163
183
  // do not unpack over files that are newer than what's in the archive
164
184
  this.newer = !!opt.newer
@@ -227,7 +247,8 @@ class Unpack extends Parser {
227
247
 
228
248
  if (!this.preservePaths) {
229
249
  const p = normPath(entry.path)
230
- if (p.split('/').includes('..')) {
250
+ const parts = p.split('/')
251
+ if (parts.includes('..') || isWindows && /^[a-z]:\.\.$/i.test(parts[0])) {
231
252
  this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
232
253
  entry,
233
254
  path: p,
@@ -235,8 +256,7 @@ class Unpack extends Parser {
235
256
  return false
236
257
  }
237
258
 
238
- // absolutes on posix are also absolutes on win32
239
- // so we only need to test this one to get both
259
+ // strip off the root
240
260
  const [root, stripped] = stripAbsolutePath(p)
241
261
  if (root) {
242
262
  entry.path = stripped
@@ -252,6 +272,22 @@ class Unpack extends Parser {
252
272
  else
253
273
  entry.absolute = normPath(path.resolve(this.cwd, entry.path))
254
274
 
275
+ // if we somehow ended up with a path that escapes the cwd, and we are
276
+ // not in preservePaths mode, then something is fishy! This should have
277
+ // been prevented above, so ignore this for coverage.
278
+ /* istanbul ignore if - defense in depth */
279
+ if (!this.preservePaths &&
280
+ entry.absolute.indexOf(this.cwd + '/') !== 0 &&
281
+ entry.absolute !== this.cwd) {
282
+ this.warn('TAR_ENTRY_ERROR', 'path escaped extraction target', {
283
+ entry,
284
+ path: normPath(entry.path),
285
+ resolvedPath: entry.absolute,
286
+ cwd: this.cwd,
287
+ })
288
+ return false
289
+ }
290
+
255
291
  // an archive can set properties on the extraction directory, but it
256
292
  // may not replace the cwd with a different kind of thing entirely.
257
293
  if (entry.absolute === this.cwd &&
@@ -497,7 +533,7 @@ class Unpack extends Parser {
497
533
  !this.unlink &&
498
534
  st.isFile() &&
499
535
  st.nlink <= 1 &&
500
- process.platform !== 'win32'
536
+ !isWindows
501
537
  }
502
538
 
503
539
  // check if a thing is there, and if so, try to clobber it
@@ -509,13 +545,30 @@ class Unpack extends Parser {
509
545
  this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
510
546
  }
511
547
 
512
- [CHECKFS2] (entry, done) {
548
+ [PRUNECACHE] (entry) {
513
549
  // if we are not creating a directory, and the path is in the dirCache,
514
550
  // then that means we are about to delete the directory we created
515
551
  // previously, and it is no longer going to be a directory, and neither
516
552
  // is any of its children.
517
- if (entry.type !== 'Directory')
553
+ // If a symbolic link is encountered on Windows, all bets are off.
554
+ // There is no reasonable way to sanitize the cache in such a way
555
+ // we will be able to avoid having filesystem collisions. If this
556
+ // happens with a non-symlink entry, it'll just fail to unpack,
557
+ // but a symlink to a directory, using an 8.3 shortname, can evade
558
+ // detection and lead to arbitrary writes to anywhere on the system.
559
+ if (isWindows && entry.type === 'SymbolicLink')
560
+ dropCache(this.dirCache)
561
+ else if (entry.type !== 'Directory')
518
562
  pruneCache(this.dirCache, entry.absolute)
563
+ }
564
+
565
+ [CHECKFS2] (entry, fullyDone) {
566
+ this[PRUNECACHE](entry)
567
+
568
+ const done = er => {
569
+ this[PRUNECACHE](entry)
570
+ fullyDone(er)
571
+ }
519
572
 
520
573
  const checkCwd = () => {
521
574
  this[MKDIR](this.cwd, this.dmode, er => {
@@ -566,7 +619,13 @@ class Unpack extends Parser {
566
619
  return afterChmod()
567
620
  return fs.chmod(entry.absolute, entry.mode, afterChmod)
568
621
  }
569
- // not a dir entry, have to remove it.
622
+ // Not a dir entry, have to remove it.
623
+ // NB: the only way to end up with an entry that is the cwd
624
+ // itself, in such a way that == does not detect, is a
625
+ // tricky windows absolute path with UNC or 8.3 parts (and
626
+ // preservePaths:true, or else it will have been stripped).
627
+ // In that case, the user has opted out of path protections
628
+ // explicitly, so if they blow away the cwd, c'est la vie.
570
629
  if (entry.absolute !== this.cwd) {
571
630
  return fs.rmdir(entry.absolute, er =>
572
631
  this[MAKEFS](er, entry, done))
@@ -641,8 +700,7 @@ class UnpackSync extends Unpack {
641
700
  }
642
701
 
643
702
  [CHECKFS] (entry) {
644
- if (entry.type !== 'Directory')
645
- pruneCache(this.dirCache, entry.absolute)
703
+ this[PRUNECACHE](entry)
646
704
 
647
705
  if (!this[CHECKED_CWD]) {
648
706
  const er = this[MKDIR](this.cwd, this.dmode)
@@ -691,7 +749,7 @@ class UnpackSync extends Unpack {
691
749
  this[MAKEFS](er, entry)
692
750
  }
693
751
 
694
- [FILE] (entry, _) {
752
+ [FILE] (entry, done) {
695
753
  const mode = entry.mode & 0o7777 || this.fmode
696
754
 
697
755
  const oner = er => {
@@ -703,6 +761,7 @@ class UnpackSync extends Unpack {
703
761
  }
704
762
  if (er || closeError)
705
763
  this[ONERROR](er || closeError, entry)
764
+ done()
706
765
  }
707
766
 
708
767
  let fd
@@ -762,11 +821,14 @@ class UnpackSync extends Unpack {
762
821
  })
763
822
  }
764
823
 
765
- [DIRECTORY] (entry, _) {
824
+ [DIRECTORY] (entry, done) {
766
825
  const mode = entry.mode & 0o7777 || this.dmode
767
826
  const er = this[MKDIR](entry.absolute, mode)
768
- if (er)
769
- return this[ONERROR](er, entry)
827
+ if (er) {
828
+ this[ONERROR](er, entry)
829
+ done()
830
+ return
831
+ }
770
832
  if (entry.mtime && !this.noMtime) {
771
833
  try {
772
834
  fs.utimesSync(entry.absolute, entry.atime || new Date(), entry.mtime)
@@ -777,6 +839,7 @@ class UnpackSync extends Unpack {
777
839
  fs.chownSync(entry.absolute, this[UID](entry), this[GID](entry))
778
840
  } catch (er) {}
779
841
  }
842
+ done()
780
843
  entry.resume()
781
844
  }
782
845
 
@@ -799,9 +862,10 @@ class UnpackSync extends Unpack {
799
862
  }
800
863
  }
801
864
 
802
- [LINK] (entry, linkpath, link, _) {
865
+ [LINK] (entry, linkpath, link, done) {
803
866
  try {
804
867
  fs[link + 'Sync'](linkpath, entry.absolute)
868
+ done()
805
869
  entry.resume()
806
870
  } catch (er) {
807
871
  return this[ONERROR](er, entry)
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
3
3
  "name": "tar",
4
4
  "description": "tar for node",
5
- "version": "6.1.8",
5
+ "version": "6.1.9",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/npm/node-tar.git"