Skip to content

Commit 2cb1120

Browse files
scumfrogisaacs
authored andcommitted
fix(unpack): improve UnpackSync symlink error "into" path accuracy
UnpackSync[ENSURE_NO_SYMLINK] previously constructed SymlinkError's "into" path using the full original linkpath parts array, which could produce misleading diagnostics. Build the "into" path from the original `cwd` value and the `parts` list.
1 parent d18e4e1 commit 2cb1120

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

src/unpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ export class UnpackSync extends Unpack {
11611161
if (er) return done()
11621162
if (st.isSymbolicLink()) {
11631163
return onError(
1164-
new SymlinkError(t, path.resolve(t, parts.join('/'))),
1164+
new SymlinkError(t, path.resolve(cwd, parts.join('/'))),
11651165
)
11661166
}
11671167
}

test/pack.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ t.test('pack a file', t => {
9494

9595
t.equal(sync.subarray(512).length, data.subarray(512).length)
9696
t.equal(
97-
(sync.subarray(512).toString()),
98-
(data.subarray(512).toString()),
97+
sync.subarray(512).toString(),
98+
data.subarray(512).toString(),
9999
)
100100
const hs = new Header(sync)
101101
t.match(hs, expect)

test/unpack.js

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,7 +3317,11 @@ t.test('ignore self-referential hardlinks', async t => {
33173317
])
33183318
const check = (t, warnings) => {
33193319
t.matchSnapshot(warnings)
3320-
t.strictSame(fs.readdirSync(t.testdirName), [], 'nothing extracted')
3320+
t.strictSame(
3321+
fs.readdirSync(t.testdirName),
3322+
[],
3323+
'nothing extracted',
3324+
)
33213325
t.end()
33223326
}
33233327
t.test('async', t => {
@@ -3450,10 +3454,11 @@ t.test('no linking through a symlink', t => {
34503454
'',
34513455
'',
34523456
])
3453-
const setup = t => t.testdir({
3454-
x: {},
3455-
'exploited-file': 'original content',
3456-
})
3457+
const setup = t =>
3458+
t.testdir({
3459+
x: {},
3460+
'exploited-file': 'original content',
3461+
})
34573462
const check = t => {
34583463
fs.writeFileSync(t.testdirName + '/x/exploit', 'pwned')
34593464
t.equal(
@@ -3463,20 +3468,38 @@ t.test('no linking through a symlink', t => {
34633468
}
34643469
t.test('sync', t => {
34653470
const cwd = setup(t)
3466-
t.throws(() => {
3467-
new UnpackSync({ cwd, strict: true }).end(exploit)
3468-
})
3471+
t.throws(
3472+
() => {
3473+
new UnpackSync({ cwd, strict: true }).end(exploit)
3474+
},
3475+
{
3476+
name: 'SymlinkError',
3477+
message: /^TAR_SYMLINK_ERROR/,
3478+
path: /a.b.escape.exploited-file$/,
3479+
symlink: /a.b.escape$/,
3480+
},
3481+
)
34693482
check(t)
34703483
t.end()
34713484
})
34723485
t.test('async', async t => {
34733486
const cwd = setup(t)
3474-
await t.rejects(new Promise((res, rej) => {
3475-
new Unpack({ cwd, strict: true })
3476-
.on('finish', res)
3477-
.on('error', rej)
3478-
.end(exploit)
3479-
}))
3487+
await t.rejects(
3488+
new Promise(
3489+
(res, rej) => {
3490+
new Unpack({ cwd, strict: true })
3491+
.on('finish', res)
3492+
.on('error', rej)
3493+
.end(exploit)
3494+
},
3495+
{
3496+
name: 'SymlinkError',
3497+
message: /^TAR_SYMLINK_ERROR/,
3498+
path: /a.b.escape.exploited-file$/,
3499+
symlink: /a.b.escape$/,
3500+
},
3501+
),
3502+
)
34803503
check(t)
34813504
})
34823505
t.end()

0 commit comments

Comments
 (0)