Skip to content

Commit 4f7abe9

Browse files
fix: ensure symlinks do not write unexpectedly (#322)
* fix: ensure symlinks do not write unexpectedly * update spec name:
1 parent 88b5ea4 commit 4f7abe9

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

src/asar.ts

+8
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ export function extractAll(archivePath: string, dest: string) {
221221
const filename = fullPath.substr(1);
222222
const destFilename = path.join(dest, filename);
223223
const file = filesystem.getFile(filename, followLinks);
224+
if (path.relative(dest, destFilename).startsWith('..')) {
225+
throw new Error(`${fullPath}: file "${destFilename}" writes out of the package`);
226+
}
224227
if ('files' in file) {
225228
// it's a directory, create it and continue with the next entry
226229
fs.mkdirpSync(destFilename);
@@ -234,6 +237,11 @@ export function extractAll(archivePath: string, dest: string) {
234237
fs.unlinkSync(destFilename);
235238
} catch {}
236239
const linkTo = path.join(relativePath, path.basename(file.link));
240+
if (path.relative(dest, linkSrcPath).startsWith('..')) {
241+
throw new Error(
242+
`${fullPath}: file "${file.link}" links out of the package to "${linkSrcPath}"`,
243+
);
244+
}
237245
fs.symlinkSync(linkTo, destFilename);
238246
} else {
239247
// it's a file, try to extract it

test/api-spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ describe('api', function () {
119119
'test/input/packthis-with-symlink/real.txt',
120120
);
121121
});
122+
it('should not extract an archive with a bad symlink', async () => {
123+
assert.throws(() => {
124+
asar.extractAll('test/input/bad-symlink.asar', 'tmp/bad-symlink/');
125+
});
126+
});
122127
it('should handle multibyte characters in paths', async () => {
123128
await asar.createPackageWithOptions(
124129
'test/input/packthis-unicode-path/',

test/input/bad-symlink.asar

339 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)