Skip to content

Commit

Permalink
fix: checking if symlink points outside the directory. (electron#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaietta committed Oct 23, 2024
1 parent b773063 commit c8774f3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/crawlfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { glob as _glob, IOptions } from 'glob';

import fs from './wrapped-fs';
import { Stats } from 'fs';
import * as path from 'path';

const glob = promisify(_glob);

Expand Down Expand Up @@ -47,8 +48,13 @@ export async function crawl(dir: string, options: IOptions) {
// those appearing in archives we need to manually exclude theme here
const exactLinkIndex = links.findIndex((link) => filename === link);
return links.every((link, index) => {
if (index === exactLinkIndex) return true;
return !filename.startsWith(link);
if (index === exactLinkIndex) {
return true;
}
const isFileWithinSymlinkDir = filename.startsWith(link);
// symlink may point outside the directory: https://github.com/electron/asar/issues/303
const relativePath = path.relative(link, path.dirname(filename));
return !isFileWithinSymlinkDir || relativePath.startsWith('..');
});
});
return [filenames, metadata] as const;
Expand Down
15 changes: 15 additions & 0 deletions test/api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ describe('api', function () {
'test/input/packthis-with-symlink/real.txt',
);
});
it('should extract an archive with symlink having the same prefix', async () => {
await asar.createPackageWithOptions(
'test/input/packthis-with-symlink-same-prefix/',
'tmp/packthis-with-symlink-same-prefix.asar',
{ dot: false },
);
asar.extractAll(
'tmp/packthis-with-symlink-same-prefix.asar',
'tmp/packthis-with-symlink-same-prefix/',
);
return compFiles(
'tmp/packthis-with-symlink-same-prefix/real.txt',
'test/input/packthis-with-symlink-same-prefix/real.txt',
);
});
it('should not extract an archive with a bad symlink', async () => {
assert.throws(() => {
asar.extractAll('test/input/bad-symlink.asar', 'tmp/bad-symlink/');
Expand Down
1 change: 1 addition & 0 deletions test/input/packthis-with-symlink-same-prefix/A
1 change: 1 addition & 0 deletions test/input/packthis-with-symlink-same-prefix/AA/real.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I AM REAL TXT FILE
1 change: 1 addition & 0 deletions test/input/packthis-with-symlink-same-prefix/real.txt

0 comments on commit c8774f3

Please sign in to comment.