tar 6.1.6 → 6.1.10

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/mkdir.js CHANGED
@@ -8,6 +8,7 @@ const mkdirp = require('mkdirp')
8
8
  const fs = require('fs')
9
9
  const path = require('path')
10
10
  const chownr = require('chownr')
11
+ const normPath = require('./normalize-windows-path.js')
11
12
 
12
13
  class SymlinkError extends Error {
13
14
  constructor (symlink, path) {
@@ -33,7 +34,20 @@ class CwdError extends Error {
33
34
  }
34
35
  }
35
36
 
37
+ const cGet = (cache, key) => cache.get(normPath(key))
38
+ const cSet = (cache, key, val) => cache.set(normPath(key), val)
39
+
40
+ const checkCwd = (dir, cb) => {
41
+ fs.stat(dir, (er, st) => {
42
+ if (er || !st.isDirectory())
43
+ er = new CwdError(dir, er && er.code || 'ENOTDIR')
44
+ cb(er)
45
+ })
46
+ }
47
+
36
48
  module.exports = (dir, opt, cb) => {
49
+ dir = normPath(dir)
50
+
37
51
  // if there's any overlap between mask and mode,
38
52
  // then we'll need an explicit chmod
39
53
  const umask = opt.umask
@@ -49,13 +63,13 @@ module.exports = (dir, opt, cb) => {
49
63
  const preserve = opt.preserve
50
64
  const unlink = opt.unlink
51
65
  const cache = opt.cache
52
- const cwd = opt.cwd
66
+ const cwd = normPath(opt.cwd)
53
67
 
54
68
  const done = (er, created) => {
55
69
  if (er)
56
70
  cb(er)
57
71
  else {
58
- cache.set(dir, true)
72
+ cSet(cache, dir, true)
59
73
  if (created && doChown)
60
74
  chownr(created, uid, gid, er => done(er))
61
75
  else if (needChmod)
@@ -65,22 +79,17 @@ module.exports = (dir, opt, cb) => {
65
79
  }
66
80
  }
67
81
 
68
- if (cache && cache.get(dir) === true)
82
+ if (cache && cGet(cache, dir) === true)
69
83
  return done()
70
84
 
71
- if (dir === cwd) {
72
- return fs.stat(dir, (er, st) => {
73
- if (er || !st.isDirectory())
74
- er = new CwdError(dir, er && er.code || 'ENOTDIR')
75
- done(er)
76
- })
77
- }
85
+ if (dir === cwd)
86
+ return checkCwd(dir, done)
78
87
 
79
88
  if (preserve)
80
89
  return mkdirp(dir, {mode}).then(made => done(null, made), done)
81
90
 
82
- const sub = path.relative(cwd, dir)
83
- const parts = sub.split(/\/|\\/)
91
+ const sub = normPath(path.relative(cwd, dir))
92
+ const parts = sub.split('/')
84
93
  mkdir_(cwd, parts, mode, cache, unlink, cwd, null, done)
85
94
  }
86
95
 
@@ -88,22 +97,19 @@ const mkdir_ = (base, parts, mode, cache, unlink, cwd, created, cb) => {
88
97
  if (!parts.length)
89
98
  return cb(null, created)
90
99
  const p = parts.shift()
91
- const part = base + '/' + p
92
- if (cache.get(part))
100
+ const part = normPath(path.resolve(base + '/' + p))
101
+ if (cGet(cache, part))
93
102
  return mkdir_(part, parts, mode, cache, unlink, cwd, created, cb)
94
103
  fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, cwd, created, cb))
95
104
  }
96
105
 
97
106
  const onmkdir = (part, parts, mode, cache, unlink, cwd, created, cb) => er => {
98
107
  if (er) {
99
- if (er.path && path.dirname(er.path) === cwd &&
100
- (er.code === 'ENOTDIR' || er.code === 'ENOENT'))
101
- return cb(new CwdError(cwd, er.code))
102
-
103
108
  fs.lstat(part, (statEr, st) => {
104
- if (statEr)
109
+ if (statEr) {
110
+ statEr.path = statEr.path && normPath(statEr.path)
105
111
  cb(statEr)
106
- else if (st.isDirectory())
112
+ } else if (st.isDirectory())
107
113
  mkdir_(part, parts, mode, cache, unlink, cwd, created, cb)
108
114
  else if (unlink) {
109
115
  fs.unlink(part, er => {
@@ -122,7 +128,21 @@ const onmkdir = (part, parts, mode, cache, unlink, cwd, created, cb) => er => {
122
128
  }
123
129
  }
124
130
 
131
+ const checkCwdSync = dir => {
132
+ let ok = false
133
+ let code = 'ENOTDIR'
134
+ try {
135
+ ok = fs.statSync(dir).isDirectory()
136
+ } catch (er) {
137
+ code = er.code
138
+ } finally {
139
+ if (!ok)
140
+ throw new CwdError(dir, code)
141
+ }
142
+ }
143
+
125
144
  module.exports.sync = (dir, opt) => {
145
+ dir = normPath(dir)
126
146
  // if there's any overlap between mask and mode,
127
147
  // then we'll need an explicit chmod
128
148
  const umask = opt.umask
@@ -138,64 +158,51 @@ module.exports.sync = (dir, opt) => {
138
158
  const preserve = opt.preserve
139
159
  const unlink = opt.unlink
140
160
  const cache = opt.cache
141
- const cwd = opt.cwd
161
+ const cwd = normPath(opt.cwd)
142
162
 
143
163
  const done = (created) => {
144
- cache.set(dir, true)
164
+ cSet(cache, dir, true)
145
165
  if (created && doChown)
146
166
  chownr.sync(created, uid, gid)
147
167
  if (needChmod)
148
168
  fs.chmodSync(dir, mode)
149
169
  }
150
170
 
151
- if (cache && cache.get(dir) === true)
171
+ if (cache && cGet(cache, dir) === true)
152
172
  return done()
153
173
 
154
174
  if (dir === cwd) {
155
- let ok = false
156
- let code = 'ENOTDIR'
157
- try {
158
- ok = fs.statSync(dir).isDirectory()
159
- } catch (er) {
160
- code = er.code
161
- } finally {
162
- if (!ok)
163
- throw new CwdError(dir, code)
164
- }
165
- done()
166
- return
175
+ checkCwdSync(cwd)
176
+ return done()
167
177
  }
168
178
 
169
179
  if (preserve)
170
180
  return done(mkdirp.sync(dir, mode))
171
181
 
172
- const sub = path.relative(cwd, dir)
173
- const parts = sub.split(/\/|\\/)
182
+ const sub = normPath(path.relative(cwd, dir))
183
+ const parts = sub.split('/')
174
184
  let created = null
175
185
  for (let p = parts.shift(), part = cwd;
176
186
  p && (part += '/' + p);
177
187
  p = parts.shift()) {
178
- if (cache.get(part))
188
+ part = normPath(path.resolve(part))
189
+ if (cGet(cache, part))
179
190
  continue
180
191
 
181
192
  try {
182
193
  fs.mkdirSync(part, mode)
183
194
  created = created || part
184
- cache.set(part, true)
195
+ cSet(cache, part, true)
185
196
  } catch (er) {
186
- if (er.path && path.dirname(er.path) === cwd &&
187
- (er.code === 'ENOTDIR' || er.code === 'ENOENT'))
188
- return new CwdError(cwd, er.code)
189
-
190
197
  const st = fs.lstatSync(part)
191
198
  if (st.isDirectory()) {
192
- cache.set(part, true)
199
+ cSet(cache, part, true)
193
200
  continue
194
201
  } else if (unlink) {
195
202
  fs.unlinkSync(part)
196
203
  fs.mkdirSync(part, mode)
197
204
  created = created || part
198
- cache.set(part, true)
205
+ cSet(cache, part, true)
199
206
  continue
200
207
  } else if (st.isSymbolicLink())
201
208
  return new SymlinkError(part, part + '/' + parts.join('/'))
@@ -0,0 +1,8 @@
1
+ // on windows, either \ or / are valid directory separators.
2
+ // on unix, \ is a valid character in filenames.
3
+ // so, on windows, and only on windows, we replace all \ chars with /,
4
+ // so that we can use / as our one and only directory separator char.
5
+
6
+ const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
7
+ module.exports = platform !== 'win32' ? p => p
8
+ : p => p && p.replace(/\\/g, '/')
package/lib/pack.js CHANGED
@@ -54,6 +54,7 @@ const ONDRAIN = Symbol('ondrain')
54
54
  const fs = require('fs')
55
55
  const path = require('path')
56
56
  const warner = require('./warn-mixin.js')
57
+ const normPath = require('./normalize-windows-path.js')
57
58
 
58
59
  const Pack = warner(class Pack extends MiniPass {
59
60
  constructor (opt) {
@@ -66,7 +67,7 @@ const Pack = warner(class Pack extends MiniPass {
66
67
  this.preservePaths = !!opt.preservePaths
67
68
  this.strict = !!opt.strict
68
69
  this.noPax = !!opt.noPax
69
- this.prefix = (opt.prefix || '').replace(/(\\|\/)+$/, '')
70
+ this.prefix = normPath(opt.prefix || '')
70
71
  this.linkCache = opt.linkCache || new Map()
71
72
  this.statCache = opt.statCache || new Map()
72
73
  this.readdirCache = opt.readdirCache || new Map()
@@ -133,7 +134,7 @@ const Pack = warner(class Pack extends MiniPass {
133
134
  }
134
135
 
135
136
  [ADDTARENTRY] (p) {
136
- const absolute = path.resolve(this.cwd, p.path)
137
+ const absolute = normPath(path.resolve(this.cwd, p.path))
137
138
  // in this case, we don't have to wait for the stat
138
139
  if (!this.filter(p.path, p))
139
140
  p.resume()
@@ -149,7 +150,7 @@ const Pack = warner(class Pack extends MiniPass {
149
150
  }
150
151
 
151
152
  [ADDFSENTRY] (p) {
152
- const absolute = path.resolve(this.cwd, p)
153
+ const absolute = normPath(path.resolve(this.cwd, p))
153
154
  this[QUEUE].push(new PackJob(p, absolute))
154
155
  this[PROCESS]()
155
156
  }
@@ -7,6 +7,12 @@
7
7
  // while still allowing maximal safe parallelization.
8
8
 
9
9
  const assert = require('assert')
10
+ const normPath = require('./normalize-windows-path.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'
10
16
 
11
17
  module.exports = () => {
12
18
  // path => [function or Set]
@@ -18,10 +24,16 @@ module.exports = () => {
18
24
  const reservations = new Map()
19
25
 
20
26
  // return a set of parent dirs for a given path
21
- const { join } = require('path')
22
- const getDirs = path =>
23
- join(path).split(/[\\/]/).slice(0, -1).reduce((set, path) =>
24
- set.length ? set.concat(join(set[set.length - 1], path)) : [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
+ }
25
37
 
26
38
  // functions currently running
27
39
  const running = new Set()
@@ -97,6 +109,18 @@ module.exports = () => {
97
109
  }
98
110
 
99
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
+ return stripSlashes(normPath(join(p)))
120
+ .normalize('NFKD')
121
+ .toLowerCase()
122
+ })
123
+
100
124
  const dirs = new Set(
101
125
  paths.map(path => getDirs(path)).reduce((a, b) => a.concat(b))
102
126
  )
package/lib/read-entry.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
  const MiniPass = require('minipass')
3
+ const normPath = require('./normalize-windows-path.js')
3
4
 
4
5
  const SLURP = Symbol('slurp')
5
6
  module.exports = class ReadEntry extends MiniPass {
@@ -46,7 +47,7 @@ module.exports = class ReadEntry extends MiniPass {
46
47
  this.ignore = true
47
48
  }
48
49
 
49
- this.path = header.path
50
+ this.path = normPath(header.path)
50
51
  this.mode = header.mode
51
52
  if (this.mode)
52
53
  this.mode = this.mode & 0o7777
@@ -58,7 +59,7 @@ module.exports = class ReadEntry extends MiniPass {
58
59
  this.mtime = header.mtime
59
60
  this.atime = header.atime
60
61
  this.ctime = header.ctime
61
- this.linkpath = header.linkpath
62
+ this.linkpath = normPath(header.linkpath)
62
63
  this.uname = header.uname
63
64
  this.gname = header.gname
64
65
 
@@ -93,7 +94,7 @@ module.exports = class ReadEntry extends MiniPass {
93
94
  // a global extended header, because that's weird.
94
95
  if (ex[k] !== null && ex[k] !== undefined &&
95
96
  !(global && k === 'path'))
96
- this[k] = ex[k]
97
+ this[k] = k === 'path' || k === 'linkpath' ? normPath(ex[k]) : ex[k]
97
98
  }
98
99
  }
99
100
  }
package/lib/replace.js CHANGED
@@ -170,7 +170,8 @@ const replace = (opt, files, cb) => {
170
170
 
171
171
  fs.fstat(fd, (er, st) => {
172
172
  if (er)
173
- return reject(er)
173
+ return fs.close(fd, () => reject(er))
174
+
174
175
  getPos(fd, st.size, (er, position) => {
175
176
  if (er)
176
177
  return reject(er)
@@ -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
@@ -15,10 +15,13 @@ const mkdir = require('./mkdir.js')
15
15
  const wc = require('./winchars.js')
16
16
  const pathReservations = require('./path-reservations.js')
17
17
  const stripAbsolutePath = require('./strip-absolute-path.js')
18
+ const normPath = require('./normalize-windows-path.js')
19
+ const stripSlash = require('./strip-trailing-slashes.js')
18
20
 
19
21
  const ONENTRY = Symbol('onEntry')
20
22
  const CHECKFS = Symbol('checkFs')
21
23
  const CHECKFS2 = Symbol('checkFs2')
24
+ const PRUNECACHE = Symbol('pruneCache')
22
25
  const ISREUSABLE = Symbol('isReusable')
23
26
  const MAKEFS = Symbol('makeFs')
24
27
  const FILE = Symbol('file')
@@ -39,13 +42,11 @@ const SKIP = Symbol('skip')
39
42
  const DOCHOWN = Symbol('doChown')
40
43
  const UID = Symbol('uid')
41
44
  const GID = Symbol('gid')
45
+ const CHECKED_CWD = Symbol('checkedCwd')
42
46
  const crypto = require('crypto')
43
47
  const getFlag = require('./get-write-flag.js')
44
-
45
- /* istanbul ignore next */
46
- const neverCalled = () => {
47
- throw new Error('sync function called cb somehow?!?')
48
- }
48
+ const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
49
+ const isWindows = platform === 'win32'
49
50
 
50
51
  // Unlinks on Windows are not atomic.
51
52
  //
@@ -64,7 +65,7 @@ const neverCalled = () => {
64
65
  // See: https://github.com/npm/node-tar/issues/183
65
66
  /* istanbul ignore next */
66
67
  const unlinkFile = (path, cb) => {
67
- if (process.platform !== 'win32')
68
+ if (!isWindows)
68
69
  return fs.unlink(path, cb)
69
70
 
70
71
  const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
@@ -77,7 +78,7 @@ const unlinkFile = (path, cb) => {
77
78
 
78
79
  /* istanbul ignore next */
79
80
  const unlinkFileSync = path => {
80
- if (process.platform !== 'win32')
81
+ if (!isWindows)
81
82
  return fs.unlinkSync(path)
82
83
 
83
84
  const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
@@ -91,6 +92,33 @@ const uint32 = (a, b, c) =>
91
92
  : b === b >>> 0 ? b
92
93
  : c
93
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
+
108
+ const pruneCache = (cache, abs) => {
109
+ abs = cacheKeyNormalize(abs)
110
+ for (const path of cache.keys()) {
111
+ const pnorm = cacheKeyNormalize(path)
112
+ if (pnorm === abs || pnorm.indexOf(abs + '/') === 0)
113
+ cache.delete(path)
114
+ }
115
+ }
116
+
117
+ const dropCache = cache => {
118
+ for (const key of cache.keys())
119
+ cache.delete(key)
120
+ }
121
+
94
122
  class Unpack extends Parser {
95
123
  constructor (opt) {
96
124
  if (!opt)
@@ -103,6 +131,8 @@ class Unpack extends Parser {
103
131
 
104
132
  super(opt)
105
133
 
134
+ this[CHECKED_CWD] = false
135
+
106
136
  this.reservations = pathReservations()
107
137
 
108
138
  this.transform = typeof opt.transform === 'function' ? opt.transform : null
@@ -148,7 +178,7 @@ class Unpack extends Parser {
148
178
  this.forceChown = opt.forceChown === true
149
179
 
150
180
  // turn ><?| in filenames into 0xf000-higher encoded forms
151
- this.win32 = !!opt.win32 || process.platform === 'win32'
181
+ this.win32 = !!opt.win32 || isWindows
152
182
 
153
183
  // do not unpack over files that are newer than what's in the archive
154
184
  this.newer = !!opt.newer
@@ -168,7 +198,7 @@ class Unpack extends Parser {
168
198
  // links, and removes symlink directories rather than erroring
169
199
  this.unlink = !!opt.unlink
170
200
 
171
- this.cwd = path.resolve(opt.cwd || process.cwd())
201
+ this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
172
202
  this.strip = +opt.strip || 0
173
203
  // if we're not chmodding, then we don't need the process umask
174
204
  this.processUmask = opt.noChmod ? 0 : process.umask()
@@ -201,23 +231,24 @@ class Unpack extends Parser {
201
231
 
202
232
  [CHECKPATH] (entry) {
203
233
  if (this.strip) {
204
- const parts = entry.path.split(/\/|\\/)
234
+ const parts = normPath(entry.path).split('/')
205
235
  if (parts.length < this.strip)
206
236
  return false
207
237
  entry.path = parts.slice(this.strip).join('/')
208
- if (entry.path === '' && entry.type !== 'Directory' && entry.type !== 'GNUDumpDir')
209
- return false
210
238
 
211
239
  if (entry.type === 'Link') {
212
- const linkparts = entry.linkpath.split(/\/|\\/)
240
+ const linkparts = normPath(entry.linkpath).split('/')
213
241
  if (linkparts.length >= this.strip)
214
242
  entry.linkpath = linkparts.slice(this.strip).join('/')
243
+ else
244
+ return false
215
245
  }
216
246
  }
217
247
 
218
248
  if (!this.preservePaths) {
219
- const p = entry.path
220
- if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
249
+ const p = normPath(entry.path)
250
+ const parts = p.split('/')
251
+ if (parts.includes('..') || isWindows && /^[a-z]:\.\.$/i.test(parts[0])) {
221
252
  this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
222
253
  entry,
223
254
  path: p,
@@ -225,8 +256,7 @@ class Unpack extends Parser {
225
256
  return false
226
257
  }
227
258
 
228
- // absolutes on posix are also absolutes on win32
229
- // so we only need to test this one to get both
259
+ // strip off the root
230
260
  const [root, stripped] = stripAbsolutePath(p)
231
261
  if (root) {
232
262
  entry.path = stripped
@@ -237,18 +267,42 @@ class Unpack extends Parser {
237
267
  }
238
268
  }
239
269
 
270
+ if (path.isAbsolute(entry.path))
271
+ entry.absolute = normPath(path.resolve(entry.path))
272
+ else
273
+ entry.absolute = normPath(path.resolve(this.cwd, entry.path))
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
+
291
+ // an archive can set properties on the extraction directory, but it
292
+ // may not replace the cwd with a different kind of thing entirely.
293
+ if (entry.absolute === this.cwd &&
294
+ entry.type !== 'Directory' &&
295
+ entry.type !== 'GNUDumpDir')
296
+ return false
297
+
240
298
  // only encode : chars that aren't drive letter indicators
241
299
  if (this.win32) {
242
- const parsed = path.win32.parse(entry.path)
243
- entry.path = parsed.root === '' ? wc.encode(entry.path)
244
- : parsed.root + wc.encode(entry.path.substr(parsed.root.length))
300
+ const { root: aRoot } = path.win32.parse(entry.absolute)
301
+ entry.absolute = aRoot + wc.encode(entry.absolute.substr(aRoot.length))
302
+ const { root: pRoot } = path.win32.parse(entry.path)
303
+ entry.path = pRoot + wc.encode(entry.path.substr(pRoot.length))
245
304
  }
246
305
 
247
- if (path.isAbsolute(entry.path))
248
- entry.absolute = entry.path
249
- else
250
- entry.absolute = path.resolve(this.cwd, entry.path)
251
-
252
306
  return true
253
307
  }
254
308
 
@@ -293,7 +347,7 @@ class Unpack extends Parser {
293
347
  }
294
348
 
295
349
  [MKDIR] (dir, mode, cb) {
296
- mkdir(dir, {
350
+ mkdir(normPath(dir), {
297
351
  uid: this.uid,
298
352
  gid: this.gid,
299
353
  processUid: this.processUid,
@@ -338,6 +392,7 @@ class Unpack extends Parser {
338
392
  stream.on('error', er => {
339
393
  if (stream.fd)
340
394
  fs.close(stream.fd, () => {})
395
+
341
396
  // flush all the data out so that we aren't left hanging
342
397
  // if the error wasn't actually fatal. otherwise the parse
343
398
  // is blocked, and we never proceed.
@@ -352,6 +407,7 @@ class Unpack extends Parser {
352
407
  /* istanbul ignore else - we should always have a fd by now */
353
408
  if (stream.fd)
354
409
  fs.close(stream.fd, () => {})
410
+
355
411
  this[ONERROR](er, entry)
356
412
  fullyDone()
357
413
  return
@@ -451,7 +507,8 @@ class Unpack extends Parser {
451
507
  }
452
508
 
453
509
  [HARDLINK] (entry, done) {
454
- this[LINK](entry, path.resolve(this.cwd, entry.linkpath), 'link', done)
510
+ const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
511
+ this[LINK](entry, linkpath, 'link', done)
455
512
  }
456
513
 
457
514
  [PEND] () {
@@ -476,7 +533,7 @@ class Unpack extends Parser {
476
533
  !this.unlink &&
477
534
  st.isFile() &&
478
535
  st.nlink <= 1 &&
479
- process.platform !== 'win32'
536
+ !isWindows
480
537
  }
481
538
 
482
539
  // check if a thing is there, and if so, try to clobber it
@@ -488,46 +545,107 @@ class Unpack extends Parser {
488
545
  this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
489
546
  }
490
547
 
491
- [CHECKFS2] (entry, done) {
548
+ [PRUNECACHE] (entry) {
492
549
  // if we are not creating a directory, and the path is in the dirCache,
493
550
  // then that means we are about to delete the directory we created
494
551
  // previously, and it is no longer going to be a directory, and neither
495
552
  // is any of its children.
496
- if (entry.type !== 'Directory') {
497
- for (const path of this.dirCache.keys()) {
498
- if (path === entry.absolute ||
499
- path.indexOf(entry.absolute + '/') === 0 ||
500
- path.indexOf(entry.absolute + '\\') === 0)
501
- this.dirCache.delete(path)
502
- }
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')
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)
503
571
  }
504
572
 
505
- this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
506
- if (er) {
507
- this[ONERROR](er, entry)
508
- done()
509
- return
573
+ const checkCwd = () => {
574
+ this[MKDIR](this.cwd, this.dmode, er => {
575
+ if (er) {
576
+ this[ONERROR](er, entry)
577
+ done()
578
+ return
579
+ }
580
+ this[CHECKED_CWD] = true
581
+ start()
582
+ })
583
+ }
584
+
585
+ const start = () => {
586
+ if (entry.absolute !== this.cwd) {
587
+ const parent = normPath(path.dirname(entry.absolute))
588
+ if (parent !== this.cwd) {
589
+ return this[MKDIR](parent, this.dmode, er => {
590
+ if (er) {
591
+ this[ONERROR](er, entry)
592
+ done()
593
+ return
594
+ }
595
+ afterMakeParent()
596
+ })
597
+ }
510
598
  }
511
- fs.lstat(entry.absolute, (er, st) => {
599
+ afterMakeParent()
600
+ }
601
+
602
+ const afterMakeParent = () => {
603
+ fs.lstat(entry.absolute, (lstatEr, st) => {
512
604
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
513
605
  this[SKIP](entry)
514
606
  done()
515
- } else if (er || this[ISREUSABLE](entry, st))
516
- this[MAKEFS](null, entry, done)
517
- else if (st.isDirectory()) {
607
+ return
608
+ }
609
+ if (lstatEr || this[ISREUSABLE](entry, st))
610
+ return this[MAKEFS](null, entry, done)
611
+
612
+ if (st.isDirectory()) {
518
613
  if (entry.type === 'Directory') {
519
- if (!this.noChmod && (!entry.mode || (st.mode & 0o7777) === entry.mode))
520
- this[MAKEFS](null, entry, done)
521
- else {
522
- fs.chmod(entry.absolute, entry.mode,
523
- er => this[MAKEFS](er, entry, done))
524
- }
525
- } else
526
- fs.rmdir(entry.absolute, er => this[MAKEFS](er, entry, done))
527
- } else
528
- unlinkFile(entry.absolute, er => this[MAKEFS](er, entry, done))
614
+ const needChmod = !this.noChmod &&
615
+ entry.mode &&
616
+ (st.mode & 0o7777) !== entry.mode
617
+ const afterChmod = er => this[MAKEFS](er, entry, done)
618
+ if (!needChmod)
619
+ return afterChmod()
620
+ return fs.chmod(entry.absolute, entry.mode, afterChmod)
621
+ }
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.
629
+ if (entry.absolute !== this.cwd) {
630
+ return fs.rmdir(entry.absolute, er =>
631
+ this[MAKEFS](er, entry, done))
632
+ }
633
+ }
634
+
635
+ // not a dir, and not reusable
636
+ // don't remove if the cwd, we want that error
637
+ if (entry.absolute === this.cwd)
638
+ return this[MAKEFS](null, entry, done)
639
+
640
+ unlinkFile(entry.absolute, er =>
641
+ this[MAKEFS](er, entry, done))
529
642
  })
530
- })
643
+ }
644
+
645
+ if (this[CHECKED_CWD])
646
+ start()
647
+ else
648
+ checkCwd()
531
649
  }
532
650
 
533
651
  [MAKEFS] (er, entry, done) {
@@ -556,7 +674,7 @@ class Unpack extends Parser {
556
674
  }
557
675
 
558
676
  [LINK] (entry, linkpath, link, done) {
559
- // XXX: get the type ('file' or 'dir') for windows
677
+ // XXX: get the type ('symlink' or 'junction') for windows
560
678
  fs[link](linkpath, entry.absolute, er => {
561
679
  if (er)
562
680
  this[ONERROR](er, entry)
@@ -569,47 +687,69 @@ class Unpack extends Parser {
569
687
  }
570
688
  }
571
689
 
690
+ const callSync = fn => {
691
+ try {
692
+ return [null, fn()]
693
+ } catch (er) {
694
+ return [er, null]
695
+ }
696
+ }
572
697
  class UnpackSync extends Unpack {
698
+ [MAKEFS] (er, entry) {
699
+ return super[MAKEFS](er, entry, () => {})
700
+ }
701
+
573
702
  [CHECKFS] (entry) {
574
- if (entry.type !== 'Directory') {
575
- for (const path of this.dirCache.keys()) {
576
- if (path === entry.absolute ||
577
- path.indexOf(entry.absolute + '/') === 0 ||
578
- path.indexOf(entry.absolute + '\\') === 0)
579
- this.dirCache.delete(path)
703
+ this[PRUNECACHE](entry)
704
+
705
+ if (!this[CHECKED_CWD]) {
706
+ const er = this[MKDIR](this.cwd, this.dmode)
707
+ if (er)
708
+ return this[ONERROR](er, entry)
709
+ this[CHECKED_CWD] = true
710
+ }
711
+
712
+ // don't bother to make the parent if the current entry is the cwd,
713
+ // we've already checked it.
714
+ if (entry.absolute !== this.cwd) {
715
+ const parent = normPath(path.dirname(entry.absolute))
716
+ if (parent !== this.cwd) {
717
+ const mkParent = this[MKDIR](parent, this.dmode)
718
+ if (mkParent)
719
+ return this[ONERROR](mkParent, entry)
580
720
  }
581
721
  }
582
722
 
583
- const er = this[MKDIR](path.dirname(entry.absolute), this.dmode, neverCalled)
584
- if (er)
585
- return this[ONERROR](er, entry)
586
- try {
587
- const st = fs.lstatSync(entry.absolute)
588
- if (this.keep || this.newer && st.mtime > entry.mtime)
589
- return this[SKIP](entry)
590
- else if (this[ISREUSABLE](entry, st))
591
- return this[MAKEFS](null, entry, neverCalled)
592
- else {
593
- try {
594
- if (st.isDirectory()) {
595
- if (entry.type === 'Directory') {
596
- if (!this.noChmod && entry.mode && (st.mode & 0o7777) !== entry.mode)
597
- fs.chmodSync(entry.absolute, entry.mode)
598
- } else
599
- fs.rmdirSync(entry.absolute)
600
- } else
601
- unlinkFileSync(entry.absolute)
602
- return this[MAKEFS](null, entry, neverCalled)
603
- } catch (er) {
604
- return this[ONERROR](er, entry)
605
- }
723
+ const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
724
+ if (st && (this.keep || this.newer && st.mtime > entry.mtime))
725
+ return this[SKIP](entry)
726
+
727
+ if (lstatEr || this[ISREUSABLE](entry, st))
728
+ return this[MAKEFS](null, entry)
729
+
730
+ if (st.isDirectory()) {
731
+ if (entry.type === 'Directory') {
732
+ const needChmod = !this.noChmod &&
733
+ entry.mode &&
734
+ (st.mode & 0o7777) !== entry.mode
735
+ const [er] = needChmod ? callSync(() => {
736
+ fs.chmodSync(entry.absolute, entry.mode)
737
+ }) : []
738
+ return this[MAKEFS](er, entry)
606
739
  }
607
- } catch (er) {
608
- return this[MAKEFS](null, entry, neverCalled)
740
+ // not a dir entry, have to remove it
741
+ const [er] = callSync(() => fs.rmdirSync(entry.absolute))
742
+ this[MAKEFS](er, entry)
609
743
  }
744
+
745
+ // not a dir, and not reusable.
746
+ // don't remove if it's the cwd, since we want that error.
747
+ const [er] = entry.absolute === this.cwd ? []
748
+ : callSync(() => unlinkFileSync(entry.absolute))
749
+ this[MAKEFS](er, entry)
610
750
  }
611
751
 
612
- [FILE] (entry, _) {
752
+ [FILE] (entry, done) {
613
753
  const mode = entry.mode & 0o7777 || this.fmode
614
754
 
615
755
  const oner = er => {
@@ -621,6 +761,7 @@ class UnpackSync extends Unpack {
621
761
  }
622
762
  if (er || closeError)
623
763
  this[ONERROR](er || closeError, entry)
764
+ done()
624
765
  }
625
766
 
626
767
  let fd
@@ -680,11 +821,14 @@ class UnpackSync extends Unpack {
680
821
  })
681
822
  }
682
823
 
683
- [DIRECTORY] (entry, _) {
824
+ [DIRECTORY] (entry, done) {
684
825
  const mode = entry.mode & 0o7777 || this.dmode
685
826
  const er = this[MKDIR](entry.absolute, mode)
686
- if (er)
687
- return this[ONERROR](er, entry)
827
+ if (er) {
828
+ this[ONERROR](er, entry)
829
+ done()
830
+ return
831
+ }
688
832
  if (entry.mtime && !this.noMtime) {
689
833
  try {
690
834
  fs.utimesSync(entry.absolute, entry.atime || new Date(), entry.mtime)
@@ -695,12 +839,13 @@ class UnpackSync extends Unpack {
695
839
  fs.chownSync(entry.absolute, this[UID](entry), this[GID](entry))
696
840
  } catch (er) {}
697
841
  }
842
+ done()
698
843
  entry.resume()
699
844
  }
700
845
 
701
846
  [MKDIR] (dir, mode) {
702
847
  try {
703
- return mkdir.sync(dir, {
848
+ return mkdir.sync(normPath(dir), {
704
849
  uid: this.uid,
705
850
  gid: this.gid,
706
851
  processUid: this.processUid,
@@ -717,9 +862,10 @@ class UnpackSync extends Unpack {
717
862
  }
718
863
  }
719
864
 
720
- [LINK] (entry, linkpath, link, _) {
865
+ [LINK] (entry, linkpath, link, done) {
721
866
  try {
722
867
  fs[link + 'Sync'](linkpath, entry.absolute)
868
+ done()
723
869
  entry.resume()
724
870
  } catch (er) {
725
871
  return this[ONERROR](er, entry)
@@ -4,12 +4,14 @@ const Pax = require('./pax.js')
4
4
  const Header = require('./header.js')
5
5
  const fs = require('fs')
6
6
  const path = require('path')
7
+ const normPath = require('./normalize-windows-path.js')
8
+ const stripSlash = require('./strip-trailing-slashes.js')
7
9
 
8
10
  const prefixPath = (path, prefix) => {
9
11
  if (!prefix)
10
- return path
11
- path = path.replace(/^\.([/\\]|$)/, '')
12
- return prefix + '/' + path
12
+ return normPath(path)
13
+ path = normPath(path).replace(/^\.(\/|$)/, '')
14
+ return stripSlash(prefix) + '/' + path
13
15
  }
14
16
 
15
17
  const maxReadSize = 16 * 1024 * 1024
@@ -31,6 +33,7 @@ const MODE = Symbol('mode')
31
33
  const AWAITDRAIN = Symbol('awaitDrain')
32
34
  const ONDRAIN = Symbol('ondrain')
33
35
  const PREFIX = Symbol('prefix')
36
+ const HAD_ERROR = Symbol('hadError')
34
37
  const warner = require('./warn-mixin.js')
35
38
  const winchars = require('./winchars.js')
36
39
  const stripAbsolutePath = require('./strip-absolute-path.js')
@@ -43,22 +46,22 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
43
46
  super(opt)
44
47
  if (typeof p !== 'string')
45
48
  throw new TypeError('path is required')
46
- this.path = p
49
+ this.path = normPath(p)
47
50
  // suppress atime, ctime, uid, gid, uname, gname
48
51
  this.portable = !!opt.portable
49
52
  // until node has builtin pwnam functions, this'll have to do
50
- this.myuid = process.getuid && process.getuid()
53
+ this.myuid = process.getuid && process.getuid() || 0
51
54
  this.myuser = process.env.USER || ''
52
55
  this.maxReadSize = opt.maxReadSize || maxReadSize
53
56
  this.linkCache = opt.linkCache || new Map()
54
57
  this.statCache = opt.statCache || new Map()
55
58
  this.preservePaths = !!opt.preservePaths
56
- this.cwd = opt.cwd || process.cwd()
59
+ this.cwd = normPath(opt.cwd || process.cwd())
57
60
  this.strict = !!opt.strict
58
61
  this.noPax = !!opt.noPax
59
62
  this.noMtime = !!opt.noMtime
60
63
  this.mtime = opt.mtime || null
61
- this.prefix = opt.prefix || null
64
+ this.prefix = opt.prefix ? normPath(opt.prefix) : null
62
65
 
63
66
  this.fd = null
64
67
  this.blockLen = null
@@ -83,11 +86,13 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
83
86
 
84
87
  this.win32 = !!opt.win32 || process.platform === 'win32'
85
88
  if (this.win32) {
89
+ // force the \ to / normalization, since we might not *actually*
90
+ // be on windows, but want \ to be considered a path separator.
86
91
  this.path = winchars.decode(this.path.replace(/\\/g, '/'))
87
92
  p = p.replace(/\\/g, '/')
88
93
  }
89
94
 
90
- this.absolute = opt.absolute || path.resolve(this.cwd, p)
95
+ this.absolute = normPath(opt.absolute || path.resolve(this.cwd, p))
91
96
 
92
97
  if (this.path === '')
93
98
  this.path = './'
@@ -105,6 +110,12 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
105
110
  this[LSTAT]()
106
111
  }
107
112
 
113
+ emit (ev, ...data) {
114
+ if (ev === 'error')
115
+ this[HAD_ERROR] = true
116
+ return super.emit(ev, ...data)
117
+ }
118
+
108
119
  [LSTAT] () {
109
120
  fs.lstat(this.absolute, (er, stat) => {
110
121
  if (er)
@@ -201,14 +212,14 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
201
212
  }
202
213
 
203
214
  [ONREADLINK] (linkpath) {
204
- this.linkpath = linkpath.replace(/\\/g, '/')
215
+ this.linkpath = normPath(linkpath)
205
216
  this[HEADER]()
206
217
  this.end()
207
218
  }
208
219
 
209
220
  [HARDLINK] (linkpath) {
210
221
  this.type = 'Link'
211
- this.linkpath = path.relative(this.cwd, linkpath).replace(/\\/g, '/')
222
+ this.linkpath = normPath(path.relative(this.cwd, linkpath))
212
223
  this.stat.size = 0
213
224
  this[HEADER]()
214
225
  this.end()
@@ -242,6 +253,9 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
242
253
 
243
254
  [ONOPENFILE] (fd) {
244
255
  this.fd = fd
256
+ if (this[HAD_ERROR])
257
+ return this[CLOSE]()
258
+
245
259
  this.blockLen = 512 * Math.ceil(this.stat.size / 512)
246
260
  this.blockRemain = this.blockLen
247
261
  const bufLen = Math.min(this.blockLen, this.maxReadSize)
@@ -403,7 +417,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
403
417
 
404
418
  this.prefix = opt.prefix || null
405
419
 
406
- this.path = readEntry.path
420
+ this.path = normPath(readEntry.path)
407
421
  this.mode = this[MODE](readEntry.mode)
408
422
  this.uid = this.portable ? null : readEntry.uid
409
423
  this.gid = this.portable ? null : readEntry.gid
@@ -413,7 +427,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
413
427
  this.mtime = this.noMtime ? null : opt.mtime || readEntry.mtime
414
428
  this.atime = this.portable ? null : readEntry.atime
415
429
  this.ctime = this.portable ? null : readEntry.ctime
416
- this.linkpath = readEntry.linkpath
430
+ this.linkpath = normPath(readEntry.linkpath)
417
431
 
418
432
  if (typeof opt.onwarn === 'function')
419
433
  this.on('warn', opt.onwarn)
package/package.json CHANGED
@@ -2,13 +2,15 @@
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.6",
5
+ "version": "6.1.10",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/npm/node-tar.git"
9
9
  },
10
10
  "scripts": {
11
- "test": "tap",
11
+ "test:posix": "tap",
12
+ "test:win32": "tap --lines=98 --branches=98 --statements=98 --functions=98",
13
+ "test": "node test/fixtures/test.js",
12
14
  "posttest": "npm run lint",
13
15
  "eslint": "eslint",
14
16
  "lint": "npm run eslint -- test lib",