Explorar el Código

Merge pull request #762 from javascript-obfuscator/escape-sequence-transformer-refactoring

Refactoring of `unicodeEscapeSequence` logic
Timofey Kachalov hace 4 años
padre
commit
39d1c898ee

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 0 - 0
dist/index.browser.js


La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 0 - 0
dist/index.cli.js


La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 0 - 0
dist/index.js


+ 4 - 3
package.json

@@ -29,7 +29,7 @@
     "class-validator": "0.12.2",
     "commander": "6.1.0",
     "escodegen": "2.0.0",
-    "eslint-scope": "^5.1.1",
+    "eslint-scope": "5.1.1",
     "estraverse": "5.2.0",
     "eventemitter3": "4.0.7",
     "fast-deep-equal": "3.1.3",
@@ -50,13 +50,14 @@
     "@types/eslint-scope": "3.7.0",
     "@types/estraverse": "5.1.0",
     "@types/estree": "0.0.45",
+    "@types/js-string-escape": "1.0.0",
     "@types/md5": "2.2.0",
     "@types/mkdirp": "1.0.1",
     "@types/mocha": "8.0.3",
     "@types/multimatch": "4.0.0",
     "@types/node": "14.11.2",
     "@types/rimraf": "3.0.0",
-    "@types/sinon": "9.0.5",
+    "@types/sinon": "9.0.6",
     "@types/string-template": "1.0.2",
     "@types/webpack-env": "1.15.3",
     "@typescript-eslint/eslint-plugin": "4.2.0",
@@ -66,7 +67,7 @@
     "coveralls": "3.1.0",
     "eslint": "7.9.0",
     "eslint-plugin-import": "2.22.0",
-    "eslint-plugin-jsdoc": "30.5.1",
+    "eslint-plugin-jsdoc": "30.5.2",
     "eslint-plugin-no-null": "1.0.2",
     "eslint-plugin-prefer-arrow": "1.2.2",
     "eslint-plugin-unicorn": "22.0.0",

+ 17 - 1
src/custom-code-helpers/string-array/StringArrayCodeHelper.ts

@@ -9,6 +9,7 @@ import { ICustomCodeHelperObfuscator } from '../../interfaces/custom-code-helper
 import { IOptions } from '../../interfaces/options/IOptions';
 import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
 import { IStringArrayStorage } from '../../interfaces/storages/string-array-transformers/IStringArrayStorage';
+import { IStringArrayStorageItemData } from '../../interfaces/storages/string-array-transformers/IStringArrayStorageItem';
 
 import { initializable } from '../../decorators/Initializable';
 
@@ -16,6 +17,7 @@ import { StringArrayTemplate } from './templates/string-array/StringArrayTemplat
 
 import { AbstractCustomCodeHelper } from '../AbstractCustomCodeHelper';
 import { NodeUtils } from '../../node/NodeUtils';
+import { StringUtils } from '../../utils/StringUtils';
 
 @injectable()
 export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
@@ -81,7 +83,21 @@ export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
     protected getCodeHelperTemplate (): string {
         return this.customCodeHelperFormatter.formatTemplate(StringArrayTemplate(), {
             stringArrayName: this.stringArrayName,
-            stringArray: this.stringArrayStorage.toString()
+            stringArrayStorageItems: this.getEncodedStringArrayStorageItems()
         });
     }
+
+    /**
+     * @returns {string}
+     */
+    private getEncodedStringArrayStorageItems (): string {
+        return Array
+            .from(this.stringArrayStorage.getStorage().values())
+            .map((stringArrayStorageItemData: IStringArrayStorageItemData): string => {
+                const escapedEncodedValue: string = StringUtils.escapeJsString(stringArrayStorageItemData.encodedValue);
+
+                return `'${escapedEncodedValue}'`;
+            })
+            .toString();
+    }
 }

+ 1 - 1
src/custom-code-helpers/string-array/templates/string-array/StringArrayTemplate.ts

@@ -3,6 +3,6 @@
  */
 export function StringArrayTemplate (): string {
     return `
-        const {stringArrayName} = [{stringArray}];
+        const {stringArrayName} = [{stringArrayStorageItems}];
     `;
 }

+ 0 - 5
src/declarations/js-string-escape.d.ts

@@ -1,5 +0,0 @@
-declare module 'js-string-escape' {
-    function jsStringEscape (input: string): string;
-
-    export = jsStringEscape;
-}

+ 12 - 5
src/node-transformers/finalizing-transformers/EscapeSequenceTransformer.ts

@@ -9,6 +9,7 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
 import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
 
 import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
+import { NodeTransformer } from '../../enums/node-transformers/NodeTransformer';
 
 import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
 import { NodeGuards } from '../../node/NodeGuards';
@@ -18,6 +19,13 @@ import { NodeUtils } from '../../node/NodeUtils';
 
 @injectable()
 export class EscapeSequenceTransformer extends AbstractNodeTransformer {
+    /**
+     * @type {NodeTransformer[]}
+     */
+    public readonly runAfter: NodeTransformer[] = [
+        NodeTransformer.CustomCodeHelpersTransformer
+    ];
+
     /**
      * @type {IEscapeSequenceEncoder}
      */
@@ -68,12 +76,11 @@ export class EscapeSequenceTransformer extends AbstractNodeTransformer {
             return literalNode;
         }
 
-        const newLiteralNode: ESTree.Literal = NodeFactory.literalNode(
-            this.escapeSequenceEncoder.encode(
-                literalNode.value,
-                this.options.unicodeEscapeSequence
-            )
+        const encodedValue: string = this.escapeSequenceEncoder.encode(
+            literalNode.value,
+            this.options.unicodeEscapeSequence
         );
+        const newLiteralNode: ESTree.Literal = NodeFactory.literalNode(encodedValue);
 
         NodeUtils.parentizeNode(newLiteralNode, parentNode);
 

+ 3 - 2
src/node-transformers/preparing-transformers/EvalCallExpressionTransformer.ts

@@ -2,7 +2,6 @@ import { inject, injectable, } from 'inversify';
 import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
 
 import * as ESTree from 'estree';
-import jsStringEscape from 'js-string-escape';
 
 import { IOptions } from '../../interfaces/options/IOptions';
 import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
@@ -15,6 +14,7 @@ import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
 import { NodeFactory } from '../../node/NodeFactory';
 import { NodeGuards } from '../../node/NodeGuards';
 import { NodeUtils } from '../../node/NodeUtils';
+import { StringUtils } from '../../utils/StringUtils';
 
 @injectable()
 export class EvalCallExpressionTransformer extends AbstractNodeTransformer {
@@ -22,6 +22,7 @@ export class EvalCallExpressionTransformer extends AbstractNodeTransformer {
      * @type {NodeTransformer.ParentificationTransformer[]}
      */
     public readonly runAfter: NodeTransformer[] = [
+        NodeTransformer.EscapeSequenceTransformer,
         NodeTransformer.ParentificationTransformer,
         NodeTransformer.VariablePreserveTransformer
     ];
@@ -179,7 +180,7 @@ export class EvalCallExpressionTransformer extends AbstractNodeTransformer {
         return NodeFactory.callExpressionNode(
             NodeFactory.identifierNode('eval'),
             [
-                NodeFactory.literalNode(jsStringEscape(obfuscatedCode))
+                NodeFactory.literalNode(StringUtils.escapeJsString(obfuscatedCode))
             ]
         );
     }

+ 1 - 25
src/storages/string-array-transformers/StringArrayStorage.ts

@@ -7,7 +7,6 @@ import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';
 import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
 import { ICryptUtilsSwappedAlphabet } from '../../interfaces/utils/ICryptUtilsSwappedAlphabet';
 import { IEncodedValue } from '../../interfaces/IEncodedValue';
-import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
 import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
 import { IOptions } from '../../interfaces/options/IOptions';
 import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
@@ -55,11 +54,6 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
      */
     private readonly cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet;
 
-    /**
-     * @type {IEscapeSequenceEncoder}
-     */
-    private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
-
     /**
      * @type {IIdentifierNamesGenerator}
      */
@@ -96,7 +90,6 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
      * @param {IRandomGenerator} randomGenerator
      * @param {IOptions} options
      * @param {ICryptUtilsSwappedAlphabet} cryptUtilsSwappedAlphabet
-     * @param {IEscapeSequenceEncoder} escapeSequenceEncoder
      */
     public constructor (
         @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
@@ -104,15 +97,13 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
         @inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
         @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
         @inject(ServiceIdentifiers.IOptions) options: IOptions,
-        @inject(ServiceIdentifiers.ICryptUtilsSwappedAlphabet) cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet,
-        @inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder
+        @inject(ServiceIdentifiers.ICryptUtilsSwappedAlphabet) cryptUtilsSwappedAlphabet: ICryptUtilsSwappedAlphabet
     ) {
         super(randomGenerator, options);
 
         this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
         this.arrayUtils = arrayUtils;
         this.cryptUtilsSwappedAlphabet = cryptUtilsSwappedAlphabet;
-        this.escapeSequenceEncoder = escapeSequenceEncoder;
 
         this.rc4Keys = this.randomGenerator.getRandomGenerator()
             .n(
@@ -225,21 +216,6 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
         );
     }
 
-    /**
-     * @returns {string}
-     */
-    public toString (): string {
-        return Array
-            .from(this.storage.values())
-            .map((stringArrayStorageItemData: IStringArrayStorageItemData) => {
-                // we have to encode here, because of possible errors during `parse` of StringArrayCustomNode
-                return `'${this.escapeSequenceEncoder.encode(
-                    stringArrayStorageItemData.encodedValue,
-                    this.options.unicodeEscapeSequence
-                )}'`;
-            }).toString();
-    }
-
     /**
      * @param {string} value
      * @returns {IStringArrayStorageItemData}

+ 11 - 0
src/utils/StringUtils.ts

@@ -0,0 +1,11 @@
+import jsStringEscape from 'js-string-escape';
+
+export class StringUtils {
+    /**
+     * @param {string} string
+     * @returns {string}
+     */
+    public static escapeJsString (string: string): string {
+        return jsStringEscape(string);
+    }
+}

+ 1 - 2
test/declarations/index.d.ts

@@ -1,3 +1,2 @@
 /// <reference path="../../src/declarations/escodegen.d.ts" />
-/// <reference path="../../src/declarations/ESTree.d.ts" />
-/// <reference path="../../src/declarations/js-string-escape.d.ts" />
+/// <reference path="../../src/declarations/ESTree.d.ts" />

+ 3 - 5
test/dev/dev.ts

@@ -8,11 +8,9 @@ import { IdentifierNamesGenerator } from '../../src/enums/generators/identifier-
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-            const foo = 'foo test';
-
-            function test () {
-                const bar = 'bar';
-            }
+           var test = '\\nreturn \\n//# sourceURL= there can only be \\'^\\' and \\'!\\' markers in a subscription marble diagram.';
+           
+           console.log(test);
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,

+ 1 - 0
test/index.spec.ts

@@ -45,6 +45,7 @@ import './unit-tests/utils/CryptUtilsSwappedAlphabet.spec';
 import './unit-tests/utils/EscapeSequenceEncoder.spec';
 import './unit-tests/utils/LevelledTopologicalSorter.spec';
 import './unit-tests/utils/NumberUtils.spec';
+import './unit-tests/utils/StringUtils.spec';
 import './unit-tests/utils/Utils.spec';
 
 /**

+ 37 - 0
test/unit-tests/utils/StringUtils.spec.ts

@@ -0,0 +1,37 @@
+import { assert } from 'chai';
+
+import { StringUtils } from '../../../src/utils/StringUtils';
+
+describe('StringUtils', function () {
+    this.timeout(30000);
+
+    describe('escapeJsString', () => {
+        describe('Variant #1: single quotes', () => {
+            const expectedEscapedJsString: string = 'const foo = \\\'Hello World!\\\'';
+
+            let escapedJsString: string;
+
+            before(() => {
+                escapedJsString = StringUtils.escapeJsString('const foo = \'Hello World!\'');
+            });
+
+            it('should escape js string', () => {
+                assert.equal(escapedJsString, expectedEscapedJsString);
+            });
+        });
+
+        describe('Variant #2: double quotes', () => {
+            const expectedEscapedJsString: string = 'const foo = \\"Hello World!\\"';
+
+            let escapedJsString: string;
+
+            before(() => {
+                escapedJsString = StringUtils.escapeJsString('const foo = "Hello World!"');
+            });
+
+            it('should escape js string', () => {
+                assert.equal(escapedJsString, expectedEscapedJsString);
+            });
+        });
+    });
+});

+ 21 - 16
yarn.lock

@@ -455,6 +455,11 @@
     "@types/minimatch" "*"
     "@types/node" "*"
 
+"@types/[email protected]":
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/@types/js-string-escape/-/js-string-escape-1.0.0.tgz#96738a64ea912f7d4b87ee097231d68fd1813332"
+  integrity sha512-UANTN9S09hivqbeR4unjVS7DrtgjYUFNK4UCmHGPuwMrHyMFeU3z9KMg0wja/fTflXo7fVl0BsAohlgRO4QowQ==
+
 "@types/json-schema@*", "@types/json-schema@^7.0.3":
   version "7.0.4"
   resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
@@ -529,10 +534,10 @@
     "@types/glob" "*"
     "@types/node" "*"
 
-"@types/[email protected].5":
-  version "9.0.5"
-  resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-9.0.5.tgz#56b2a12662dd8c7d081cdc511af5f872cb37377f"
-  integrity sha512-4CnkGdM/5/FXDGqL32JQ1ttVrGvhOoesLLF7VnTh4KdjK5N5VQOtxaylFqqTjnHx55MnD9O02Nbk5c1ELC8wlQ==
+"@types/[email protected].6":
+  version "9.0.6"
+  resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-9.0.6.tgz#fb4b6883fe0417e6a1ac5d9753bdcb7016dd4dd0"
+  integrity sha512-j3GK0fiHgn8fe7sqOpInMjm0A2Tary1NBZ8gbI/sZ0C0JxYeO+nh8H0/pW/0l94vNWcH1FnZOZu/cOvIfNZTrg==
   dependencies:
     "@types/sinonjs__fake-timers" "*"
 
@@ -2133,10 +2138,10 @@ [email protected]:
     resolve "^1.17.0"
     tsconfig-paths "^3.9.0"
 
[email protected].1:
-  version "30.5.1"
-  resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-30.5.1.tgz#b024295db3ce3b258909419d9bcf747b8e65e2a7"
-  integrity sha512-cY3YNxdhFcQVkcQLnZw/iZGsTPMuWa9yWZclorMWkjdHprBQX0TMWMEcmJYM3IjHp1HJr7aD0Z0sCRifEBhnzg==
[email protected].2:
+  version "30.5.2"
+  resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-30.5.2.tgz#a2002c52d5fb4a9d42b9fa3a3136a9dbdcb7f69e"
+  integrity sha512-LJfjqVGYrPUZv4Gt+1OB5NHhySVPeX4Sdd1qUUjTPnBSwbezry7j3NdPVIeU67VwCVJU2rmqd6jhZ7VYec37Cw==
   dependencies:
     comment-parser "^0.7.6"
     debug "^4.1.1"
@@ -2175,6 +2180,14 @@ [email protected]:
     safe-regex "^2.1.1"
     semver "^7.3.2"
 
[email protected]:
+  version "5.1.1"
+  resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"
+  integrity sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==
+  dependencies:
+    esrecurse "^4.3.0"
+    estraverse "^4.1.1"
+
 eslint-scope@^4.0.3:
   version "4.0.3"
   resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-4.0.3.tgz#ca03833310f6889a3264781aa82e63eb9cfe7848"
@@ -2199,14 +2212,6 @@ eslint-scope@^5.1.0:
     esrecurse "^4.1.0"
     estraverse "^4.1.1"
 
-eslint-scope@^5.1.1:
-  version "5.1.1"
-  resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"
-  integrity sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==
-  dependencies:
-    esrecurse "^4.3.0"
-    estraverse "^4.1.1"
-
 eslint-template-visitor@^2.2.1:
   version "2.2.1"
   resolved "https://registry.yarnpkg.com/eslint-template-visitor/-/eslint-template-visitor-2.2.1.tgz#2dccb1ab28fa7429e56ba6dd0144def2d89bc2d6"

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio