Check for improperly-formatted arguments (fix #885) (PR #1080)

Verifies short arguments do not contain an extra dash ( right: `-h`, error: `--h` )
Verifies long arguments contain at least two dashes ( right: `--help`, error: `-help` )
This commit is contained in:
erythros 2020-12-06 12:34:32 -05:00 committed by GitHub
parent 9b608d4d24
commit 27ecfcbeca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import * as dns from 'dns';
import * as log from 'loglevel';
import { buildNativefierApp } from './main';
import { isArgFormatValid } from './helpers/helpers';
import { isWindows } from './helpers/helpers';
// package.json is `require`d to let tsc strip the `src` folder by determining
@ -68,6 +69,12 @@ function checkInternet(): void {
if (require.main === module) {
const sanitizedArgs = [];
process.argv.forEach((arg) => {
if (isArgFormatValid(arg) === false) {
log.error(
`Invalid argument passed: ${arg} .\nNativefier supports short options (like "-n") and long options (like "--name"), all lowercase. Run "nativefier --help" for help.\nAborting`,
);
process.exit(1);
}
if (sanitizedArgs.length > 0) {
const previousArg = sanitizedArgs[sanitizedArgs.length - 1];

View File

@ -0,0 +1,39 @@
import { isArgFormatValid } from './helpers';
describe('isArgFormatValid', () => {
test('is true for short arguments', () => {
expect(isArgFormatValid('-t')).toBe(true);
});
test('is false for improperly formatted short arguments', () => {
expect(isArgFormatValid('--t')).toBe(false);
});
test('is true for long arguments', () => {
expect(isArgFormatValid('--test')).toBe(true);
});
test('is false for improperly formatted long arguments', () => {
expect(isArgFormatValid('---test')).toBe(false);
});
test('is false for improperly formatted long arguments', () => {
expect(isArgFormatValid('-test')).toBe(false);
});
test('is true for long arguments with dashes', () => {
expect(isArgFormatValid('--test-run')).toBe(true);
});
test('is false for improperly formatted long arguments with dashes', () => {
expect(isArgFormatValid('--test--run')).toBe(false);
});
test('is true for long arguments with many dashes', () => {
expect(isArgFormatValid('--test-run-with-many-dashes')).toBe(true);
});
test('is false for improperly formatted excessively long arguments', () => {
expect(isArgFormatValid('--test--run--with--many--dashes')).toBe(false);
});
});

View File

@ -139,3 +139,10 @@ export function getAllowedIconFormats(platform: string): string[] {
log.debug(`Allowed icon formats when building for ${platform}:`, formats);
return formats;
}
export function isArgFormatValid(arg: string): boolean {
return (
/^-[a-z]$/i.exec(arg) !== null ||
/^--[a-z]{2,}(-[a-z]{2,})*$/i.exec(arg) !== null
);
}