Skip to content

Commit 6aafff0

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 5c5059a commit 6aafff0

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
@@ -209,6 +209,8 @@ class Unpack extends Parser {
209209
const linkparts = normPath(entry.linkpath).split('/')
210210
if (linkparts.length >= this.strip)
211211
entry.linkpath = linkparts.slice(this.strip).join('/')
212+
else
213+
return false
212214
}
213215
}
214216

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)