Skip to content

Commit 9ec1830

Browse files
authored
fix: checking if symlink with same prefix points outside the directory (#335)
fix: checking if symlink points outside the directory. (#303)
1 parent f30b93d commit 9ec1830

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

src/crawlfs.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { glob as _glob } from 'glob';
33

44
import fs from './wrapped-fs';
55
import { Stats } from 'fs';
6+
import * as path from 'path';
67
import { IOptions } from './types/glob';
78

89
const glob = promisify(_glob);
@@ -48,8 +49,13 @@ export async function crawl(dir: string, options: IOptions) {
4849
// those appearing in archives we need to manually exclude theme here
4950
const exactLinkIndex = links.findIndex((link) => filename === link);
5051
return links.every((link, index) => {
51-
if (index === exactLinkIndex) return true;
52-
return !filename.startsWith(link);
52+
if (index === exactLinkIndex) {
53+
return true;
54+
}
55+
const isFileWithinSymlinkDir = filename.startsWith(link);
56+
// symlink may point outside the directory: https://github.com/electron/asar/issues/303
57+
const relativePath = path.relative(link, path.dirname(filename));
58+
return !isFileWithinSymlinkDir || relativePath.startsWith('..');
5359
});
5460
});
5561
return [filenames, metadata] as const;

test/api-spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ describe('api', function () {
119119
'test/input/packthis-with-symlink/real.txt',
120120
);
121121
});
122+
it('should extract an archive with symlink having the same prefix', async () => {
123+
await asar.createPackageWithOptions(
124+
'test/input/packthis-with-symlink-same-prefix/',
125+
'tmp/packthis-with-symlink-same-prefix.asar',
126+
{ dot: false },
127+
);
128+
asar.extractAll(
129+
'tmp/packthis-with-symlink-same-prefix.asar',
130+
'tmp/packthis-with-symlink-same-prefix/',
131+
);
132+
return compFiles(
133+
'tmp/packthis-with-symlink-same-prefix/real.txt',
134+
'test/input/packthis-with-symlink-same-prefix/real.txt',
135+
);
136+
});
122137
it('should not extract an archive with a bad symlink', async () => {
123138
assert.throws(() => {
124139
asar.extractAll('test/input/bad-symlink.asar', 'tmp/bad-symlink/');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AA
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I AM REAL TXT FILE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AA/real.txt

0 commit comments

Comments
 (0)