tar 4.4.17 → 4.4.18
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/path-reservations.js +26 -5
- package/lib/strip-absolute-path.js +12 -2
- package/lib/unpack.js +95 -25
- package/package.json +1 -1
package/lib/path-reservations.js
CHANGED
|
@@ -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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
@@ -18,10 +18,12 @@ const wc = require('./winchars.js')
|
|
|
18
18
|
const stripAbsolutePath = require('./strip-absolute-path.js')
|
|
19
19
|
const pathReservations = require('./path-reservations.js')
|
|
20
20
|
const normPath = require('./normalize-windows-path.js')
|
|
21
|
+
const stripSlash = require('./strip-trailing-slashes.js')
|
|
21
22
|
|
|
22
23
|
const ONENTRY = Symbol('onEntry')
|
|
23
24
|
const CHECKFS = Symbol('checkFs')
|
|
24
25
|
const CHECKFS2 = Symbol('checkFs2')
|
|
26
|
+
const PRUNECACHE = Symbol('pruneCache')
|
|
25
27
|
const ISREUSABLE = Symbol('isReusable')
|
|
26
28
|
const MAKEFS = Symbol('makeFs')
|
|
27
29
|
const FILE = Symbol('file')
|
|
@@ -45,6 +47,8 @@ const UID = Symbol('uid')
|
|
|
45
47
|
const GID = Symbol('gid')
|
|
46
48
|
const CHECKED_CWD = Symbol('checkedCwd')
|
|
47
49
|
const crypto = require('crypto')
|
|
50
|
+
const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
|
|
51
|
+
const isWindows = platform === 'win32'
|
|
48
52
|
|
|
49
53
|
// Unlinks on Windows are not atomic.
|
|
50
54
|
//
|
|
@@ -63,7 +67,7 @@ const crypto = require('crypto')
|
|
|
63
67
|
// See: https://github.com/npm/node-tar/issues/183
|
|
64
68
|
/* istanbul ignore next */
|
|
65
69
|
const unlinkFile = (path, cb) => {
|
|
66
|
-
if (
|
|
70
|
+
if (!isWindows)
|
|
67
71
|
return fs.unlink(path, cb)
|
|
68
72
|
|
|
69
73
|
const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
|
|
@@ -76,7 +80,7 @@ const unlinkFile = (path, cb) => {
|
|
|
76
80
|
|
|
77
81
|
/* istanbul ignore next */
|
|
78
82
|
const unlinkFileSync = path => {
|
|
79
|
-
if (
|
|
83
|
+
if (!isWindows)
|
|
80
84
|
return fs.unlinkSync(path)
|
|
81
85
|
|
|
82
86
|
const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
|
|
@@ -90,17 +94,33 @@ const uint32 = (a, b, c) =>
|
|
|
90
94
|
: b === b >>> 0 ? b
|
|
91
95
|
: c
|
|
92
96
|
|
|
97
|
+
// clear the cache if it's a case-insensitive unicode-squashing match.
|
|
98
|
+
// we can't know if the current file system is case-sensitive or supports
|
|
99
|
+
// unicode fully, so we check for similarity on the maximally compatible
|
|
100
|
+
// representation. Err on the side of pruning, since all it's doing is
|
|
101
|
+
// preventing lstats, and it's not the end of the world if we get a false
|
|
102
|
+
// positive.
|
|
103
|
+
// Note that on windows, we always drop the entire cache whenever a
|
|
104
|
+
// symbolic link is encountered, because 8.3 filenames are impossible
|
|
105
|
+
// to reason about, and collisions are hazards rather than just failures.
|
|
106
|
+
const cacheKeyNormalize = path => stripSlash(normPath(path))
|
|
107
|
+
.normalize('NFKD')
|
|
108
|
+
.toLowerCase()
|
|
109
|
+
|
|
93
110
|
const pruneCache = (cache, abs) => {
|
|
94
|
-
|
|
95
|
-
// know if the current file system is case-sensitive or not.
|
|
96
|
-
abs = normPath(abs).toLowerCase()
|
|
111
|
+
abs = cacheKeyNormalize(abs)
|
|
97
112
|
for (const path of cache.keys()) {
|
|
98
|
-
const
|
|
99
|
-
if (
|
|
113
|
+
const pnorm = cacheKeyNormalize(path)
|
|
114
|
+
if (pnorm === abs || pnorm.indexOf(abs + '/') === 0)
|
|
100
115
|
cache.delete(path)
|
|
101
116
|
}
|
|
102
117
|
}
|
|
103
118
|
|
|
119
|
+
const dropCache = cache => {
|
|
120
|
+
for (const key of cache.keys())
|
|
121
|
+
cache.delete(key)
|
|
122
|
+
}
|
|
123
|
+
|
|
104
124
|
class Unpack extends Parser {
|
|
105
125
|
constructor (opt) {
|
|
106
126
|
if (!opt)
|
|
@@ -159,7 +179,7 @@ class Unpack extends Parser {
|
|
|
159
179
|
this.forceChown = opt.forceChown === true
|
|
160
180
|
|
|
161
181
|
// turn ><?| in filenames into 0xf000-higher encoded forms
|
|
162
|
-
this.win32 = !!opt.win32 ||
|
|
182
|
+
this.win32 = !!opt.win32 || isWindows
|
|
163
183
|
|
|
164
184
|
// do not unpack over files that are newer than what's in the archive
|
|
165
185
|
this.newer = !!opt.newer
|
|
@@ -216,13 +236,13 @@ class Unpack extends Parser {
|
|
|
216
236
|
|
|
217
237
|
if (!this.preservePaths) {
|
|
218
238
|
const p = normPath(entry.path)
|
|
219
|
-
|
|
239
|
+
const parts = p.split('/')
|
|
240
|
+
if (parts.includes('..') || isWindows && /^[a-z]:\.\.$/i.test(parts[0])) {
|
|
220
241
|
this.warn(`path contains '..'`, p)
|
|
221
242
|
return false
|
|
222
243
|
}
|
|
223
244
|
|
|
224
|
-
//
|
|
225
|
-
// so we only need to test this one to get both
|
|
245
|
+
// strip off the root
|
|
226
246
|
const s = stripAbsolutePath(p)
|
|
227
247
|
if (s[0]) {
|
|
228
248
|
entry.path = s[1]
|
|
@@ -235,6 +255,22 @@ class Unpack extends Parser {
|
|
|
235
255
|
else
|
|
236
256
|
entry.absolute = normPath(path.resolve(this.cwd, entry.path))
|
|
237
257
|
|
|
258
|
+
// if we somehow ended up with a path that escapes the cwd, and we are
|
|
259
|
+
// not in preservePaths mode, then something is fishy! This should have
|
|
260
|
+
// been prevented above, so ignore this for coverage.
|
|
261
|
+
/* istanbul ignore if - defense in depth */
|
|
262
|
+
if (!this.preservePaths &&
|
|
263
|
+
entry.absolute.indexOf(this.cwd + '/') !== 0 &&
|
|
264
|
+
entry.absolute !== this.cwd) {
|
|
265
|
+
this.warn('TAR_ENTRY_ERROR', 'path escaped extraction target', {
|
|
266
|
+
entry,
|
|
267
|
+
path: normPath(entry.path),
|
|
268
|
+
resolvedPath: entry.absolute,
|
|
269
|
+
cwd: this.cwd,
|
|
270
|
+
})
|
|
271
|
+
return false
|
|
272
|
+
}
|
|
273
|
+
|
|
238
274
|
// an archive can set properties on the extraction directory, but it
|
|
239
275
|
// may not replace the cwd with a different kind of thing entirely.
|
|
240
276
|
if (entry.absolute === this.cwd &&
|
|
@@ -470,7 +506,7 @@ class Unpack extends Parser {
|
|
|
470
506
|
!this.unlink &&
|
|
471
507
|
st.isFile() &&
|
|
472
508
|
st.nlink <= 1 &&
|
|
473
|
-
|
|
509
|
+
!isWindows
|
|
474
510
|
}
|
|
475
511
|
|
|
476
512
|
// check if a thing is there, and if so, try to clobber it
|
|
@@ -481,13 +517,31 @@ class Unpack extends Parser {
|
|
|
481
517
|
paths.push(entry.linkpath)
|
|
482
518
|
this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
|
|
483
519
|
}
|
|
484
|
-
|
|
520
|
+
|
|
521
|
+
[PRUNECACHE] (entry) {
|
|
485
522
|
// if we are not creating a directory, and the path is in the dirCache,
|
|
486
523
|
// then that means we are about to delete the directory we created
|
|
487
524
|
// previously, and it is no longer going to be a directory, and neither
|
|
488
525
|
// is any of its children.
|
|
489
|
-
|
|
526
|
+
// If a symbolic link is encountered on Windows, all bets are off.
|
|
527
|
+
// There is no reasonable way to sanitize the cache in such a way
|
|
528
|
+
// we will be able to avoid having filesystem collisions. If this
|
|
529
|
+
// happens with a non-symlink entry, it'll just fail to unpack,
|
|
530
|
+
// but a symlink to a directory, using an 8.3 shortname, can evade
|
|
531
|
+
// detection and lead to arbitrary writes to anywhere on the system.
|
|
532
|
+
if (isWindows && entry.type === 'SymbolicLink')
|
|
533
|
+
dropCache(this.dirCache)
|
|
534
|
+
else if (entry.type !== 'Directory')
|
|
490
535
|
pruneCache(this.dirCache, entry.absolute)
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
[CHECKFS2] (entry, fullyDone) {
|
|
539
|
+
this[PRUNECACHE](entry)
|
|
540
|
+
|
|
541
|
+
const done = er => {
|
|
542
|
+
this[PRUNECACHE](entry)
|
|
543
|
+
fullyDone(er)
|
|
544
|
+
}
|
|
491
545
|
|
|
492
546
|
const checkCwd = () => {
|
|
493
547
|
this[MKDIR](this.cwd, this.dmode, er => {
|
|
@@ -538,7 +592,13 @@ class Unpack extends Parser {
|
|
|
538
592
|
return afterChmod()
|
|
539
593
|
return fs.chmod(entry.absolute, entry.mode, afterChmod)
|
|
540
594
|
}
|
|
541
|
-
//
|
|
595
|
+
// Not a dir entry, have to remove it.
|
|
596
|
+
// NB: the only way to end up with an entry that is the cwd
|
|
597
|
+
// itself, in such a way that == does not detect, is a
|
|
598
|
+
// tricky windows absolute path with UNC or 8.3 parts (and
|
|
599
|
+
// preservePaths:true, or else it will have been stripped).
|
|
600
|
+
// In that case, the user has opted out of path protections
|
|
601
|
+
// explicitly, so if they blow away the cwd, c'est la vie.
|
|
542
602
|
if (entry.absolute !== this.cwd) {
|
|
543
603
|
return fs.rmdir(entry.absolute, er =>
|
|
544
604
|
this[MAKEFS](er, entry, done))
|
|
@@ -608,8 +668,7 @@ class UnpackSync extends Unpack {
|
|
|
608
668
|
}
|
|
609
669
|
|
|
610
670
|
[CHECKFS] (entry) {
|
|
611
|
-
|
|
612
|
-
pruneCache(this.dirCache, entry.absolute)
|
|
671
|
+
this[PRUNECACHE](entry)
|
|
613
672
|
|
|
614
673
|
if (!this[CHECKED_CWD]) {
|
|
615
674
|
const er = this[MKDIR](this.cwd, this.dmode)
|
|
@@ -658,13 +717,19 @@ class UnpackSync extends Unpack {
|
|
|
658
717
|
this[MAKEFS](er, entry)
|
|
659
718
|
}
|
|
660
719
|
|
|
661
|
-
[FILE] (entry,
|
|
720
|
+
[FILE] (entry, done) {
|
|
662
721
|
const mode = entry.mode & 0o7777 || this.fmode
|
|
663
722
|
|
|
664
723
|
const oner = er => {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
724
|
+
let closeError
|
|
725
|
+
try {
|
|
726
|
+
fs.closeSync(fd)
|
|
727
|
+
} catch (e) {
|
|
728
|
+
closeError = e
|
|
729
|
+
}
|
|
730
|
+
if (er || closeError)
|
|
731
|
+
this[ONERROR](er || closeError, entry)
|
|
732
|
+
done()
|
|
668
733
|
}
|
|
669
734
|
|
|
670
735
|
let stream
|
|
@@ -725,11 +790,14 @@ class UnpackSync extends Unpack {
|
|
|
725
790
|
})
|
|
726
791
|
}
|
|
727
792
|
|
|
728
|
-
[DIRECTORY] (entry,
|
|
793
|
+
[DIRECTORY] (entry, done) {
|
|
729
794
|
const mode = entry.mode & 0o7777 || this.dmode
|
|
730
795
|
const er = this[MKDIR](entry.absolute, mode)
|
|
731
|
-
if (er)
|
|
732
|
-
|
|
796
|
+
if (er) {
|
|
797
|
+
this[ONERROR](er, entry)
|
|
798
|
+
done()
|
|
799
|
+
return
|
|
800
|
+
}
|
|
733
801
|
if (entry.mtime && !this.noMtime) {
|
|
734
802
|
try {
|
|
735
803
|
fs.utimesSync(entry.absolute, entry.atime || new Date(), entry.mtime)
|
|
@@ -740,6 +808,7 @@ class UnpackSync extends Unpack {
|
|
|
740
808
|
fs.chownSync(entry.absolute, this[UID](entry), this[GID](entry))
|
|
741
809
|
} catch (er) {}
|
|
742
810
|
}
|
|
811
|
+
done()
|
|
743
812
|
entry.resume()
|
|
744
813
|
}
|
|
745
814
|
|
|
@@ -762,9 +831,10 @@ class UnpackSync extends Unpack {
|
|
|
762
831
|
}
|
|
763
832
|
}
|
|
764
833
|
|
|
765
|
-
[LINK] (entry, linkpath, link,
|
|
834
|
+
[LINK] (entry, linkpath, link, done) {
|
|
766
835
|
try {
|
|
767
836
|
fs[link + 'Sync'](linkpath, entry.absolute)
|
|
837
|
+
done()
|
|
768
838
|
entry.resume()
|
|
769
839
|
} catch (er) {
|
|
770
840
|
return this[ONERROR](er, entry)
|