Skip to content

Commit a1dd7e7

Browse files
committed
refactor and throw on bad symlink
1 parent 7ce355d commit a1dd7e7

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ exports.extract = function extract (cwd, opts) {
120120
if (!cwd) cwd = '.'
121121
if (!opts) opts = {}
122122

123+
cwd = path.resolve(cwd)
124+
123125
const xfs = opts.fs || fs
124126
const ignore = opts.ignore || opts.filter || noop
125127
const mapStream = opts.mapStream || echo
@@ -216,18 +218,21 @@ exports.extract = function extract (cwd, opts) {
216218
function onsymlink () {
217219
if (win32) return next() // skip symlinks on win for now before it can be tested
218220
xfs.unlink(name, function () {
221+
const dst = path.resolve(path.dirname(name), header.linkname)
222+
if (!inCwd(dst)) return next(new Error(name + ' is not a valid symlink'))
223+
219224
xfs.symlink(header.linkname, name, stat)
220225
})
221226
}
222227

223228
function onlink () {
224229
if (win32) return next() // skip links on win for now before it can be tested
225230
xfs.unlink(name, function () {
226-
const srcpath = path.join(cwd, path.join('/', header.linkname))
231+
const dst = path.join(cwd, path.join('/', header.linkname))
227232

228-
xfs.link(srcpath, name, function (err) {
233+
xfs.link(dst, name, function (err) {
229234
if (err && err.code === 'EPERM' && opts.hardlinkAsFilesFallback) {
230-
stream = xfs.createReadStream(srcpath)
235+
stream = xfs.createReadStream(dst)
231236
return onfile()
232237
}
233238

@@ -236,6 +241,10 @@ exports.extract = function extract (cwd, opts) {
236241
})
237242
}
238243

244+
function inCwd (dst) {
245+
return dst.startsWith(cwd)
246+
}
247+
239248
function onfile () {
240249
const ws = xfs.createWriteStream(name)
241250
const rs = mapStream(stream, header)

test/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ test('do not extract invalid tar', function (t) {
311311
fs.createReadStream(a)
312312
.pipe(tar.extract(out))
313313
.on('error', function (err) {
314-
t.ok(/is not a valid path/i.test(err.message))
314+
t.ok(/is not a valid symlink/i.test(err.message))
315315
fs.stat(path.join(out, '../bar'), function (err) {
316316
t.ok(err)
317317
})

0 commit comments

Comments
 (0)