Fix arg validation regression in #1080 (fix #1083)

This commit is contained in:
Ronan Jouchet 2020-12-06 23:19:02 -05:00
parent 46bc71cb63
commit f4af78018f
3 changed files with 25 additions and 29 deletions

View File

@ -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`,
);

View File

@ -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);
});
});

View File

@ -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
);
}