Skip to content

Commit 21f3e9b

Browse files
committed
fix: skip extract if linkpath is stripped entirely
Fix tar.Unpack() to skip extraction of hardlinks and symlinks when a 'strip' option is provided, if the entry linkpath would be completely stripped. Previously, the linkpath would not be stripped if it had fewer path parts than the strip option. This matches the behavior of modern versions of bsdtar. Gnutar has the same extraction semantics, but emits a warning when the resulting linkpath is completely stripped.
1 parent 32027d6 commit 21f3e9b

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

lib/unpack.js

+2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ class Unpack extends Parser {
219219
const linkparts = normPath(entry.linkpath).split('/')
220220
if (linkparts.length >= this.strip)
221221
entry.linkpath = linkparts.slice(this.strip).join('/')
222+
else
223+
return false
222224
}
223225
}
224226

test/unpack.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,8 @@ t.test('links!', t => {
191191
t.end()
192192
}
193193
const checkForStrip3 = t => {
194-
t.ok(fs.lstatSync(dir + '/3').isDirectory())
195-
let err = null
196-
try {
197-
fs.lstatSync(dir + '/3/hardlink-3')
198-
} catch(e) {
199-
err = e
200-
}
201-
// can't be extracted because we've passed it in the tar
202-
// (specially crafted tar for this not to work)
203-
t.equal(err.code, 'ENOENT')
194+
// strips the linkpath entirely, so the link doesn't get extracted.
195+
t.throws(() => fs.lstatSync(dir + '/3'), { code: 'ENOENT' })
204196
t.end()
205197
}
206198

0 commit comments

Comments
 (0)