2024-04-03 19:57:52 +02:00
|
|
|
import { remove, writeFile } from 'fs-extra';
|
|
|
|
import { createTempDir } from '@joplin/lib/testing/test-utils';
|
|
|
|
import { join } from 'path';
|
2024-04-10 12:35:35 +02:00
|
|
|
import isSafeToOpen from './isSafeToOpen';
|
2024-04-03 19:57:52 +02:00
|
|
|
|
|
|
|
|
2024-04-10 12:35:35 +02:00
|
|
|
describe('isSafeToOpen', () => {
|
2024-04-03 19:57:52 +02:00
|
|
|
test.each([
|
|
|
|
{ fileName: 'a.txt', expected: true },
|
|
|
|
{ fileName: 'a.json', expected: true },
|
|
|
|
{ fileName: 'a.JSON', expected: true },
|
|
|
|
{ fileName: 'a.tar.gz', expected: true },
|
|
|
|
{ fileName: 'a.exe', expected: false },
|
|
|
|
{ fileName: 'test.com', expected: false },
|
|
|
|
{ fileName: 'a.pyc', expected: false },
|
|
|
|
{ fileName: 'a.pyo', expected: false },
|
|
|
|
{ fileName: 'a.pyw', expected: false },
|
|
|
|
{ fileName: 'a.jar', expected: false },
|
|
|
|
{ fileName: 'a.bat', expected: false },
|
|
|
|
{ fileName: 'a.cmd', expected: false },
|
|
|
|
{ fileName: 'noExtension', expected: false },
|
|
|
|
])('should mark executable files as possibly unsafe to open (%j)', async ({ fileName, expected }) => {
|
|
|
|
const tempDir = await createTempDir();
|
|
|
|
try {
|
|
|
|
const fullPath = join(tempDir, fileName);
|
|
|
|
await writeFile(fullPath, 'test');
|
2024-04-10 12:35:35 +02:00
|
|
|
expect(await isSafeToOpen(fullPath)).toBe(expected);
|
2024-04-03 19:57:52 +02:00
|
|
|
} finally {
|
|
|
|
await remove(tempDir);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|