Skip to content

Commit

Permalink
validate parent paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Apr 29, 2018
1 parent e074d9c commit 2712c11
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
test/fixtures/copy
test/fixtures/invalid
38 changes: 27 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,28 @@ exports.extract = function (cwd, opts) {
}, stat)
}

mkdirfix(path.dirname(name), {
fs: xfs, own: own, uid: header.uid, gid: header.gid
}, function (err) {
var dir = path.dirname(name)

validate(xfs, dir, path.join(cwd, '.'), function (err, valid) {
if (err) return next(err)
if (!valid) return next(new Error(dir + ' is not a valid path'))

switch (header.type) {
case 'file': return onfile()
case 'link': return onlink()
case 'symlink': return onsymlink()
}
mkdirfix(dir, {
fs: xfs, own: own, uid: header.uid, gid: header.gid
}, function (err) {
if (err) return next(err)

if (strict) return next(new Error('unsupported type for ' + name + ' (' + header.type + ')'))
switch (header.type) {
case 'file': return onfile()
case 'link': return onlink()
case 'symlink': return onsymlink()
}

stream.resume()
next()
if (strict) return next(new Error('unsupported type for ' + name + ' (' + header.type + ')'))

stream.resume()
next()
})
})
})

Expand All @@ -318,6 +325,15 @@ exports.extract = function (cwd, opts) {
return extract
}

function validate (fs, name, root, cb) {
if (name === root) return cb(null, true)
fs.lstat(name, function (err, st) {
if (err && err.code !== 'ENOENT') return cb(err)
if (err || st.isDirectory()) return validate(fs, path.join(name, '..'), root, cb)
cb(null, false)
})
}

function mkdirfix (name, opts, cb) {
mkdirp(name, {fs: opts.xfs}, function (err, made) {
if (!err && made && opts.own) {
Expand Down
Binary file added test/fixtures/invalid.tar
Binary file not shown.
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,21 @@ test('not finalizing the pack', function (t) {
t.deepEqual(aFiles, ['hello.txt'])
}
})

test('do not extract invalid tar', function (t) {
var a = path.join(__dirname, 'fixtures', 'invalid.tar')

var out = path.join(__dirname, 'fixtures', 'invalid')

rimraf.sync(out)

fs.createReadStream(a)
.pipe(tar.extract(out))
.on('error', function (err) {
t.ok(/is not a valid path/i.test(err.message))
fs.stat(path.join(out, '../bar'), function (err) {
t.ok(err)
t.end()
})
})
})

0 comments on commit 2712c11

Please sign in to comment.