Check that argument is an url or a regular file before opening

Changelog-entry: Check that argument is an url or a regular file before opening
Change-type: patch
This commit is contained in:
Alexis Svinartchouk 2020-06-10 14:48:44 +02:00
parent b1376dfa73
commit 52bdd02a4b

View File

@ -17,6 +17,7 @@
import { delay } from 'bluebird'; import { delay } from 'bluebird';
import * as electron from 'electron'; import * as electron from 'electron';
import { autoUpdater } from 'electron-updater'; import { autoUpdater } from 'electron-updater';
import { promises as fs } from 'fs';
import { platform } from 'os'; import { platform } from 'os';
import * as _ from 'lodash'; import * as _ from 'lodash';
import * as path from 'path'; import * as path from 'path';
@ -57,7 +58,17 @@ async function checkForUpdates(interval: number) {
} }
} }
function getCommandLineURL(argv: string[]): string | undefined { async function isFile(filePath: string): Promise<boolean> {
try {
const stat = await fs.stat(filePath);
return stat.isFile();
} catch {
// noop
}
return false;
}
async function getCommandLineURL(argv: string[]): Promise<string | undefined> {
argv = argv.slice(electron.app.isPackaged ? 1 : 2); argv = argv.slice(electron.app.isPackaged ? 1 : 2);
if (argv.length) { if (argv.length) {
const value = argv[argv.length - 1]; const value = argv[argv.length - 1];
@ -69,6 +80,14 @@ function getCommandLineURL(argv: string[]): string | undefined {
if (platform() === 'darwin' && value.startsWith('-psn_')) { if (platform() === 'darwin' && value.startsWith('-psn_')) {
return; return;
} }
if (
!value.startsWith('http://') &&
!value.startsWith('https://') &&
!value.startsWith(scheme) &&
!(await isFile(value))
) {
return;
}
return value; return value;
} }
} }
@ -204,9 +223,9 @@ async function main(): Promise<void> {
window.restore(); window.restore();
} }
window.focus(); window.focus();
await selectImageURL(getCommandLineURL(argv)); await selectImageURL(await getCommandLineURL(argv));
}); });
await selectImageURL(getCommandLineURL(process.argv)); await selectImageURL(await getCommandLineURL(process.argv));
} }
} }