Skip to content

Commit dfc5923

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 575a511 commit dfc5923

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

lib/unpack.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ class Unpack extends Parser {
220220
const linkparts = normPath(entry.linkpath).split('/')
221221
if (linkparts.length >= this.strip)
222222
entry.linkpath = linkparts.slice(this.strip).join('/')
223+
else
224+
return false
223225
}
224226
}
225227

test/unpack.js

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

0 commit comments

Comments
 (0)