Skip to content

Commit 374460e

Browse files
authored
add optional disablement of symlink validation (#119)
* add optional disablement of symlinks * add test
1 parent 5bfe6df commit 374460e

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22
test/fixtures/copy
33
test/fixtures/invalid
44
test/fixtures/outside
5+
test/fixtures/valid
6+
.DS_Store

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ exports.extract = function extract (cwd, opts) {
131131
const now = new Date()
132132
const umask = typeof opts.umask === 'number' ? ~opts.umask : ~processUmask()
133133
const strict = opts.strict !== false
134+
const validateSymLinks = opts.validateSymlinks !== false
134135

135136
let map = opts.map || noop
136137
let dmode = typeof opts.dmode === 'number' ? opts.dmode : 0
@@ -219,7 +220,7 @@ exports.extract = function extract (cwd, opts) {
219220
if (win32) return next() // skip symlinks on win for now before it can be tested
220221
xfs.unlink(name, function () {
221222
const dst = path.resolve(path.dirname(name), header.linkname)
222-
if (!inCwd(dst)) return next(new Error(name + ' is not a valid symlink'))
223+
if (!inCwd(dst) && validateSymLinks) return next(new Error(name + ' is not a valid symlink'))
223224

224225
xfs.symlink(header.linkname, name, stat)
225226
})

test/fixtures/valid.tar

10 KB
Binary file not shown.

test/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,34 @@ test('do not extract invalid tar', function (t) {
321321
})
322322
})
323323

324+
test('extract tar intended for use by chroot', function (t) {
325+
if (win32) { // no symlink support on win32 currently. TODO: test if this can be enabled somehow
326+
t.plan(1)
327+
t.ok(true)
328+
return
329+
}
330+
331+
t.plan(1)
332+
333+
const a = path.join(__dirname, 'fixtures', 'valid.tar')
334+
335+
const out = path.join(__dirname, 'fixtures', 'valid')
336+
337+
rimraf.sync(out)
338+
339+
fs.createReadStream(a)
340+
.pipe(tar.extract(out, { validateSymlinks: false }))
341+
.on('error', function (err) {
342+
t.ok(/is not a valid symlink/i.test(err.message))
343+
fs.stat(path.join(out, '../bar'), function (err) {
344+
t.ok(err)
345+
})
346+
})
347+
.on('finish', function () {
348+
t.ok(true)
349+
})
350+
})
351+
324352
test('no abs hardlink targets', function (t) {
325353
if (win32) { // no symlink support on win32 currently. TODO: test if this can be enabled somehow
326354
t.plan(1)

0 commit comments

Comments
 (0)