tar 6.1.7 → 6.1.11
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/normalize-unicode.js +11 -0
- package/lib/path-reservations.js +27 -6
- package/lib/strip-absolute-path.js +12 -2
- package/lib/strip-trailing-slashes.js +10 -21
- package/lib/unpack.js +88 -22
- package/package.json +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// warning: extremely hot code path.
|
|
2
|
+
// This has been meticulously optimized for use
|
|
3
|
+
// within npm install on large package trees.
|
|
4
|
+
// Do not edit without careful benchmarking.
|
|
5
|
+
const normalizeCache = Object.create(null)
|
|
6
|
+
const {hasOwnProperty} = Object.prototype
|
|
7
|
+
module.exports = s => {
|
|
8
|
+
if (!hasOwnProperty.call(normalizeCache, s))
|
|
9
|
+
normalizeCache[s] = s.normalize('NFKD')
|
|
10
|
+
return normalizeCache[s]
|
|
11
|
+
}
|
package/lib/path-reservations.js
CHANGED
|
@@ -7,7 +7,12 @@
|
|
|
7
7
|
// while still allowing maximal safe parallelization.
|
|
8
8
|
|
|
9
9
|
const assert = require('assert')
|
|
10
|
-
const
|
|
10
|
+
const normalize = require('./normalize-unicode.js')
|
|
11
|
+
const stripSlashes = require('./strip-trailing-slashes.js')
|
|
12
|
+
const { join } = require('path')
|
|
13
|
+
|
|
14
|
+
const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
|
|
15
|
+
const isWindows = platform === 'win32'
|
|
11
16
|
|
|
12
17
|
module.exports = () => {
|
|
13
18
|
// path => [function or Set]
|
|
@@ -19,11 +24,16 @@ module.exports = () => {
|
|
|
19
24
|
const reservations = new Map()
|
|
20
25
|
|
|
21
26
|
// return a set of parent dirs for a given path
|
|
22
|
-
|
|
23
|
-
const getDirs = path =>
|
|
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 = 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,6 +109,17 @@ module.exports = () => {
|
|
|
99
109
|
}
|
|
100
110
|
|
|
101
111
|
const reserve = (paths, fn) => {
|
|
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
|
+
// don't need normPath, because we skip this entirely for windows
|
|
120
|
+
return normalize(stripSlashes(join(p))).toLowerCase()
|
|
121
|
+
})
|
|
122
|
+
|
|
102
123
|
const dirs = new Set(
|
|
103
124
|
paths.map(path => getDirs(path)).reduce((a, b) => a.concat(b))
|
|
104
125
|
)
|
|
@@ -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
|
}
|
|
@@ -1,24 +1,13 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
'/'.repeat(1024),
|
|
6
|
-
'/'.repeat(512),
|
|
7
|
-
'/'.repeat(256),
|
|
8
|
-
'/'.repeat(128),
|
|
9
|
-
'/'.repeat(64),
|
|
10
|
-
'/'.repeat(32),
|
|
11
|
-
'/'.repeat(16),
|
|
12
|
-
'/'.repeat(8),
|
|
13
|
-
'/'.repeat(4),
|
|
14
|
-
'/'.repeat(2),
|
|
15
|
-
'/',
|
|
16
|
-
]
|
|
17
|
-
|
|
1
|
+
// warning: extremely hot code path.
|
|
2
|
+
// This has been meticulously optimized for use
|
|
3
|
+
// within npm install on large package trees.
|
|
4
|
+
// Do not edit without careful benchmarking.
|
|
18
5
|
module.exports = str => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
6
|
+
let i = str.length - 1
|
|
7
|
+
let slashesStart = -1
|
|
8
|
+
while (i > -1 && str.charAt(i) === '/') {
|
|
9
|
+
slashesStart = i
|
|
10
|
+
i--
|
|
22
11
|
}
|
|
23
|
-
return str
|
|
12
|
+
return slashesStart === -1 ? str : str.slice(0, slashesStart)
|
|
24
13
|
}
|
package/lib/unpack.js
CHANGED
|
@@ -16,10 +16,13 @@ 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')
|
|
20
|
+
const normalize = require('./normalize-unicode.js')
|
|
19
21
|
|
|
20
22
|
const ONENTRY = Symbol('onEntry')
|
|
21
23
|
const CHECKFS = Symbol('checkFs')
|
|
22
24
|
const CHECKFS2 = Symbol('checkFs2')
|
|
25
|
+
const PRUNECACHE = Symbol('pruneCache')
|
|
23
26
|
const ISREUSABLE = Symbol('isReusable')
|
|
24
27
|
const MAKEFS = Symbol('makeFs')
|
|
25
28
|
const FILE = Symbol('file')
|
|
@@ -43,6 +46,8 @@ const GID = Symbol('gid')
|
|
|
43
46
|
const CHECKED_CWD = Symbol('checkedCwd')
|
|
44
47
|
const crypto = require('crypto')
|
|
45
48
|
const getFlag = require('./get-write-flag.js')
|
|
49
|
+
const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
|
|
50
|
+
const isWindows = platform === 'win32'
|
|
46
51
|
|
|
47
52
|
// Unlinks on Windows are not atomic.
|
|
48
53
|
//
|
|
@@ -61,7 +66,7 @@ const getFlag = require('./get-write-flag.js')
|
|
|
61
66
|
// See: https://github.com/npm/node-tar/issues/183
|
|
62
67
|
/* istanbul ignore next */
|
|
63
68
|
const unlinkFile = (path, cb) => {
|
|
64
|
-
if (
|
|
69
|
+
if (!isWindows)
|
|
65
70
|
return fs.unlink(path, cb)
|
|
66
71
|
|
|
67
72
|
const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
|
|
@@ -74,7 +79,7 @@ const unlinkFile = (path, cb) => {
|
|
|
74
79
|
|
|
75
80
|
/* istanbul ignore next */
|
|
76
81
|
const unlinkFileSync = path => {
|
|
77
|
-
if (
|
|
82
|
+
if (!isWindows)
|
|
78
83
|
return fs.unlinkSync(path)
|
|
79
84
|
|
|
80
85
|
const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
|
|
@@ -88,17 +93,32 @@ const uint32 = (a, b, c) =>
|
|
|
88
93
|
: b === b >>> 0 ? b
|
|
89
94
|
: c
|
|
90
95
|
|
|
96
|
+
// clear the cache if it's a case-insensitive unicode-squashing match.
|
|
97
|
+
// we can't know if the current file system is case-sensitive or supports
|
|
98
|
+
// unicode fully, so we check for similarity on the maximally compatible
|
|
99
|
+
// representation. Err on the side of pruning, since all it's doing is
|
|
100
|
+
// preventing lstats, and it's not the end of the world if we get a false
|
|
101
|
+
// positive.
|
|
102
|
+
// Note that on windows, we always drop the entire cache whenever a
|
|
103
|
+
// symbolic link is encountered, because 8.3 filenames are impossible
|
|
104
|
+
// to reason about, and collisions are hazards rather than just failures.
|
|
105
|
+
const cacheKeyNormalize = path => normalize(stripSlash(normPath(path)))
|
|
106
|
+
.toLowerCase()
|
|
107
|
+
|
|
91
108
|
const pruneCache = (cache, abs) => {
|
|
92
|
-
|
|
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
|
|
97
|
-
if (
|
|
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 ||
|
|
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
|
|
@@ -220,12 +240,15 @@ class Unpack extends Parser {
|
|
|
220
240
|
const linkparts = normPath(entry.linkpath).split('/')
|
|
221
241
|
if (linkparts.length >= this.strip)
|
|
222
242
|
entry.linkpath = linkparts.slice(this.strip).join('/')
|
|
243
|
+
else
|
|
244
|
+
return false
|
|
223
245
|
}
|
|
224
246
|
}
|
|
225
247
|
|
|
226
248
|
if (!this.preservePaths) {
|
|
227
249
|
const p = normPath(entry.path)
|
|
228
|
-
|
|
250
|
+
const parts = p.split('/')
|
|
251
|
+
if (parts.includes('..') || isWindows && /^[a-z]:\.\.$/i.test(parts[0])) {
|
|
229
252
|
this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
|
|
230
253
|
entry,
|
|
231
254
|
path: p,
|
|
@@ -233,8 +256,7 @@ class Unpack extends Parser {
|
|
|
233
256
|
return false
|
|
234
257
|
}
|
|
235
258
|
|
|
236
|
-
//
|
|
237
|
-
// so we only need to test this one to get both
|
|
259
|
+
// strip off the root
|
|
238
260
|
const [root, stripped] = stripAbsolutePath(p)
|
|
239
261
|
if (root) {
|
|
240
262
|
entry.path = stripped
|
|
@@ -250,6 +272,22 @@ class Unpack extends Parser {
|
|
|
250
272
|
else
|
|
251
273
|
entry.absolute = normPath(path.resolve(this.cwd, entry.path))
|
|
252
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
|
+
|
|
253
291
|
// an archive can set properties on the extraction directory, but it
|
|
254
292
|
// may not replace the cwd with a different kind of thing entirely.
|
|
255
293
|
if (entry.absolute === this.cwd &&
|
|
@@ -495,7 +533,7 @@ class Unpack extends Parser {
|
|
|
495
533
|
!this.unlink &&
|
|
496
534
|
st.isFile() &&
|
|
497
535
|
st.nlink <= 1 &&
|
|
498
|
-
|
|
536
|
+
!isWindows
|
|
499
537
|
}
|
|
500
538
|
|
|
501
539
|
// check if a thing is there, and if so, try to clobber it
|
|
@@ -507,13 +545,30 @@ class Unpack extends Parser {
|
|
|
507
545
|
this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
|
|
508
546
|
}
|
|
509
547
|
|
|
510
|
-
[
|
|
548
|
+
[PRUNECACHE] (entry) {
|
|
511
549
|
// if we are not creating a directory, and the path is in the dirCache,
|
|
512
550
|
// then that means we are about to delete the directory we created
|
|
513
551
|
// previously, and it is no longer going to be a directory, and neither
|
|
514
552
|
// is any of its children.
|
|
515
|
-
|
|
553
|
+
// If a symbolic link is encountered, all bets are off. There is no
|
|
554
|
+
// reasonable way to sanitize the cache in such a way we will be able to
|
|
555
|
+
// avoid having filesystem collisions. If this happens with a non-symlink
|
|
556
|
+
// entry, it'll just fail to unpack, but a symlink to a directory, using an
|
|
557
|
+
// 8.3 shortname or certain unicode attacks, can evade detection and lead
|
|
558
|
+
// to arbitrary writes to anywhere on the system.
|
|
559
|
+
if (entry.type === 'SymbolicLink')
|
|
560
|
+
dropCache(this.dirCache)
|
|
561
|
+
else if (entry.type !== 'Directory')
|
|
516
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
|
+
}
|
|
517
572
|
|
|
518
573
|
const checkCwd = () => {
|
|
519
574
|
this[MKDIR](this.cwd, this.dmode, er => {
|
|
@@ -564,7 +619,13 @@ class Unpack extends Parser {
|
|
|
564
619
|
return afterChmod()
|
|
565
620
|
return fs.chmod(entry.absolute, entry.mode, afterChmod)
|
|
566
621
|
}
|
|
567
|
-
//
|
|
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.
|
|
568
629
|
if (entry.absolute !== this.cwd) {
|
|
569
630
|
return fs.rmdir(entry.absolute, er =>
|
|
570
631
|
this[MAKEFS](er, entry, done))
|
|
@@ -639,8 +700,7 @@ class UnpackSync extends Unpack {
|
|
|
639
700
|
}
|
|
640
701
|
|
|
641
702
|
[CHECKFS] (entry) {
|
|
642
|
-
|
|
643
|
-
pruneCache(this.dirCache, entry.absolute)
|
|
703
|
+
this[PRUNECACHE](entry)
|
|
644
704
|
|
|
645
705
|
if (!this[CHECKED_CWD]) {
|
|
646
706
|
const er = this[MKDIR](this.cwd, this.dmode)
|
|
@@ -689,7 +749,7 @@ class UnpackSync extends Unpack {
|
|
|
689
749
|
this[MAKEFS](er, entry)
|
|
690
750
|
}
|
|
691
751
|
|
|
692
|
-
[FILE] (entry,
|
|
752
|
+
[FILE] (entry, done) {
|
|
693
753
|
const mode = entry.mode & 0o7777 || this.fmode
|
|
694
754
|
|
|
695
755
|
const oner = er => {
|
|
@@ -701,6 +761,7 @@ class UnpackSync extends Unpack {
|
|
|
701
761
|
}
|
|
702
762
|
if (er || closeError)
|
|
703
763
|
this[ONERROR](er || closeError, entry)
|
|
764
|
+
done()
|
|
704
765
|
}
|
|
705
766
|
|
|
706
767
|
let fd
|
|
@@ -760,11 +821,14 @@ class UnpackSync extends Unpack {
|
|
|
760
821
|
})
|
|
761
822
|
}
|
|
762
823
|
|
|
763
|
-
[DIRECTORY] (entry,
|
|
824
|
+
[DIRECTORY] (entry, done) {
|
|
764
825
|
const mode = entry.mode & 0o7777 || this.dmode
|
|
765
826
|
const er = this[MKDIR](entry.absolute, mode)
|
|
766
|
-
if (er)
|
|
767
|
-
|
|
827
|
+
if (er) {
|
|
828
|
+
this[ONERROR](er, entry)
|
|
829
|
+
done()
|
|
830
|
+
return
|
|
831
|
+
}
|
|
768
832
|
if (entry.mtime && !this.noMtime) {
|
|
769
833
|
try {
|
|
770
834
|
fs.utimesSync(entry.absolute, entry.atime || new Date(), entry.mtime)
|
|
@@ -775,6 +839,7 @@ class UnpackSync extends Unpack {
|
|
|
775
839
|
fs.chownSync(entry.absolute, this[UID](entry), this[GID](entry))
|
|
776
840
|
} catch (er) {}
|
|
777
841
|
}
|
|
842
|
+
done()
|
|
778
843
|
entry.resume()
|
|
779
844
|
}
|
|
780
845
|
|
|
@@ -797,9 +862,10 @@ class UnpackSync extends Unpack {
|
|
|
797
862
|
}
|
|
798
863
|
}
|
|
799
864
|
|
|
800
|
-
[LINK] (entry, linkpath, link,
|
|
865
|
+
[LINK] (entry, linkpath, link, done) {
|
|
801
866
|
try {
|
|
802
867
|
fs[link + 'Sync'](linkpath, entry.absolute)
|
|
868
|
+
done()
|
|
803
869
|
entry.resume()
|
|
804
870
|
} catch (er) {
|
|
805
871
|
return this[ONERROR](er, entry)
|
package/package.json
CHANGED