Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: checking if symlink with same prefix points outside the directory #335

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 } from 'glob';

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

const glob = promisify(_glob);
Expand Down Expand Up @@ -48,8 +49,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