Pārlūkot izejas kodu

Merge pull request #654 from javascript-obfuscator/config-file-extension-validation

Added config file extension validation
Timofey Kachalov 4 gadi atpakaļ
vecāks
revīzija
43271f9f14

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@ Change Log
 v1.3.0
 ---
 * Improvements of `stringArrayEncoding`: `base64` and `rc4`
+* **CLI**: added config file extension validation (it still supports `.js` and `.json` extensions)
 
 v1.2.2
 ---

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/index.browser.js


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/index.cli.js


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/index.js


+ 18 - 1
src/cli/utils/CLIUtils.ts

@@ -1,6 +1,16 @@
+import * as path from 'path';
+
 import { TDictionary } from '../../types/TDictionary';
 
 export class CLIUtils {
+    /**
+     * @type {string[]}
+     */
+    public static readonly allowedConfigFileExtensions: string[] = [
+        '.js',
+        '.json'
+    ];
+
     /**
      * @param {string} configPath
      * @returns {TDictionary}
@@ -8,13 +18,20 @@ export class CLIUtils {
     public static getUserConfig (configPath: string): TDictionary {
         let config: TDictionary;
 
+        const configFileExtension: string = path.extname(configPath);
+        const isValidExtension: boolean = CLIUtils.allowedConfigFileExtensions.includes(configFileExtension);
+
+        if (!isValidExtension) {
+            throw new ReferenceError('Given config path must be a valid `.js` or `.json` file path');
+        }
+
         try {
             config = require(configPath);
         } catch {
             try {
                 config = __non_webpack_require__(configPath);
             } catch {
-                throw new ReferenceError('Given config path must be a valid `.js` or `.json` file path');
+                throw new ReferenceError(`Cannot open config file with path: ${configPath}`);
             }
         }
 

+ 18 - 2
test/unit-tests/cli/utils/CLIUtils.spec.ts

@@ -51,7 +51,23 @@ describe('CLIUtils', () => {
             });
         });
 
-        describe('Variant #2: invalid config file path', () => {
+        describe('Variant #2: invalid config file extension', () => {
+            const configDirName: string = 'test/fixtures';
+            const configFileName: string = 'configs.config';
+            const configFilePath: string = `../../../${configDirName}/${configFileName}`;
+
+            let testFunc: () => void;
+
+            before(() => {
+                testFunc = () => CLIUtils.getUserConfig(configFilePath);
+            });
+
+            it('should throw an error if `configFilePath` is not a valid path', () => {
+                assert.throws(testFunc, /Given config path must be a valid/);
+            });
+        });
+
+        describe('Variant #3: invalid config file path', () => {
             const configDirName: string = 'test/fixtures';
             const configFileName: string = 'configs.js';
             const configFilePath: string = `../../../${configDirName}/${configFileName}`;
@@ -63,7 +79,7 @@ describe('CLIUtils', () => {
             });
 
             it('should throw an error if `configFilePath` is not a valid path', () => {
-                assert.throws(testFunc, ReferenceError);
+                assert.throws(testFunc, /Cannot open config file/);
             });
         });
     });

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels