From f4af78018f9557214cd3ae49c7c5eca5e5681b9c Mon Sep 17 00:00:00 2001 From: Ronan Jouchet Date: Sun, 6 Dec 2020 23:19:02 -0500 Subject: [PATCH] Fix arg validation regression in #1080 (fix #1083) --- src/cli.ts | 4 ++-- src/helpers/helpers.test.ts | 40 +++++++++++++++---------------------- src/helpers/helpers.ts | 10 +++++++--- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 0f740bb..8793d9c 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -6,7 +6,7 @@ import * as dns from 'dns'; import * as log from 'loglevel'; import { buildNativefierApp } from './main'; -import { isArgFormatValid } from './helpers/helpers'; +import { isArgFormatInvalid } from './helpers/helpers'; import { isWindows } from './helpers/helpers'; // package.json is `require`d to let tsc strip the `src` folder by determining @@ -69,7 +69,7 @@ function checkInternet(): void { if (require.main === module) { const sanitizedArgs = []; process.argv.forEach((arg) => { - if (isArgFormatValid(arg) === false) { + if (isArgFormatInvalid(arg)) { 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`, ); diff --git a/src/helpers/helpers.test.ts b/src/helpers/helpers.test.ts index 0781c7b..1f50909 100644 --- a/src/helpers/helpers.test.ts +++ b/src/helpers/helpers.test.ts @@ -1,39 +1,31 @@ -import { isArgFormatValid } from './helpers'; +import { isArgFormatInvalid } from './helpers'; -describe('isArgFormatValid', () => { - test('is true for short arguments', () => { - expect(isArgFormatValid('-t')).toBe(true); +describe('isArgFormatInvalid', () => { + test('is false for correct short args', () => { + expect(isArgFormatInvalid('-t')).toBe(false); }); - test('is false for improperly formatted short arguments', () => { - expect(isArgFormatValid('--t')).toBe(false); + test('is true for improperly double-dashed short args', () => { + expect(isArgFormatInvalid('--t')).toBe(true); }); - test('is true for long arguments', () => { - expect(isArgFormatValid('--test')).toBe(true); + test('is false for correct long args', () => { + expect(isArgFormatInvalid('--test')).toBe(false); }); - test('is false for improperly formatted long arguments', () => { - expect(isArgFormatValid('---test')).toBe(false); + test('is true for improperly triple-dashed long args', () => { + expect(isArgFormatInvalid('---test')).toBe(true); }); - test('is false for improperly formatted long arguments', () => { - expect(isArgFormatValid('-test')).toBe(false); + test('is true for improperly single-dashed long args', () => { + expect(isArgFormatInvalid('-test')).toBe(true); }); - test('is true for long arguments with dashes', () => { - expect(isArgFormatValid('--test-run')).toBe(true); + test('is false for correct long args with dashes', () => { + expect(isArgFormatInvalid('--test-run')).toBe(false); }); - 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); + test('is false for correct long args with many dashes', () => { + expect(isArgFormatInvalid('--test-run-with-many-dashes')).toBe(false); }); }); diff --git a/src/helpers/helpers.ts b/src/helpers/helpers.ts index ccec0c5..4e2c0df 100644 --- a/src/helpers/helpers.ts +++ b/src/helpers/helpers.ts @@ -140,9 +140,13 @@ export function getAllowedIconFormats(platform: string): string[] { return formats; } -export function isArgFormatValid(arg: string): boolean { +/** + * Refuse args like '--n' or '-name', we accept either short '-n' or long '--name' + */ +export function isArgFormatInvalid(arg: string): boolean { return ( - /^-[a-z]$/i.exec(arg) !== null || - /^--[a-z]{2,}(-[a-z]{2,})*$/i.exec(arg) !== null + arg.startsWith('---') || + /^--[a-z]$/i.exec(arg) !== null || + /^-[a-z]{2,}$/i.exec(arg) !== null ); }